Пример #1
0
        /// <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
              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;
        }
Пример #2
0
        /// <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
            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(string.Format("Unexpected type when converting XML: {0}", objectType));
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object.");
            }

            if (!string.IsNullOrEmpty(this.DeserializeRootElementName))
            {
                //rootNode = document.CreateElement(DeserializeRootElementName);
                //document.AppendChild(rootNode);
                this.ReadElement(reader, document, rootNode, DeserializeRootElementName, manager);
            }
            else
            {
                reader.Read();
                this.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);
        }