Merge branch 'master' into wojtpl2-master

This commit is contained in:
James Newton-King
2016-09-10 18:41:51 +12:00
@@ -23,13 +23,14 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System.Globalization;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Text;
#if !(NETFX_CORE || PORTABLE || DNXCORE50 || PORTABLE40)
#if !(NETFX_CORE || DNXCORE50 || PORTABLE40)
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Tests.Serialization;
@@ -61,6 +62,7 @@ namespace Newtonsoft.Json.Tests.Converters
[TestFixture]
public class XmlNodeConverterTest : TestFixtureBase
{
#if !PORTABLE
private string SerializeXmlNode(XmlNode node)
{
string json = JsonConvert.SerializeXmlNode(node, Formatting.Indented);
@@ -128,6 +130,24 @@ namespace Newtonsoft.Json.Tests.Converters
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
[Test]
@@ -228,6 +248,7 @@ namespace Newtonsoft.Json.Tests.Converters
}
#endif
#if !PORTABLE
[Test]
public void MultipleNamespacesXmlDocument()
{
@@ -293,6 +314,7 @@ namespace Newtonsoft.Json.Tests.Converters
}
]", json);
}
#endif
#if !NET20
[Test]
@@ -405,6 +427,7 @@ namespace Newtonsoft.Json.Tests.Converters
</root>", doc.ToString());
}
#if !PORTABLE
[Test]
public void SerializeEmptyDocument()
{
@@ -430,6 +453,7 @@ namespace Newtonsoft.Json.Tests.Converters
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
Assert.AreEqual(@"""""", json);
}
#endif
[Test]
public void SerializeAndDeserializeXmlWithNamespaceInChildrenAndNoValueInChildren()
@@ -449,6 +473,7 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.IsTrue(equals);
}
#if !PORTABLE
[Test]
public void DeserializeUndeclaredNamespacePrefix()
{
@@ -461,7 +486,9 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(doc.OuterXml, xdoc.ToString());
}
#endif
#endif
#if !PORTABLE
[Test]
public void DeserializeMultipleRootElements()
{
@@ -1150,6 +1177,7 @@ namespace Newtonsoft.Json.Tests.Converters
() => { 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.");
}
#endif
#if !NET20
[Test]
@@ -1168,9 +1196,11 @@ namespace Newtonsoft.Json.Tests.Converters
{
string json = @"{""count"": 773840,""photos"": 773840}";
#if !PORTABLE
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newDoc.InnerXml);
#endif
#if !NET20
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);
@@ -1215,6 +1246,7 @@ namespace Newtonsoft.Json.Tests.Converters
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
</available_sizes>
</myRoot>", IndentXml(newDoc.InnerXml));
#endif
#if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
@@ -1234,8 +1266,10 @@ namespace Newtonsoft.Json.Tests.Converters
</myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
#endif
#if !PORTABLE
string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented);
Console.WriteLine(newJson);
#endif
}
[Test]
@@ -1257,6 +1291,7 @@ namespace Newtonsoft.Json.Tests.Converters
]
}";
#if !PORTABLE
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot", true);
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>
</myRoot>", IndentXml(newDoc.InnerXml));
#endif
#if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot", true);
@@ -1291,8 +1327,10 @@ namespace Newtonsoft.Json.Tests.Converters
</myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
#endif
#if !PORTABLE
string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented, true);
StringAssert.AreEqual(json, newJson);
#endif
}
[Test]
@@ -1315,9 +1353,11 @@ namespace Newtonsoft.Json.Tests.Converters
]
}";
#if !PORTABLE
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);
#endif
#if !NET20
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
@@ -1326,6 +1366,7 @@ namespace Newtonsoft.Json.Tests.Converters
#endif
}
#if !PORTABLE
[Test]
public void Encoding()
{
@@ -1338,7 +1379,9 @@ namespace Newtonsoft.Json.Tests.Converters
""name"": ""O\""Connor""
}", json);
}
#endif
#if !PORTABLE
[Test]
public void SerializeComment()
{
@@ -1895,23 +1938,6 @@ namespace Newtonsoft.Json.Tests.Converters
}", 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]
public void OmitRootObject()
{
@@ -2062,6 +2088,7 @@ namespace Newtonsoft.Json.Tests.Converters
}
}
}
#endif
#if !NET20
[Test]
@@ -2302,6 +2329,7 @@ namespace Newtonsoft.Json.Tests.Converters
}
#endif
#if !PORTABLE
[Test]
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);
}
#endif
[Test]
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);
#endif
#if !(NETFX_CORE)
#if !(PORTABLE)
var xml1 = new XmlDocument();
xml1.LoadXml(xmlString);
@@ -2628,6 +2657,7 @@ namespace Newtonsoft.Json.Tests.Converters
}
#endif
#if !PORTABLE
[Test]
public void SerializeAndDeserializeXmlElementWithNamespaceInChildrenRootDontHaveNameSpace()
{
@@ -2687,8 +2717,41 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual("null", json);
}
#endif
#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]
public void SerializeEmptyNodeAndOmitRoot_XElement()
{