-When converting JSON to XML using XmlNodeConverter, changed special JSON properties ($id, $ref, $type) to be attributes rather than child elements

This commit is contained in:
JamesNK
2010-02-21 03:32:32 +00:00
parent 262bb492f5
commit 98f3757498
3 changed files with 353 additions and 161 deletions
@@ -25,6 +25,7 @@
#if !SILVERLIGHT
using System;
using Newtonsoft.Json.Tests.Serialization;
using NUnit.Framework;
using Newtonsoft.Json;
using System.IO;
@@ -71,12 +72,19 @@ namespace Newtonsoft.Json.Tests.Converters
}
private XmlNode DeserializeXmlNode(string json)
{
return DeserializeXmlNode(json, null);
}
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), new JsonSerializer());
@@ -848,6 +856,56 @@ namespace Newtonsoft.Json.Tests.Converters
</person>
</root>".Replace(Environment.NewLine, string.Empty), doc.InnerXml);
}
[Test]
public void SerializeDeserializeSpecialProperties()
{
PreserveReferencesHandlingTests.CircularDictionary circularDictionary = new PreserveReferencesHandlingTests.CircularDictionary();
circularDictionary.Add("other", new PreserveReferencesHandlingTests.CircularDictionary { { "blah", null } });
circularDictionary.Add("self", circularDictionary);
string json = JsonConvert.SerializeObject(circularDictionary, Formatting.Indented,
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
Assert.AreEqual(@"{
""$id"": ""1"",
""other"": {
""$id"": ""2"",
""blah"": null
},
""self"": {
""$ref"": ""1""
}
}", json);
XmlNode node = DeserializeXmlNode(json, "root");
string xml = GetIndentedInnerXml(node);
string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<root xmlns:json=""http://james.newtonking.com/projects/json"" json:id=""1"">
<other json:id=""2"">
<blah />
</other>
<self json:ref=""1"" />
</root>";
Assert.AreEqual(expected, xml);
string xmlJson = SerializeXmlNode(node);
string expectedXmlJson = @"{
""root"": {
""$id"": ""1"",
""other"": {
""$id"": ""2"",
""blah"": null
},
""self"": {
""$ref"": ""1""
}
}
}";
Assert.AreEqual(expectedXmlJson, xmlJson);
}
}
}
#endif