-Add LINQ to XML object support to XmlNodeConverter
-Add SerializeXNode, DeserializeXNode methods to JsonConvert -Remove "-" prefix when serializing a JSON constructor, prefix not compatible with XML naming standard
This commit is contained in:
@@ -32,11 +32,71 @@ using System.Xml;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
#if !NET20
|
||||
using System.Xml.Linq;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
public class XmlNodeConverterTest : TestFixtureBase
|
||||
{
|
||||
private string SerializeXmlNode(XmlNode node)
|
||||
{
|
||||
string json = JsonConvert.SerializeXmlNode(node, Formatting.Indented);
|
||||
XmlNodeReader reader = new XmlNodeReader(node);
|
||||
|
||||
#if !NET20
|
||||
XObject xNode;
|
||||
if (node is XmlDocument)
|
||||
{
|
||||
xNode = XDocument.Load(reader);
|
||||
}
|
||||
else if (node is XmlAttribute)
|
||||
{
|
||||
XmlAttribute attribute = (XmlAttribute) node;
|
||||
xNode = new XAttribute(XName.Get(attribute.LocalName, attribute.NamespaceURI), attribute.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
reader.MoveToContent();
|
||||
xNode = XNode.ReadFrom(reader);
|
||||
}
|
||||
|
||||
string linqJson = JsonConvert.SerializeXNode(xNode, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(json, linqJson);
|
||||
#endif
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
private XmlNode DeserializeXmlNode(string json)
|
||||
{
|
||||
JsonTextReader reader;
|
||||
|
||||
reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
|
||||
XmlNode node = (XmlNode)converter.ReadJson(reader, typeof (XmlDocument), new JsonSerializer());
|
||||
|
||||
#if !NET20
|
||||
string xmlText = node.OuterXml;
|
||||
|
||||
reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
XDocument d = (XDocument) converter.ReadJson(reader, typeof (XDocument), new JsonSerializer());
|
||||
|
||||
string linqXmlText = d.ToString(SaveOptions.DisableFormatting);
|
||||
if (d.Declaration != null)
|
||||
linqXmlText = d.Declaration + linqXmlText;
|
||||
|
||||
Assert.AreEqual(xmlText, linqXmlText);
|
||||
#endif
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DocumentSerializeIndented()
|
||||
{
|
||||
@@ -53,19 +113,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
JsonSerializer jsonSerializer = new JsonSerializer();
|
||||
jsonSerializer.Converters.Add(new XmlNodeConverter());
|
||||
|
||||
jsonSerializer.Serialize(jsonWriter, doc);
|
||||
}
|
||||
|
||||
string jsonText = sw.ToString();
|
||||
string jsonText = SerializeXmlNode(doc);
|
||||
string expected = @"{
|
||||
""?xml"": {
|
||||
""@version"": ""1.0"",
|
||||
@@ -119,8 +167,20 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
Console.WriteLine("SerializeNodeTypes");
|
||||
|
||||
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
|
||||
<xs:schema xs:id=""SomeID""
|
||||
xmlns=""""
|
||||
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
|
||||
xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
|
||||
<xs:element name=""MyDataSet"" msdata:IsDataSet=""true"">
|
||||
</xs:element>
|
||||
</xs:schema>";
|
||||
|
||||
XmlDocument document = new XmlDocument();
|
||||
document.LoadXml(xml);
|
||||
|
||||
// XmlAttribute
|
||||
XmlAttribute attribute = doc.CreateAttribute("msdata:IsDataSet");
|
||||
XmlAttribute attribute = document.DocumentElement.ChildNodes[0].Attributes["IsDataSet", "urn:schemas-microsoft-com:xml-msdata"];
|
||||
attribute.Value = "true";
|
||||
|
||||
jsonText = JsonConvert.SerializeXmlNode(attribute);
|
||||
@@ -128,6 +188,14 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Console.WriteLine(jsonText);
|
||||
Assert.AreEqual(@"{""@msdata:IsDataSet"":""true""}", jsonText);
|
||||
|
||||
#if !NET20
|
||||
XDocument d = XDocument.Parse(xml);
|
||||
XAttribute a = d.Root.Element("{http://www.w3.org/2001/XMLSchema}element").Attribute("{urn:schemas-microsoft-com:xml-msdata}IsDataSet");
|
||||
|
||||
jsonText = JsonConvert.SerializeXNode(a);
|
||||
|
||||
Assert.AreEqual(@"{""@msdata:IsDataSet"":""true""}", jsonText);
|
||||
#endif
|
||||
|
||||
// XmlProcessingInstruction
|
||||
XmlProcessingInstruction instruction = doc.CreateProcessingInstruction("xml-stylesheet", @"href=""classic.xsl"" type=""text/xml""");
|
||||
@@ -148,16 +216,33 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
|
||||
// XmlElement
|
||||
XmlElement element = doc.CreateElement("xs:Choice");
|
||||
element.SetAttributeNode(attribute);
|
||||
XmlElement element = doc.CreateElement("xs", "Choice", "http://www.w3.org/2001/XMLSchema");
|
||||
element.SetAttributeNode(doc.CreateAttribute("msdata", "IsDataSet", "urn:schemas-microsoft-com:xml-msdata"));
|
||||
|
||||
XmlAttribute aa = doc.CreateAttribute(@"xmlns", "xs", "http://www.w3.org/2000/xmlns/");
|
||||
aa.Value = "http://www.w3.org/2001/XMLSchema";
|
||||
element.SetAttributeNode(aa);
|
||||
|
||||
aa = doc.CreateAttribute(@"xmlns", "msdata", "http://www.w3.org/2000/xmlns/");
|
||||
aa.Value = "urn:schemas-microsoft-com:xml-msdata";
|
||||
element.SetAttributeNode(aa);
|
||||
|
||||
element.AppendChild(instruction);
|
||||
element.AppendChild(cDataSection);
|
||||
|
||||
jsonText = JsonConvert.SerializeXmlNode(element);
|
||||
doc.AppendChild(element);
|
||||
|
||||
Console.WriteLine(jsonText);
|
||||
Assert.AreEqual(@"{""xs:Choice"":{""@msdata:IsDataSet"":""true"",""?xml-stylesheet"":""href=\""classic.xsl\"" type=\""text/xml\"""",""#cdata-section"":""<Kiwi>true</Kiwi>""}}", jsonText);
|
||||
jsonText = JsonConvert.SerializeXmlNode(element, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""xs:Choice"": {
|
||||
""@msdata:IsDataSet"": """",
|
||||
""@xmlns:xs"": ""http://www.w3.org/2001/XMLSchema"",
|
||||
""@xmlns:msdata"": ""urn:schemas-microsoft-com:xml-msdata"",
|
||||
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
|
||||
""#cdata-section"": ""<Kiwi>true</Kiwi>""
|
||||
}
|
||||
}", jsonText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -210,9 +295,56 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
string jsonText = JsonConvert.SerializeXmlNode(doc);
|
||||
string jsonText = SerializeXmlNode(doc);
|
||||
|
||||
XmlDocument deserializedDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
string expected = @"{
|
||||
""?xml"": {
|
||||
""@version"": ""1.0"",
|
||||
""@encoding"": ""utf-8""
|
||||
},
|
||||
""xs:schema"": {
|
||||
""@xs:id"": ""SomeID"",
|
||||
""@xmlns"": """",
|
||||
""@xmlns:xs"": ""http://www.w3.org/2001/XMLSchema"",
|
||||
""@xmlns:msdata"": ""urn:schemas-microsoft-com:xml-msdata"",
|
||||
""xs:element"": {
|
||||
""@name"": ""MyDataSet"",
|
||||
""@msdata:IsDataSet"": ""true"",
|
||||
""xs:complexType"": {
|
||||
""xs:choice"": {
|
||||
""@maxOccurs"": ""unbounded"",
|
||||
""xs:element"": {
|
||||
""@name"": ""customers"",
|
||||
""xs:complexType"": {
|
||||
""xs:sequence"": {
|
||||
""xs:element"": [
|
||||
{
|
||||
""@name"": ""CustomerID"",
|
||||
""@type"": ""xs:integer"",
|
||||
""@minOccurs"": ""0""
|
||||
},
|
||||
{
|
||||
""@name"": ""CompanyName"",
|
||||
""@type"": ""xs:string"",
|
||||
""@minOccurs"": ""0""
|
||||
},
|
||||
{
|
||||
""@name"": ""Phone"",
|
||||
""@type"": ""xs:string""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
Assert.AreEqual(expected, jsonText);
|
||||
|
||||
XmlDocument deserializedDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
Assert.AreEqual(doc.InnerXml, deserializedDoc.InnerXml);
|
||||
|
||||
@@ -244,7 +376,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
}";
|
||||
|
||||
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
XmlDocument doc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
string expected = @"<?xml version=""1.0"" standalone=""no""?>
|
||||
<span class=""vevent"">
|
||||
@@ -295,9 +427,9 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
string jsonText = JsonConvert.SerializeXmlNode(doc);
|
||||
string jsonText = SerializeXmlNode(doc);
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
|
||||
}
|
||||
@@ -320,11 +452,11 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
string jsonText = JsonConvert.SerializeXmlNode(doc);
|
||||
string jsonText = SerializeXmlNode(doc);
|
||||
|
||||
Console.WriteLine(jsonText);
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
|
||||
}
|
||||
@@ -334,7 +466,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
string jsonText = @"{""?xml"":{""@version"":""1.0"",""@standalone"":""no""},""root"":{""person"":[{""@id"":""1"",""Float"":2.5,""Integer"":99},{""@id"":""2"",""Boolean"":true,""date"":""\/Date(954374400000)\/""}]}}";
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
string expected = @"<?xml version=""1.0"" standalone=""no""?><root><person id=""1""><Float>2.5</Float><Integer>99</Integer></person><person id=""2""><Boolean>true</Boolean><date>2000-03-30T00:00:00Z</date></person></root>";
|
||||
|
||||
@@ -360,14 +492,25 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
string jsonText = @"{root:{r:new Date(34343, 55)}}";
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
string expected = @"<root><r><-Date>34343</-Date><-Date>55</-Date></r></root>";
|
||||
string expected = @"<root><r><Date>34343</Date><Date>55</Date></r></root>";
|
||||
|
||||
Assert.AreEqual(expected, newDoc.InnerXml);
|
||||
|
||||
string json = JsonConvert.SerializeXmlNode(newDoc);
|
||||
Assert.AreEqual(@"{""root"":{""r"":new Date(""34343"",""55"")}}", json);
|
||||
string json = SerializeXmlNode(newDoc);
|
||||
expected = @"{
|
||||
""root"": {
|
||||
""r"": {
|
||||
""Date"": [
|
||||
""34343"",
|
||||
""55""
|
||||
]
|
||||
}
|
||||
}
|
||||
}";
|
||||
|
||||
Assert.AreEqual(expected, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -384,8 +527,20 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument arrayDoc = new XmlDocument();
|
||||
arrayDoc.LoadXml(arrayXml);
|
||||
|
||||
string arrayJsonText = JsonConvert.SerializeXmlNode(arrayDoc);
|
||||
Assert.AreEqual(@"{""root"":{""person"":{""@id"":""1"",""name"":""Alan"",""url"":""http://www.google.com"",""role"":[""Admin""]}}}", arrayJsonText);
|
||||
string arrayJsonText = SerializeXmlNode(arrayDoc);
|
||||
string expected = @"{
|
||||
""root"": {
|
||||
""person"": {
|
||||
""@id"": ""1"",
|
||||
""name"": ""Alan"",
|
||||
""url"": ""http://www.google.com"",
|
||||
""role"": [
|
||||
""Admin""
|
||||
]
|
||||
}
|
||||
}
|
||||
}";
|
||||
Assert.AreEqual(expected, arrayJsonText);
|
||||
|
||||
arrayXml = @"<root xmlns:json=""http://james.newtonking.com/projects/json"">
|
||||
<person id=""1"">
|
||||
@@ -399,8 +554,21 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
arrayDoc = new XmlDocument();
|
||||
arrayDoc.LoadXml(arrayXml);
|
||||
|
||||
arrayJsonText = JsonConvert.SerializeXmlNode(arrayDoc);
|
||||
Assert.AreEqual(@"{""root"":{""person"":{""@id"":""1"",""name"":""Alan"",""url"":""http://www.google.com"",""role"":[""Admin1"",""Admin2""]}}}", arrayJsonText);
|
||||
arrayJsonText = SerializeXmlNode(arrayDoc);
|
||||
expected = @"{
|
||||
""root"": {
|
||||
""person"": {
|
||||
""@id"": ""1"",
|
||||
""name"": ""Alan"",
|
||||
""url"": ""http://www.google.com"",
|
||||
""role"": [
|
||||
""Admin1"",
|
||||
""Admin2""
|
||||
]
|
||||
}
|
||||
}
|
||||
}";
|
||||
Assert.AreEqual(expected, arrayJsonText);
|
||||
|
||||
arrayXml = @"<root xmlns:json=""http://james.newtonking.com/projects/json"">
|
||||
<person id=""1"">
|
||||
@@ -413,58 +581,54 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
arrayDoc = new XmlDocument();
|
||||
arrayDoc.LoadXml(arrayXml);
|
||||
|
||||
arrayJsonText = JsonConvert.SerializeXmlNode(arrayDoc);
|
||||
Assert.AreEqual(@"{""root"":{""person"":{""@id"":""1"",""name"":""Alan"",""url"":""http://www.google.com"",""role"":""Admin1""}}}", arrayJsonText);
|
||||
arrayJsonText = SerializeXmlNode(arrayDoc);
|
||||
expected = @"{
|
||||
""root"": {
|
||||
""person"": {
|
||||
""@id"": ""1"",
|
||||
""name"": ""Alan"",
|
||||
""url"": ""http://www.google.com"",
|
||||
""role"": ""Admin1""
|
||||
}
|
||||
}
|
||||
}";
|
||||
Assert.AreEqual(expected, arrayJsonText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.")]
|
||||
public void MultipleRootProperties()
|
||||
public void MultipleRootPropertiesXmlDocument()
|
||||
{
|
||||
string strJSON = @"{
|
||||
""count"": 773840,""photos"": [
|
||||
{
|
||||
""photo_id"": 532693,
|
||||
""photo_title"": ""Wheatfield in afternoon light"",
|
||||
""photo_url"": ""http://www.panoramio.com/photo/532693"",
|
||||
""photofileurl"": ""http://static2.bareka.com/photos/medium/532693.jpg"",
|
||||
""longitude"": 11.280727,
|
||||
""latitude"": 59.643198,
|
||||
""width"": 500,
|
||||
""height"": 333,
|
||||
""upload_date"": ""22 January 2007"",
|
||||
""owner_id"": 39160,
|
||||
""owner_name"": ""Snemann"",
|
||||
""owner_url"": ""http://www.panoramio.com/user/39160"",
|
||||
},
|
||||
{
|
||||
""photo_id"": 505229,
|
||||
""photo_title"": ""Etangs près de Dijon"",
|
||||
""photo_url"": ""http://www.panoramio.com/photo/505229"",
|
||||
""photofileurl"": ""http://static2.bareka.com/photos/medium/505229.jpg"",
|
||||
""longitude"": 5.168552,
|
||||
""latitude"": 47.312642,
|
||||
""width"": 350,
|
||||
""height"": 500,
|
||||
""upload_date"": ""20 January 2007"",
|
||||
""owner_id"": 78506,
|
||||
""owner_name"": ""Philippe Stoop"",
|
||||
""owner_url"": ""http://www.panoramio.com/user/78506""
|
||||
}
|
||||
]
|
||||
}";
|
||||
string json = @"{""count"": 773840,""photos"": null}";
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(strJSON);
|
||||
JsonConvert.DeserializeXmlNode(json);
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.")]
|
||||
public void MultipleRootPropertiesXDocument()
|
||||
{
|
||||
string json = @"{""count"": 773840,""photos"": null}";
|
||||
|
||||
JsonConvert.DeserializeXNode(json);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void MultipleRootPropertiesAddRootElement()
|
||||
{
|
||||
string strJSON = @"{""count"": 773840,""photos"": 773840}";
|
||||
string json = @"{""count"": 773840,""photos"": 773840}";
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(strJSON, "myRoot");
|
||||
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
|
||||
|
||||
Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newDoc.InnerXml);
|
||||
|
||||
#if !NET20
|
||||
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
|
||||
|
||||
Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newXDoc.ToString(SaveOptions.DisableFormatting));
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -483,9 +647,15 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]
|
||||
}";
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "myRoot");
|
||||
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
|
||||
|
||||
Assert.AreEqual(@"<myRoot><available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes></available_sizes><available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes></available_sizes></myRoot>", newDoc.InnerXml);
|
||||
|
||||
#if !NET20
|
||||
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
|
||||
|
||||
Assert.AreEqual(@"<myRoot><available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes></available_sizes><available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes></available_sizes></myRoot>", newXDoc.ToString(SaveOptions.DisableFormatting));
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -508,9 +678,15 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
]
|
||||
}";
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "myRoot");
|
||||
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
|
||||
|
||||
Assert.AreEqual(@"<myRoot><available_sizes><available_sizes><available_sizes>113</available_sizes><available_sizes>150</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>189</available_sizes><available_sizes>250</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>341</available_sizes><available_sizes>450</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-450x450.jpg</available_sizes></available_sizes></myRoot>", newDoc.InnerXml);
|
||||
|
||||
#if !NET20
|
||||
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
|
||||
|
||||
Assert.AreEqual(@"<myRoot><available_sizes><available_sizes><available_sizes>113</available_sizes><available_sizes>150</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>189</available_sizes><available_sizes>250</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>341</available_sizes><available_sizes>450</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-450x450.jpg</available_sizes></available_sizes></myRoot>", newXDoc.ToString(SaveOptions.DisableFormatting));
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -520,8 +696,10 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
doc.LoadXml(@"<name>O""Connor</name>"); // i use "" so it will be easier to see the problem
|
||||
|
||||
string json = JsonConvert.SerializeXmlNode(doc);
|
||||
Assert.AreEqual(@"{""name"":""O\""Connor""}", json);
|
||||
string json = SerializeXmlNode(doc);
|
||||
Assert.AreEqual(@"{
|
||||
""name"": ""O\""Connor""
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -533,7 +711,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
string jsonText = JsonConvert.SerializeObject(doc, Formatting.Indented, new XmlNodeConverter());
|
||||
string jsonText = SerializeXmlNode(doc);
|
||||
|
||||
string expected = @"{
|
||||
""span"": {
|
||||
@@ -548,7 +726,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
Assert.AreEqual(expected, jsonText);
|
||||
|
||||
XmlDocument newDoc = JsonConvert.DeserializeObject<XmlDocument>(jsonText, new XmlNodeConverter());
|
||||
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
Assert.AreEqual(@"<span class=""vevent""><a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! --></span>", newDoc.InnerXml);
|
||||
}
|
||||
|
||||
@@ -570,7 +748,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(xml);
|
||||
|
||||
string jsonText = JsonConvert.SerializeXmlNode(doc);
|
||||
string jsonText = SerializeXmlNode(doc);
|
||||
// {
|
||||
// "?xml": {
|
||||
// "@version": "1.0",
|
||||
@@ -616,7 +794,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
}", jsonText);
|
||||
|
||||
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText);
|
||||
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
|
||||
|
||||
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
|
||||
}
|
||||
@@ -645,7 +823,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
}";
|
||||
|
||||
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
|
||||
XmlDocument doc = (XmlDocument)DeserializeXmlNode(json);
|
||||
// <?xml version="1.0" standalone="no"?>
|
||||
// <root>
|
||||
// <person id="1">
|
||||
|
||||
@@ -48,6 +48,9 @@ using System.Runtime.Serialization;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Reflection;
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
using System.Xml.Linq;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
@@ -2715,12 +2718,14 @@ keyword such as type of business.""
|
||||
json = JsonConvert.SerializeObject(myB, Formatting.Indented);
|
||||
Assert.AreEqual(@"{
|
||||
""BB_field1"": 4,
|
||||
""BB_field2"": 4,
|
||||
""AA_field1"": 3,
|
||||
""BB_property1"": 4,
|
||||
""BB_property2"": 4,
|
||||
""BB_property3"": 4,
|
||||
""BB_property4"": 4,
|
||||
""BB_property5"": 4,
|
||||
""BB_property7"": 4,
|
||||
""AA_property1"": 3,
|
||||
""AA_property2"": 3,
|
||||
""AA_property3"": 3,
|
||||
@@ -2734,6 +2739,7 @@ keyword such as type of business.""
|
||||
AA myA = JsonConvert.DeserializeObject<AA>(
|
||||
@"{
|
||||
""AA_field1"": 2,
|
||||
""AA_field2"": 2,
|
||||
""AA_property1"": 2,
|
||||
""AA_property2"": 2,
|
||||
""AA_property3"": 2,
|
||||
@@ -2743,6 +2749,7 @@ keyword such as type of business.""
|
||||
}");
|
||||
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field1", BindingFlags.Instance | BindingFlags.NonPublic), myA));
|
||||
Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field2", BindingFlags.Instance | BindingFlags.NonPublic), myA));
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property1", BindingFlags.Instance | BindingFlags.NonPublic), myA));
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property2", BindingFlags.Instance | BindingFlags.NonPublic), myA));
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property3", BindingFlags.Instance | BindingFlags.NonPublic), myA));
|
||||
@@ -2753,7 +2760,9 @@ keyword such as type of business.""
|
||||
BB myB = JsonConvert.DeserializeObject<BB>(
|
||||
@"{
|
||||
""BB_field1"": 4,
|
||||
""BB_field2"": 4,
|
||||
""AA_field1"": 3,
|
||||
""AA_field2"": 3,
|
||||
""AA_property1"": 2,
|
||||
""AA_property2"": 2,
|
||||
""AA_property3"": 2,
|
||||
@@ -2765,10 +2774,13 @@ keyword such as type of business.""
|
||||
""BB_property3"": 3,
|
||||
""BB_property4"": 3,
|
||||
""BB_property5"": 3,
|
||||
""BB_property6"": 3
|
||||
""BB_property6"": 3,
|
||||
""BB_property7"": 3,
|
||||
""BB_property8"": 3
|
||||
}");
|
||||
|
||||
Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field1", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetField("AA_field2", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property1", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property2", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
Assert.AreEqual(2, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property3", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
@@ -2777,18 +2789,22 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(0, ReflectionUtils.GetMemberValue(typeof(AA).GetProperty("AA_property6", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
|
||||
Assert.AreEqual(4, myB.BB_field1);
|
||||
Assert.AreEqual(4, myB.BB_field2);
|
||||
Assert.AreEqual(3, myB.BB_property1);
|
||||
Assert.AreEqual(3, myB.BB_property2);
|
||||
Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property3", BindingFlags.Instance | BindingFlags.Public), myB));
|
||||
Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property4", BindingFlags.Instance | BindingFlags.NonPublic), myB));
|
||||
Assert.AreEqual(0, myB.BB_property5);
|
||||
Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property6", BindingFlags.Instance | BindingFlags.Public), myB));
|
||||
Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property7", BindingFlags.Instance | BindingFlags.Public), myB));
|
||||
Assert.AreEqual(3, ReflectionUtils.GetMemberValue(typeof(BB).GetProperty("BB_property8", BindingFlags.Instance | BindingFlags.Public), myB));
|
||||
}
|
||||
|
||||
public class AA
|
||||
{
|
||||
[JsonProperty]
|
||||
protected int AA_field1;
|
||||
protected int AA_field2;
|
||||
[JsonProperty]
|
||||
protected int AA_property1 { get; set; }
|
||||
[JsonProperty]
|
||||
@@ -2807,6 +2823,7 @@ keyword such as type of business.""
|
||||
public AA(int f)
|
||||
{
|
||||
AA_field1 = f;
|
||||
AA_field2 = f;
|
||||
AA_property1 = f;
|
||||
AA_property2 = f;
|
||||
AA_property3 = f;
|
||||
@@ -2820,6 +2837,7 @@ keyword such as type of business.""
|
||||
{
|
||||
[JsonProperty]
|
||||
public int BB_field1;
|
||||
public int BB_field2;
|
||||
[JsonProperty]
|
||||
public int BB_property1 { get; set; }
|
||||
[JsonProperty]
|
||||
@@ -2830,6 +2848,9 @@ keyword such as type of business.""
|
||||
private int BB_property4 { get; set; }
|
||||
public int BB_property5 { get; private set; }
|
||||
public int BB_property6 { private get; set; }
|
||||
[JsonProperty]
|
||||
public int BB_property7 { protected get; set; }
|
||||
public int BB_property8 { protected get; set; }
|
||||
|
||||
public BB()
|
||||
{
|
||||
@@ -2839,13 +2860,84 @@ keyword such as type of business.""
|
||||
: base(f)
|
||||
{
|
||||
BB_field1 = g;
|
||||
BB_field2 = g;
|
||||
BB_property1 = g;
|
||||
BB_property2 = g;
|
||||
BB_property3 = g;
|
||||
BB_property4 = g;
|
||||
BB_property5 = g;
|
||||
BB_property6 = g;
|
||||
BB_property7 = g;
|
||||
BB_property8 = g;
|
||||
}
|
||||
}
|
||||
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
public class XNodeTestObject
|
||||
{
|
||||
public XDocument Document { get; set; }
|
||||
public XElement Element { get; set; }
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SILVERLIGHT
|
||||
public class XmlNodeTestObject
|
||||
{
|
||||
public XmlDocument Document { get; set; }
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
[Test]
|
||||
public void SerializeDeserializeXNodeProperties()
|
||||
{
|
||||
XNodeTestObject testObject = new XNodeTestObject();
|
||||
testObject.Document = XDocument.Parse("<root>hehe, root</root>");
|
||||
testObject.Element = XElement.Parse(@"<fifth xmlns:json=""http://json.org"" json:Awesome=""true"">element</fifth>");
|
||||
|
||||
string json = JsonConvert.SerializeObject(testObject, Formatting.Indented);
|
||||
string expected = @"{
|
||||
""Document"": {
|
||||
""root"": ""hehe, root""
|
||||
},
|
||||
""Element"": {
|
||||
""fifth"": {
|
||||
""@xmlns:json"": ""http://json.org"",
|
||||
""@json:Awesome"": ""true"",
|
||||
""#text"": ""element""
|
||||
}
|
||||
}
|
||||
}";
|
||||
Assert.AreEqual(expected, json);
|
||||
|
||||
XNodeTestObject newTestObject = JsonConvert.DeserializeObject<XNodeTestObject>(json);
|
||||
Assert.AreEqual(testObject.Document.ToString(), newTestObject.Document.ToString());
|
||||
Assert.AreEqual(testObject.Element.ToString(), newTestObject.Element.ToString());
|
||||
|
||||
Assert.IsNull(newTestObject.Element.Parent);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SILVERLIGHT
|
||||
[Test]
|
||||
public void SerializeDeserializeXmlNodeProperties()
|
||||
{
|
||||
XmlNodeTestObject testObject = new XmlNodeTestObject();
|
||||
XmlDocument document = new XmlDocument();
|
||||
document.LoadXml("<root>hehe, root</root>");
|
||||
testObject.Document = document;
|
||||
|
||||
string json = JsonConvert.SerializeObject(testObject, Formatting.Indented);
|
||||
string expected = @"{
|
||||
""Document"": {
|
||||
""root"": ""hehe, root""
|
||||
}
|
||||
}";
|
||||
Assert.AreEqual(expected, json);
|
||||
|
||||
XmlNodeTestObject newTestObject = JsonConvert.DeserializeObject<XmlNodeTestObject>(json);
|
||||
Assert.AreEqual(testObject.Document.InnerXml, newTestObject.Document.InnerXml);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -30,6 +30,9 @@ using Newtonsoft.Json.Utilities;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using System.Text;
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
using System.Xml.Linq;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json
|
||||
{
|
||||
@@ -735,6 +738,20 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
#if !NET20 && !SILVERLIGHT
|
||||
public static string SerializeXNode(XObject node)
|
||||
{
|
||||
return SerializeXNode(node, Formatting.None);
|
||||
}
|
||||
|
||||
public static string SerializeXNode(XObject node, Formatting formatting)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
|
||||
return SerializeObject(node, formatting, converter);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SILVERLIGHT
|
||||
/// <summary>
|
||||
/// Serializes the XML node to a JSON string.
|
||||
@@ -742,10 +759,21 @@ namespace Newtonsoft.Json
|
||||
/// <param name="node">The node to serialize.</param>
|
||||
/// <returns>A JSON string of the XmlNode.</returns>
|
||||
public static string SerializeXmlNode(XmlNode node)
|
||||
{
|
||||
return SerializeXmlNode(node, Formatting.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the XML node to a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to serialize.</param>
|
||||
/// <param name="formatting">Indicates how the output is formatted.</param>
|
||||
/// <returns>A JSON string of the XmlNode.</returns>
|
||||
public static string SerializeXmlNode(XmlNode node, Formatting formatting)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
|
||||
return SerializeObject(node, converter);
|
||||
return SerializeObject(node, formatting, converter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -753,7 +781,7 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <returns>The deserialized XmlNode</returns>
|
||||
public static XmlNode DeserializeXmlNode(string value)
|
||||
public static XmlDocument DeserializeXmlNode(string value)
|
||||
{
|
||||
return DeserializeXmlNode(value, null);
|
||||
}
|
||||
@@ -764,13 +792,39 @@ namespace Newtonsoft.Json
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <param name="deserializeRootElementName">The name of the root element to append when deserializing.</param>
|
||||
/// <returns>The deserialized XmlNode</returns>
|
||||
public static XmlNode DeserializeXmlNode(string value, string deserializeRootElementName)
|
||||
public static XmlDocument DeserializeXmlNode(string value, string deserializeRootElementName)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
converter.DeserializeRootElementName = deserializeRootElementName;
|
||||
|
||||
return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter);
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
/// <summary>
|
||||
/// Deserializes the XNode from a JSON string.
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <returns>The deserialized XmlNode</returns>
|
||||
public static XDocument DeserializeXNode(string value)
|
||||
{
|
||||
return DeserializeXNode(value, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the XmlNode from a JSON string nested in a root elment.
|
||||
/// </summary>
|
||||
/// <param name="value">The JSON string.</param>
|
||||
/// <param name="deserializeRootElementName">The name of the root element to append when deserializing.</param>
|
||||
/// <returns>The deserialized XmlNode</returns>
|
||||
public static XDocument DeserializeXNode(string value, string deserializeRootElementName)
|
||||
{
|
||||
XmlNodeConverter converter = new XmlNodeConverter();
|
||||
converter.DeserializeRootElementName = deserializeRootElementName;
|
||||
|
||||
return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,9 @@
|
||||
</Reference>
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xml.Linq">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonBinaryType.cs" />
|
||||
|
||||
@@ -50,6 +50,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
#endif
|
||||
new BinaryConverter(),
|
||||
#if !SILVERLIGHT
|
||||
new XmlNodeConverter(),
|
||||
new DataSetConverter(),
|
||||
new DataTableConverter()
|
||||
#endif
|
||||
|
||||
@@ -98,5 +98,39 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetPrefix(string qualifiedName)
|
||||
{
|
||||
string prefix;
|
||||
string localName;
|
||||
GetQualifiedNameParts(qualifiedName, out prefix, out localName);
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public static string GetLocalName(string qualifiedName)
|
||||
{
|
||||
string prefix;
|
||||
string localName;
|
||||
GetQualifiedNameParts(qualifiedName, out prefix, out localName);
|
||||
|
||||
return localName;
|
||||
}
|
||||
|
||||
public static void GetQualifiedNameParts(string qualifiedName, out string prefix, out string localName)
|
||||
{
|
||||
int colonPosition = qualifiedName.IndexOf(':');
|
||||
|
||||
if ((colonPosition == -1 || colonPosition == 0) || (qualifiedName.Length - 1) == colonPosition)
|
||||
{
|
||||
prefix = null;
|
||||
localName = qualifiedName;
|
||||
}
|
||||
else
|
||||
{
|
||||
prefix = qualifiedName.Substring(0, colonPosition);
|
||||
localName = qualifiedName.Substring(colonPosition + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user