This commit is contained in:
James Newton-King
2016-09-10 18:41:30 +12:00
parent ac2d619474
commit 216ff4ab8b
@@ -23,13 +23,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#endregion #endregion
using System.Globalization;
#if NET20 #if NET20
using Newtonsoft.Json.Utilities.LinqBridge; using Newtonsoft.Json.Utilities.LinqBridge;
#else #else
using System.Linq; using System.Linq;
#endif #endif
using System.Text; using System.Text;
#if !(NETFX_CORE || PORTABLE || DNXCORE50 || PORTABLE40) #if !(NETFX_CORE || DNXCORE50 || PORTABLE40)
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Newtonsoft.Json.Tests.Serialization; using Newtonsoft.Json.Tests.Serialization;
@@ -61,6 +62,7 @@ namespace Newtonsoft.Json.Tests.Converters
[TestFixture] [TestFixture]
public class XmlNodeConverterTest : TestFixtureBase public class XmlNodeConverterTest : TestFixtureBase
{ {
#if !PORTABLE
private string SerializeXmlNode(XmlNode node) private string SerializeXmlNode(XmlNode node)
{ {
string json = JsonConvert.SerializeXmlNode(node, Formatting.Indented); string json = JsonConvert.SerializeXmlNode(node, Formatting.Indented);
@@ -128,6 +130,24 @@ namespace Newtonsoft.Json.Tests.Converters
return node; return node;
} }
#endif
private string IndentXml(string xml)
{
XmlReader reader = XmlReader.Create(new StringReader(xml));
StringWriter sw = new StringWriter();
XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
while (reader.Read())
{
writer.WriteNode(reader, false);
}
writer.Flush();
return sw.ToString();
}
#if !NET20 #if !NET20
[Test] [Test]
@@ -228,6 +248,7 @@ namespace Newtonsoft.Json.Tests.Converters
} }
#endif #endif
#if !PORTABLE
[Test] [Test]
public void MultipleNamespacesXmlDocument() public void MultipleNamespacesXmlDocument()
{ {
@@ -293,6 +314,7 @@ namespace Newtonsoft.Json.Tests.Converters
} }
]", json); ]", json);
} }
#endif
#if !NET20 #if !NET20
[Test] [Test]
@@ -405,6 +427,7 @@ namespace Newtonsoft.Json.Tests.Converters
</root>", doc.ToString()); </root>", doc.ToString());
} }
#if !PORTABLE
[Test] [Test]
public void SerializeEmptyDocument() public void SerializeEmptyDocument()
{ {
@@ -430,6 +453,7 @@ namespace Newtonsoft.Json.Tests.Converters
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true); json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
Assert.AreEqual(@"""""", json); Assert.AreEqual(@"""""", json);
} }
#endif
[Test] [Test]
public void SerializeAndDeserializeXmlWithNamespaceInChildrenAndNoValueInChildren() public void SerializeAndDeserializeXmlWithNamespaceInChildrenAndNoValueInChildren()
@@ -449,6 +473,7 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.IsTrue(equals); Assert.IsTrue(equals);
} }
#if !PORTABLE
[Test] [Test]
public void DeserializeUndeclaredNamespacePrefix() public void DeserializeUndeclaredNamespacePrefix()
{ {
@@ -461,7 +486,9 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(doc.OuterXml, xdoc.ToString()); Assert.AreEqual(doc.OuterXml, xdoc.ToString());
} }
#endif #endif
#endif
#if !PORTABLE
[Test] [Test]
public void DeserializeMultipleRootElements() public void DeserializeMultipleRootElements()
{ {
@@ -1150,6 +1177,7 @@ namespace Newtonsoft.Json.Tests.Converters
() => { JsonConvert.DeserializeXmlNode(json); }, () => { JsonConvert.DeserializeXmlNode(json); },
"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. Path 'photos', line 1, position 26."); "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. Path 'photos', line 1, position 26.");
} }
#endif
#if !NET20 #if !NET20
[Test] [Test]
@@ -1168,9 +1196,11 @@ namespace Newtonsoft.Json.Tests.Converters
{ {
string json = @"{""count"": 773840,""photos"": 773840}"; string json = @"{""count"": 773840,""photos"": 773840}";
#if !PORTABLE
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot"); XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newDoc.InnerXml); Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newDoc.InnerXml);
#endif
#if !NET20 #if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot"); XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
@@ -1198,7 +1228,8 @@ namespace Newtonsoft.Json.Tests.Converters
] ]
}"; }";
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot"); #if !PORTABLE
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
string xml = IndentXml(newDoc.InnerXml); string xml = IndentXml(newDoc.InnerXml);
@@ -1215,6 +1246,7 @@ namespace Newtonsoft.Json.Tests.Converters
<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> </available_sizes>
</myRoot>", IndentXml(newDoc.InnerXml)); </myRoot>", IndentXml(newDoc.InnerXml));
#endif
#if !NET20 #if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot"); XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
@@ -1234,8 +1266,10 @@ namespace Newtonsoft.Json.Tests.Converters
</myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting))); </myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
#endif #endif
#if !PORTABLE
string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented); string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented);
Console.WriteLine(newJson); Console.WriteLine(newJson);
#endif
} }
[Test] [Test]
@@ -1257,6 +1291,7 @@ namespace Newtonsoft.Json.Tests.Converters
] ]
}"; }";
#if !PORTABLE
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot", true); XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot", true);
StringAssert.AreEqual(@"<myRoot> StringAssert.AreEqual(@"<myRoot>
@@ -1272,6 +1307,7 @@ namespace Newtonsoft.Json.Tests.Converters
<available_sizes json:Array=""true"">assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes> <available_sizes json:Array=""true"">assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
</available_sizes> </available_sizes>
</myRoot>", IndentXml(newDoc.InnerXml)); </myRoot>", IndentXml(newDoc.InnerXml));
#endif
#if !NET20 #if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot", true); XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot", true);
@@ -1291,8 +1327,10 @@ namespace Newtonsoft.Json.Tests.Converters
</myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting))); </myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
#endif #endif
#if !PORTABLE
string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented, true); string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented, true);
StringAssert.AreEqual(json, newJson); StringAssert.AreEqual(json, newJson);
#endif
} }
[Test] [Test]
@@ -1315,9 +1353,11 @@ namespace Newtonsoft.Json.Tests.Converters
] ]
}"; }";
#if !PORTABLE
XmlDocument newDoc = 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); 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);
#endif
#if !NET20 #if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot"); XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
@@ -1326,6 +1366,7 @@ namespace Newtonsoft.Json.Tests.Converters
#endif #endif
} }
#if !PORTABLE
[Test] [Test]
public void Encoding() public void Encoding()
{ {
@@ -1338,7 +1379,9 @@ namespace Newtonsoft.Json.Tests.Converters
""name"": ""O\""Connor"" ""name"": ""O\""Connor""
}", json); }", json);
} }
#endif
#if !PORTABLE
[Test] [Test]
public void SerializeComment() public void SerializeComment()
{ {
@@ -1895,23 +1938,6 @@ namespace Newtonsoft.Json.Tests.Converters
}", output2); }", output2);
} }
private string IndentXml(string xml)
{
XmlReader reader = XmlReader.Create(new StringReader(xml));
StringWriter sw = new StringWriter();
XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
while (reader.Read())
{
writer.WriteNode(reader, false);
}
writer.Flush();
return sw.ToString();
}
[Test] [Test]
public void OmitRootObject() public void OmitRootObject()
{ {
@@ -2062,6 +2088,7 @@ namespace Newtonsoft.Json.Tests.Converters
} }
} }
} }
#endif
#if !NET20 #if !NET20
[Test] [Test]
@@ -2302,6 +2329,7 @@ namespace Newtonsoft.Json.Tests.Converters
} }
#endif #endif
#if !PORTABLE
[Test] [Test]
public void DeserializeXmlNodeDefaultNamespace() public void DeserializeXmlNodeDefaultNamespace()
{ {
@@ -2560,6 +2588,7 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(@"<item action=""update"" itemid=""1"" description=""temp""><elements action=""none"" id=""2"" /><elements action=""none"" id=""3"" /></item>", xmldoc.InnerXml); Assert.AreEqual(@"<item action=""update"" itemid=""1"" description=""temp""><elements action=""none"" id=""2"" /><elements action=""none"" id=""3"" /></item>", xmldoc.InnerXml);
} }
#endif
[Test] [Test]
public void SerializingXmlNamespaceScope() public void SerializingXmlNamespaceScope()
@@ -2577,7 +2606,7 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(@"{""root"":{""@xmlns"":""http://www.example.com/ns"",""a"":null,""bns:b"":{""@xmlns:bns"":""http://www.example.com/ns""},""c"":null}}", json1); Assert.AreEqual(@"{""root"":{""@xmlns"":""http://www.example.com/ns"",""a"":null,""bns:b"":{""@xmlns:bns"":""http://www.example.com/ns""},""c"":null}}", json1);
#endif #endif
#if !(NETFX_CORE) #if !(PORTABLE)
var xml1 = new XmlDocument(); var xml1 = new XmlDocument();
xml1.LoadXml(xmlString); xml1.LoadXml(xmlString);
@@ -2628,6 +2657,7 @@ namespace Newtonsoft.Json.Tests.Converters
} }
#endif #endif
#if !PORTABLE
[Test] [Test]
public void SerializeAndDeserializeXmlElementWithNamespaceInChildrenRootDontHaveNameSpace() public void SerializeAndDeserializeXmlElementWithNamespaceInChildrenRootDontHaveNameSpace()
{ {
@@ -2687,8 +2717,41 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual("null", json); Assert.AreEqual("null", json);
} }
#endif
#if !NET20 #if !NET20
[Test]
public void DateTimeToXml_Unspecified()
{
string json = @"{""CreatedDate"": ""2014-01-23T00:00:00""}";
var dxml = JsonConvert.DeserializeXNode(json, "root");
Assert.AreEqual("2014-01-23T00:00:00", dxml.Root.Element("CreatedDate").Value);
Console.WriteLine("DateTimeToXml_Unspecified: " + dxml.Root.Element("CreatedDate").Value);
}
[Test]
public void DateTimeToXml_Utc()
{
string json = @"{""CreatedDate"": ""2014-01-23T00:00:00Z""}";
var dxml = JsonConvert.DeserializeXNode(json, "root");
Assert.AreEqual("2014-01-23T00:00:00Z", dxml.Root.Element("CreatedDate").Value);
Console.WriteLine("DateTimeToXml_Utc: " + dxml.Root.Element("CreatedDate").Value);
}
[Test]
public void DateTimeToXml_Local()
{
DateTime dt = DateTime.Parse("2014-01-23T00:00:00+01:00");
string json = @"{""CreatedDate"": ""2014-01-23T00:00:00+01:00""}";
var dxml = JsonConvert.DeserializeXNode(json, "root");
Assert.AreEqual(dt.ToString("yyyy-MM-ddTHH:mm:sszzzzzzz", CultureInfo.InvariantCulture), dxml.Root.Element("CreatedDate").Value);
Console.WriteLine("DateTimeToXml_Local: " + dxml.Root.Element("CreatedDate").Value);
}
[Test] [Test]
public void SerializeEmptyNodeAndOmitRoot_XElement() public void SerializeEmptyNodeAndOmitRoot_XElement()
{ {