public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } XmlNamespaceManager xmlNamespaceManagers = new XmlNamespaceManager(new NameTable()); IXmlDocument xDocumentWrapper = null; IXmlNode xmlNode = null; if (typeof(XObject).IsAssignableFrom(objectType)) { if (objectType != typeof(XDocument) && objectType != typeof(XElement)) { throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement."); } xDocumentWrapper = new XDocumentWrapper(new XDocument()); xmlNode = xDocumentWrapper; } if (typeof(XmlNode).IsAssignableFrom(objectType)) { if (objectType != typeof(XmlDocument)) { throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments"); } xDocumentWrapper = new XmlDocumentWrapper(new XmlDocument() { XmlResolver = null }); xmlNode = xDocumentWrapper; } if (xDocumentWrapper == null || xmlNode == null) { throw new JsonSerializationException(string.Concat("Unexpected type when converting XML: ", objectType)); } if (reader.TokenType != JsonToken.StartObject) { throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object."); } if (string.IsNullOrEmpty(this.DeserializeRootElementName)) { reader.Read(); this.DeserializeNode(reader, xDocumentWrapper, xmlNamespaceManagers, xmlNode); } else { this.ReadElement(reader, xDocumentWrapper, xmlNode, this.DeserializeRootElementName, xmlNamespaceManagers); } if (objectType != typeof(XElement)) { return(xDocumentWrapper.WrappedNode); } XElement wrappedNode = (XElement)xDocumentWrapper.DocumentElement.WrappedNode; wrappedNode.Remove(); return(wrappedNode); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable()); IXmlDocument document = null; IXmlNode currentNode = null; if (typeof(XObject).IsAssignableFrom(objectType)) { if ((objectType != typeof(XDocument)) && (objectType != typeof(XElement))) { throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement."); } document = new XDocumentWrapper(new XDocument()); currentNode = document; } if (typeof(System.Xml.XmlNode).IsAssignableFrom(objectType)) { if (objectType != typeof(XmlDocument)) { throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments"); } XmlDocument document1 = new XmlDocument { XmlResolver = null }; document = new XmlDocumentWrapper(document1); currentNode = document; } if ((document == null) || (currentNode == null)) { throw new JsonSerializationException("Unexpected type when converting XML: " + objectType); } if (reader.TokenType != JsonToken.StartObject) { throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object."); } if (!string.IsNullOrEmpty(this.DeserializeRootElementName)) { this.ReadElement(reader, document, currentNode, this.DeserializeRootElementName, manager); } else { reader.Read(); this.DeserializeNode(reader, document, manager, currentNode); } if (objectType == typeof(XElement)) { XElement wrappedNode = (XElement)document.DocumentElement.WrappedNode; wrappedNode.Remove(); return(wrappedNode); } return(document.WrappedNode); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable()); IXmlDocument document = null; IXmlNode rootNode = null; if (typeof(XObject).IsAssignableFrom(objectType)) { if (objectType != typeof(XDocument) && objectType != typeof(XElement)) { throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement."); } XDocument d = new XDocument(); document = new XDocumentWrapper(d); rootNode = document; } if (document == null || rootNode == null) { throw new JsonSerializationException("Unexpected type when converting XML: " + objectType); } if (reader.TokenType != JsonToken.StartObject) { throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object."); } if (!string.IsNullOrEmpty(DeserializeRootElementName)) { //rootNode = document.CreateElement(DeserializeRootElementName); //document.AppendChild(rootNode); ReadElement(reader, document, rootNode, DeserializeRootElementName, manager); } else { reader.Read(); DeserializeNode(reader, document, manager, rootNode); } if (objectType == typeof(XElement)) { XElement element = (XElement)document.DocumentElement.WrappedNode; element.Remove(); return(element); } return(document.WrappedNode); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable()); IXmlDocument document = null; IXmlNode rootNode = null; #if !NET20 if (typeof(XObject).IsAssignableFrom(objectType)) { if (objectType != typeof (XDocument) && objectType != typeof (XElement)) throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement."); XDocument d = new XDocument(); document = new XDocumentWrapper(d); rootNode = document; } #endif #if !(SILVERLIGHT || NETFX_CORE) if (typeof(XmlNode).IsAssignableFrom(objectType)) { if (objectType != typeof (XmlDocument)) throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments"); XmlDocument d = new XmlDocument(); document = new XmlDocumentWrapper(d); rootNode = document; } #endif if (document == null || rootNode == null) throw new JsonSerializationException("Unexpected type when converting XML: " + objectType); if (reader.TokenType != JsonToken.StartObject) throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object."); if (!string.IsNullOrEmpty(DeserializeRootElementName)) { //rootNode = document.CreateElement(DeserializeRootElementName); //document.AppendChild(rootNode); ReadElement(reader, document, rootNode, DeserializeRootElementName, manager); } else { reader.Read(); DeserializeNode(reader, document, manager, rootNode); } #if !NET20 if (objectType == typeof(XElement)) { XElement element = (XElement)document.DocumentElement.WrappedNode; element.Remove(); return element; } #endif return document.WrappedNode; }