| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.InteropExtensions |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.IO; |
| | 8 | | using System.Runtime.Serialization; |
| | 9 | | using System.Xml; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// This class describes a serializer class used to serialize and deserialize an Object. |
| | 13 | | /// This class is almost identical to DataContractSerializer; only difference is that |
| | 14 | | /// ReadObject(Stream) and WriteObject(Stream, object) pick Binary Xml Reader/Writer |
| | 15 | | /// instead of text. |
| | 16 | | /// </summary> |
| | 17 | | sealed class DataContractBinarySerializer : XmlObjectSerializer |
| | 18 | | { |
| | 19 | | readonly DataContractSerializer dataContractSerializer; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Initializes a new DataContractBinarySerializer instance |
| | 23 | | /// </summary> |
| | 24 | | public DataContractBinarySerializer(Type type) |
| | 25 | | { |
| | 26 | | this.dataContractSerializer = new DataContractSerializer(type); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Converts from stream to the corresponding object |
| | 31 | | /// </summary> |
| | 32 | | /// <returns>Object corresponding to the stream</returns> |
| | 33 | | /// <remarks>Override the default (Text) and use Binary Xml Reader instead</remarks> |
| | 34 | | public override object ReadObject(Stream stream) |
| | 35 | | { |
| | 36 | | return this.ReadObject(XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Serializes the object into the stream |
| | 41 | | /// </summary> |
| | 42 | | /// <remarks>Override the default (Text) and use Binary Xml Reader instead</remarks> |
| | 43 | | public override void WriteObject(Stream stream, object graph) |
| | 44 | | { |
| | 45 | | if (stream == null) |
| | 46 | | { |
| | 47 | | throw new ArgumentNullException(nameof(stream)); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | var xmlDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream, null, null, false); |
| | 51 | | this.WriteObject(xmlDictionaryWriter, graph); |
| | 52 | | xmlDictionaryWriter.Flush(); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Serializes the object into the stream using the XmlDictionaryWriter |
| | 57 | | /// </summary> |
| | 58 | | public override void WriteObject(XmlDictionaryWriter writer, object graph) |
| | 59 | | { |
| | 60 | | if (writer == null) |
| | 61 | | { |
| | 62 | | throw new ArgumentNullException(nameof(writer)); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | this.dataContractSerializer.WriteObject(writer, graph); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// This method simply delegates to the DataContractSerializer implementation |
| | 70 | | /// </summary> |
| | 71 | | public override bool IsStartObject(XmlDictionaryReader reader) |
| | 72 | | { |
| | 73 | | return this.dataContractSerializer.IsStartObject(reader); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// This method simply delegates to the DataContractSerializer implementation |
| | 78 | | /// </summary> |
| | 79 | | public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName) |
| | 80 | | { |
| | 81 | | return this.dataContractSerializer.ReadObject(reader, verifyObjectName); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// This method simply delegates to the DataContractSerializer implementation |
| | 86 | | /// </summary> |
| | 87 | | public override void WriteEndObject(XmlDictionaryWriter writer) |
| | 88 | | { |
| | 89 | | this.dataContractSerializer.WriteEndObject(writer); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// This method simply delegates to the DataContractSerializer implementation |
| | 94 | | /// </summary> |
| | 95 | | public override void WriteObjectContent(XmlDictionaryWriter writer, object graph) |
| | 96 | | { |
| | 97 | | this.dataContractSerializer.WriteObjectContent(writer, graph); |
| | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// This method simply delegates to the DataContractSerializer implementation |
| | 102 | | /// </summary> |
| | 103 | | public override void WriteStartObject(XmlDictionaryWriter writer, object graph) |
| | 104 | | { |
| | 105 | | this.dataContractSerializer.WriteStartObject(writer, graph); |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Returns a static <see cref="DataContractBinarySerializer"/> instance of type T |
| | 111 | | /// </summary> |
| | 112 | | public static class DataContractBinarySerializer<T> |
| | 113 | | { |
| | 114 | | /// <summary> |
| | 115 | | /// Initializes a DataContractBinarySerializer instance of type T |
| | 116 | | /// </summary> |
| 0 | 117 | | public static readonly XmlObjectSerializer Instance = new DataContractBinarySerializer(typeof(T)); |
| | 118 | | } |
| | 119 | | } |