private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { if (string.IsNullOrEmpty(propertyName)) { throw JsonSerializationException.Create(reader, "XmlNodeConverter cannot convert JSON with an empty property name to XML."); } Dictionary <string, string> strs = this.ReadAttributeElements(reader, manager); string prefix = MiscellaneousUtils.GetPrefix(propertyName); if (propertyName.StartsWith('@')) { string str = propertyName.Substring(1); string prefix1 = MiscellaneousUtils.GetPrefix(str); XmlNodeConverter.AddAttribute(reader, document, currentNode, str, manager, prefix1); return; } if (propertyName.StartsWith('$')) { if (propertyName == "$values") { propertyName = propertyName.Substring(1); prefix = manager.LookupPrefix("http://james.newtonking.com/projects/json"); this.CreateElement(reader, document, currentNode, propertyName, manager, prefix, strs); return; } if (propertyName == "$id" || propertyName == "$ref" || propertyName == "$type" || propertyName == "$value") { string str1 = propertyName.Substring(1); string str2 = manager.LookupPrefix("http://james.newtonking.com/projects/json"); XmlNodeConverter.AddAttribute(reader, document, currentNode, str1, manager, str2); return; } } this.CreateElement(reader, document, currentNode, propertyName, manager, prefix, strs); }
private XmlNode DeserializeXmlNode(string json, string deserializeRootElementName) { JsonTextReader reader; reader = new JsonTextReader(new StringReader(json)); reader.Read(); XmlNodeConverter converter = new XmlNodeConverter(); if (deserializeRootElementName != null) converter.DeserializeRootElementName = deserializeRootElementName; XmlNode node = (XmlNode)converter.ReadJson(reader, typeof (XmlDocument), null, new JsonSerializer()); #if !NET20 string xmlText = node.OuterXml; reader = new JsonTextReader(new StringReader(json)); reader.Read(); XDocument d = (XDocument) converter.ReadJson(reader, typeof (XDocument), null, new JsonSerializer()); string linqXmlText = d.ToString(SaveOptions.DisableFormatting); if (d.Declaration != null) linqXmlText = d.Declaration + linqXmlText; Assert.AreEqual(xmlText, linqXmlText); #endif return node; }
public static String XDocToJson(XDocument xDoc) { var sw = new StringWriter(); using (var jsonWriter = new JsonPropertyFixupWriter(sw)) { // jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; var jsonSerializer = new JsonSerializer(); var converter = new XmlNodeConverter(); jsonSerializer.Converters.Add(converter); jsonSerializer.Serialize(jsonWriter, xDoc); } var jsonText = sw.ToString(); return jsonText; }
/// <summary> /// Initializes a new instance of the <see cref="JsonInterpreter"/> class. /// </summary> /// <param name="source">The <see cref="IResource"/>.</param> public JsonInterpreter(IResource source) : base(source) { string xmlContents = string.Empty; using(source) { JsonReader reader = new JsonTextReader(new StreamReader(source.Stream)); reader.Read(); XmlNodeConverter toXml = new XmlNodeConverter(); XmlDocument xmlDoc = (XmlDocument)toXml.ReadJson(reader, typeof(XmlDocument)); xmlContents = xmlDoc.OuterXml; reader.Close(); } IResource resource = new StaticContentResource(xmlContents); xmlInterpreter = new XmlConfigurationInterpreter(resource); }
public void ConvertNullString() { JObject json = new JObject(); json["Prop1"] = (string)null; json["Prop2"] = new MyModel().MyProperty; var xmlNodeConverter = new XmlNodeConverter { DeserializeRootElementName = "object" }; var jsonSerializerSettings = new JsonSerializerSettings { Converters = new JsonConverter[] { xmlNodeConverter } }; var jsonSerializer = JsonSerializer.CreateDefault(jsonSerializerSettings); XDocument d = json.ToObject<XDocument>(jsonSerializer); StringAssert.Equals(@"<object> <Prop1 /> <Prop2 /> </object>", d.ToString()); }
private void SerializeNode( JsonWriter writer, IXmlNode node, XmlNamespaceManager manager, bool writePropertyName) { switch (node.NodeType) { case XmlNodeType.Element: if (this.IsArray(node) && XmlNodeConverter.AllSameName(node) && node.ChildNodes.Count > 0) { this.SerializeGroupedNodes(writer, node, manager, false); break; } manager.PushScope(); foreach (IXmlNode attribute in node.Attributes) { if (attribute.NamespaceUri == "http://www.w3.org/2000/xmlns/") { string prefix = attribute.LocalName != "xmlns" ? XmlConvert.DecodeName(attribute.LocalName) : string.Empty; string uri = attribute.Value; manager.AddNamespace(prefix, uri); } } if (writePropertyName) { writer.WritePropertyName(this.GetPropertyName(node, manager)); } if (!this.ValueAttributes(node.Attributes) && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text) { writer.WriteValue(node.ChildNodes[0].Value); } else if (node.ChildNodes.Count == 0 && CollectionUtils.IsNullOrEmpty <IXmlNode>((ICollection <IXmlNode>)node.Attributes)) { if (((IXmlElement)node).IsEmpty) { writer.WriteNull(); } else { writer.WriteValue(string.Empty); } } else { writer.WriteStartObject(); for (int index = 0; index < node.Attributes.Count; ++index) { this.SerializeNode(writer, node.Attributes[index], manager, true); } this.SerializeGroupedNodes(writer, node, manager, true); writer.WriteEndObject(); } manager.PopScope(); break; case XmlNodeType.Attribute: case XmlNodeType.Text: case XmlNodeType.CDATA: case XmlNodeType.ProcessingInstruction: case XmlNodeType.Whitespace: case XmlNodeType.SignificantWhitespace: if (node.NamespaceUri == "http://www.w3.org/2000/xmlns/" && node.Value == "http://james.newtonking.com/projects/json" || node.NamespaceUri == "http://james.newtonking.com/projects/json" && node.LocalName == "Array") { break; } if (writePropertyName) { writer.WritePropertyName(this.GetPropertyName(node, manager)); } writer.WriteValue(node.Value); break; case XmlNodeType.Comment: if (!writePropertyName) { break; } writer.WriteComment(node.Value); break; case XmlNodeType.Document: case XmlNodeType.DocumentFragment: this.SerializeGroupedNodes(writer, node, manager, writePropertyName); break; case XmlNodeType.DocumentType: IXmlDocumentType xmlDocumentType = (IXmlDocumentType)node; writer.WritePropertyName(this.GetPropertyName(node, manager)); writer.WriteStartObject(); if (!string.IsNullOrEmpty(xmlDocumentType.Name)) { writer.WritePropertyName("@name"); writer.WriteValue(xmlDocumentType.Name); } if (!string.IsNullOrEmpty(xmlDocumentType.Public)) { writer.WritePropertyName("@public"); writer.WriteValue(xmlDocumentType.Public); } if (!string.IsNullOrEmpty(xmlDocumentType.System)) { writer.WritePropertyName("@system"); writer.WriteValue(xmlDocumentType.System); } if (!string.IsNullOrEmpty(xmlDocumentType.InternalSubset)) { writer.WritePropertyName("@internalSubset"); writer.WriteValue(xmlDocumentType.InternalSubset); } writer.WriteEndObject(); break; case XmlNodeType.XmlDeclaration: IXmlDeclaration xmlDeclaration = (IXmlDeclaration)node; writer.WritePropertyName(this.GetPropertyName(node, manager)); writer.WriteStartObject(); if (!string.IsNullOrEmpty(xmlDeclaration.Version)) { writer.WritePropertyName("@version"); writer.WriteValue(xmlDeclaration.Version); } if (!string.IsNullOrEmpty(xmlDeclaration.Encoding)) { writer.WritePropertyName("@encoding"); writer.WriteValue(xmlDeclaration.Encoding); } if (!string.IsNullOrEmpty(xmlDeclaration.Standalone)) { writer.WritePropertyName("@standalone"); writer.WriteValue(xmlDeclaration.Standalone); } writer.WriteEndObject(); break; default: throw new JsonSerializationException("Unexpected XmlNodeType when serializing nodes: " + (object)node.NodeType); } }
public static XmlNode DeerializeXmlNode(string value) { XmlNodeConverter converter = new XmlNodeConverter(); return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter); }
public static string SerializeXmlNode(XmlNode node) { XmlNodeConverter converter = new XmlNodeConverter(); return SerializeObject(node, converter); }