| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Runtime.Serialization; |
| | | 6 | | using System.Xml; |
| | | 7 | | using System.Xml.Serialization; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Dispatcher |
| | | 10 | | { |
| | | 11 | | internal class XmlSerializerObjectSerializer : XmlObjectSerializer |
| | | 12 | | { |
| | | 13 | | private XmlSerializer _serializer; |
| | | 14 | | private Type _rootType; |
| | | 15 | | private string _rootName; |
| | | 16 | | private string _rootNamespace; |
| | | 17 | | private bool _isSerializerSetExplicit = false; |
| | | 18 | | |
| | 0 | 19 | | internal XmlSerializerObjectSerializer(Type type) |
| | | 20 | | { |
| | 0 | 21 | | Initialize(type, null /*rootName*/, null /*rootNamespace*/, null /*xmlSerializer*/); |
| | 0 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | internal XmlSerializerObjectSerializer(Type type, XmlQualifiedName qualifiedName, XmlSerializer xmlSerializer) |
| | | 25 | | { |
| | 0 | 26 | | if (qualifiedName == null) |
| | | 27 | | { |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(qualifiedName)); |
| | | 29 | | } |
| | 0 | 30 | | Initialize(type, qualifiedName.Name, qualifiedName.Namespace, xmlSerializer); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private void Initialize(Type type, string rootName, string rootNamespace, XmlSerializer xmlSerializer) |
| | | 34 | | { |
| | 0 | 35 | | _rootType = type ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(type)); |
| | 0 | 36 | | _rootName = rootName; |
| | 0 | 37 | | _rootNamespace = rootNamespace ?? string.Empty; |
| | 0 | 38 | | _serializer = xmlSerializer; |
| | | 39 | | |
| | 0 | 40 | | if (_serializer == null) |
| | | 41 | | { |
| | 0 | 42 | | if (_rootName == null) |
| | | 43 | | { |
| | 0 | 44 | | _serializer = new XmlSerializer(type); |
| | | 45 | | } |
| | | 46 | | else |
| | | 47 | | { |
| | 0 | 48 | | XmlRootAttribute xmlRoot = new XmlRootAttribute |
| | 0 | 49 | | { |
| | 0 | 50 | | ElementName = _rootName, |
| | 0 | 51 | | Namespace = _rootNamespace |
| | 0 | 52 | | }; |
| | 0 | 53 | | _serializer = new XmlSerializer(type, xmlRoot); |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 0 | 58 | | _isSerializerSetExplicit = true; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | //try to get rootName and rootNamespace from type since root name not set explicitly |
| | 0 | 62 | | if (_rootName == null) |
| | | 63 | | { |
| | 0 | 64 | | XmlTypeMapping mapping = new XmlReflectionImporter().ImportTypeMapping(_rootType); |
| | 0 | 65 | | _rootName = mapping.ElementName; |
| | 0 | 66 | | _rootNamespace = mapping.Namespace; |
| | | 67 | | } |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public override void WriteObject(XmlDictionaryWriter writer, object graph) |
| | | 71 | | { |
| | 0 | 72 | | if (_isSerializerSetExplicit) |
| | | 73 | | { |
| | 0 | 74 | | _serializer.Serialize(writer, new object[] { graph }); |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 0 | 78 | | _serializer.Serialize(writer, graph); |
| | | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public override void WriteStartObject(XmlDictionaryWriter writer, object graph) |
| | | 83 | | { |
| | 0 | 84 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public override void WriteObjectContent(XmlDictionaryWriter writer, object graph) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public override void WriteEndObject(XmlDictionaryWriter writer) |
| | | 93 | | { |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException()); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName) |
| | | 98 | | { |
| | 0 | 99 | | if (_isSerializerSetExplicit) |
| | | 100 | | { |
| | 0 | 101 | | object[] deserializedObjects = (object[])_serializer.Deserialize(reader); |
| | 0 | 102 | | if (deserializedObjects != null && deserializedObjects.Length > 0) |
| | | 103 | | { |
| | 0 | 104 | | return deserializedObjects[0]; |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | return null; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | else |
| | | 112 | | { |
| | 0 | 113 | | return _serializer.Deserialize(reader); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public override bool IsStartObject(XmlDictionaryReader reader) |
| | | 118 | | { |
| | 0 | 119 | | if (reader == null) |
| | | 120 | | { |
| | 0 | 121 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | reader.MoveToElement(); |
| | | 125 | | |
| | 0 | 126 | | if (_rootName != null) |
| | | 127 | | { |
| | 0 | 128 | | return reader.IsStartElement(_rootName, _rootNamespace); |
| | | 129 | | } |
| | | 130 | | else |
| | | 131 | | { |
| | 0 | 132 | | return reader.IsStartElement(); |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |