-Changed blank XML elements to be written as an empty string instead of null
-Added support for XML document type -Changed string JValue with a null value to be written as null instead of empty string -Fixed StringEnumConverter thread safety
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Text;
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -144,8 +145,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
doc.LoadXml("<root></root>");
|
||||
|
||||
json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
|
||||
Assert.AreEqual("null", json);
|
||||
|
||||
Assert.AreEqual(@"""""", json);
|
||||
|
||||
XDocument doc1 = XDocument.Parse("<root />");
|
||||
|
||||
@@ -155,7 +155,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
doc1 = XDocument.Parse("<root></root>");
|
||||
|
||||
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
|
||||
Assert.AreEqual("null", json);
|
||||
Assert.AreEqual(@"""""", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -345,6 +345,137 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XmlDocumentTypeSerialize()
|
||||
{
|
||||
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?><!DOCTYPE STOCKQUOTE PUBLIC ""-//W3C//DTD StockQuote 1.5//EN"" ""http://www.irxml.org/dtd/stockquote_1.5.dtd""><STOCKQUOTE ROWCOUNT=""2""><RESULT><ROW><ASK>0</ASK><BID>0</BID><CHANGE>-16.310</CHANGE><COMPANYNAME>Dow Jones</COMPANYNAME><DATETIME>2014-04-17 15:50:37</DATETIME><DIVIDEND>0</DIVIDEND><EPS>0</EPS><EXCHANGE></EXCHANGE><HIGH>16460.490</HIGH><LASTDATETIME>2014-04-17 15:50:37</LASTDATETIME><LASTPRICE>16408.540</LASTPRICE><LOW>16368.140</LOW><OPEN>16424.140</OPEN><PCHANGE>-0.099</PCHANGE><PE>0</PE><PREVIOUSCLOSE>16424.850</PREVIOUSCLOSE><SHARES>0</SHARES><TICKER>DJII</TICKER><TRADES>0</TRADES><VOLUME>136188700</VOLUME><YEARHIGH>11309.000</YEARHIGH><YEARLOW>9302.280</YEARLOW><YIELD>0</YIELD></ROW><ROW><ASK>0</ASK><BID>0</BID><CHANGE>9.290</CHANGE><COMPANYNAME>NASDAQ</COMPANYNAME><DATETIME>2014-04-17 15:40:01</DATETIME><DIVIDEND>0</DIVIDEND><EPS>0</EPS><EXCHANGE></EXCHANGE><HIGH>4110.460</HIGH><LASTDATETIME>2014-04-17 15:40:01</LASTDATETIME><LASTPRICE>4095.520</LASTPRICE><LOW>4064.700</LOW><OPEN>4080.300</OPEN><PCHANGE>0.227</PCHANGE><PE>0</PE><PREVIOUSCLOSE>4086.230</PREVIOUSCLOSE><SHARES>0</SHARES><TICKER>COMP</TICKER><TRADES>0</TRADES><VOLUME>1784210100</VOLUME><YEARHIGH>4371.710</YEARHIGH><YEARLOW>3154.960</YEARLOW><YIELD>0</YIELD></ROW></RESULT><STATUS>Couldn't find ticker: SPIC?</STATUS><STATUSCODE>2</STATUSCODE></STOCKQUOTE>";
|
||||
|
||||
string expected = @"{
|
||||
""?xml"": {
|
||||
""@version"": ""1.0"",
|
||||
""@encoding"": ""utf-8""
|
||||
},
|
||||
""!DOCTYPE"": {
|
||||
""@name"": ""STOCKQUOTE"",
|
||||
""@public"": ""-//W3C//DTD StockQuote 1.5//EN"",
|
||||
""@system"": ""http://www.irxml.org/dtd/stockquote_1.5.dtd""
|
||||
},
|
||||
""STOCKQUOTE"": {
|
||||
""@ROWCOUNT"": ""2"",
|
||||
""RESULT"": {
|
||||
""ROW"": [
|
||||
{
|
||||
""ASK"": ""0"",
|
||||
""BID"": ""0"",
|
||||
""CHANGE"": ""-16.310"",
|
||||
""COMPANYNAME"": ""Dow Jones"",
|
||||
""DATETIME"": ""2014-04-17 15:50:37"",
|
||||
""DIVIDEND"": ""0"",
|
||||
""EPS"": ""0"",
|
||||
""EXCHANGE"": """",
|
||||
""HIGH"": ""16460.490"",
|
||||
""LASTDATETIME"": ""2014-04-17 15:50:37"",
|
||||
""LASTPRICE"": ""16408.540"",
|
||||
""LOW"": ""16368.140"",
|
||||
""OPEN"": ""16424.140"",
|
||||
""PCHANGE"": ""-0.099"",
|
||||
""PE"": ""0"",
|
||||
""PREVIOUSCLOSE"": ""16424.850"",
|
||||
""SHARES"": ""0"",
|
||||
""TICKER"": ""DJII"",
|
||||
""TRADES"": ""0"",
|
||||
""VOLUME"": ""136188700"",
|
||||
""YEARHIGH"": ""11309.000"",
|
||||
""YEARLOW"": ""9302.280"",
|
||||
""YIELD"": ""0""
|
||||
},
|
||||
{
|
||||
""ASK"": ""0"",
|
||||
""BID"": ""0"",
|
||||
""CHANGE"": ""9.290"",
|
||||
""COMPANYNAME"": ""NASDAQ"",
|
||||
""DATETIME"": ""2014-04-17 15:40:01"",
|
||||
""DIVIDEND"": ""0"",
|
||||
""EPS"": ""0"",
|
||||
""EXCHANGE"": """",
|
||||
""HIGH"": ""4110.460"",
|
||||
""LASTDATETIME"": ""2014-04-17 15:40:01"",
|
||||
""LASTPRICE"": ""4095.520"",
|
||||
""LOW"": ""4064.700"",
|
||||
""OPEN"": ""4080.300"",
|
||||
""PCHANGE"": ""0.227"",
|
||||
""PE"": ""0"",
|
||||
""PREVIOUSCLOSE"": ""4086.230"",
|
||||
""SHARES"": ""0"",
|
||||
""TICKER"": ""COMP"",
|
||||
""TRADES"": ""0"",
|
||||
""VOLUME"": ""1784210100"",
|
||||
""YEARHIGH"": ""4371.710"",
|
||||
""YEARLOW"": ""3154.960"",
|
||||
""YIELD"": ""0""
|
||||
}
|
||||
]
|
||||
},
|
||||
""STATUS"": ""Couldn't find ticker: SPIC?"",
|
||||
""STATUSCODE"": ""2""
|
||||
}
|
||||
}";
|
||||
|
||||
XmlDocument doc1 = new XmlDocument();
|
||||
doc1.LoadXml(xml);
|
||||
|
||||
string json1 = JsonConvert.SerializeXmlNode(doc1, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(expected, json1);
|
||||
|
||||
XmlDocument doc11 = JsonConvert.DeserializeXmlNode(json1);
|
||||
|
||||
Assert.AreEqual(xml, ToStringWithDeclaration(doc11));
|
||||
|
||||
#if !NET20
|
||||
XDocument doc2 = XDocument.Parse(xml);
|
||||
|
||||
string json2 = JsonConvert.SerializeXNode(doc2, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(expected, json2);
|
||||
|
||||
XDocument doc22 = JsonConvert.DeserializeXNode(json2);
|
||||
|
||||
Assert.AreEqual(xml, ToStringWithDeclaration(doc22));
|
||||
#endif
|
||||
}
|
||||
|
||||
public class Utf8StringWriter : StringWriter
|
||||
{
|
||||
public override Encoding Encoding { get { return Encoding.UTF8; } }
|
||||
|
||||
public Utf8StringWriter(StringBuilder sb) : base(sb)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
public static string ToStringWithDeclaration(XDocument doc, bool indent = false)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
using (var writer = XmlWriter.Create(new Utf8StringWriter(builder), new XmlWriterSettings { Indent = indent }))
|
||||
{
|
||||
doc.Save(writer);
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
#endif
|
||||
|
||||
public static string ToStringWithDeclaration(XmlDocument doc, bool indent = false)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
using (var writer = XmlWriter.Create(new Utf8StringWriter(builder), new XmlWriterSettings { Indent = indent }))
|
||||
{
|
||||
doc.Save(writer);
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NamespaceSerializeDeserialize()
|
||||
{
|
||||
@@ -1325,12 +1456,23 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
],
|
||||
""C"": [
|
||||
null,
|
||||
null
|
||||
"""",
|
||||
""""
|
||||
]
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
|
||||
XmlDocument d2 = JsonConvert.DeserializeXmlNode(json);
|
||||
|
||||
Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-8""?>
|
||||
<root>
|
||||
<A>
|
||||
<B name=""sample"" />
|
||||
<C></C>
|
||||
<C></C>
|
||||
</A>
|
||||
</root>", ToStringWithDeclaration(d2, true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(0.99, (double)root[5]);
|
||||
Assert.AreEqual(0.000000000000000001d, (double)root[6]);
|
||||
Assert.AreEqual(0.000000000000000001m, (decimal)root[7]);
|
||||
Assert.AreEqual(string.Empty, (string)root[8]);
|
||||
Assert.AreEqual(null, (string)root[8]);
|
||||
Assert.AreEqual("This is a string.", (string)root[9]);
|
||||
Assert.AreEqual(null, ((JValue)root[10]).Value);
|
||||
Assert.AreEqual(null, ((JValue)root[11]).Value);
|
||||
|
||||
@@ -39,9 +39,9 @@
|
||||
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Collections.Immutable, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<Reference Include="System.Collections.Immutable, Version=1.1.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.27\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
|
||||
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.1.20-beta\lib\portable-net45+win8\System.Collections.Immutable.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
|
||||
@@ -75,4 +75,4 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.2.17209")]
|
||||
[assembly: AssemblyFileVersion("6.0.2.17225")]
|
||||
|
||||
@@ -102,42 +102,49 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
#endregion
|
||||
|
||||
// #region Array
|
||||
// [Test]
|
||||
// public void SerializeArray()
|
||||
// {
|
||||
// ImmutableArray<string> l = ImmutableArray.From(new List<string>
|
||||
// {
|
||||
// "One",
|
||||
// "II",
|
||||
// "3"
|
||||
// });
|
||||
#region Array
|
||||
[Test]
|
||||
public void SerializeArray()
|
||||
{
|
||||
ImmutableArray<string> l = ImmutableArray.CreateRange(new List<string>
|
||||
{
|
||||
"One",
|
||||
"II",
|
||||
"3"
|
||||
});
|
||||
|
||||
// string json = JsonConvert.SerializeObject(l, Formatting.Indented);
|
||||
// Assert.AreEqual(@"[
|
||||
// ""One"",
|
||||
// ""II"",
|
||||
// ""3""
|
||||
//]", json);
|
||||
// }
|
||||
string json = JsonConvert.SerializeObject(l, Formatting.Indented);
|
||||
Assert.AreEqual(@"[
|
||||
""One"",
|
||||
""II"",
|
||||
""3""
|
||||
]", json);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public void DeserializeArray()
|
||||
// {
|
||||
// string json = @"[
|
||||
// ""One"",
|
||||
// ""II"",
|
||||
// ""3""
|
||||
//]";
|
||||
[Test]
|
||||
public void DeserializeArray()
|
||||
{
|
||||
string json = @"[
|
||||
""One"",
|
||||
""II"",
|
||||
""3""
|
||||
]";
|
||||
|
||||
// ImmutableArray<string> l = JsonConvert.DeserializeObject<ImmutableArray<string>>(json);
|
||||
ImmutableArray<string> l = JsonConvert.DeserializeObject<ImmutableArray<string>>(json);
|
||||
|
||||
// Assert.AreEqual(3, l.Length);
|
||||
// Assert.AreEqual("One", l[0]);
|
||||
// Assert.AreEqual("II", l[1]);
|
||||
// Assert.AreEqual("3", l[2]);
|
||||
// }
|
||||
// #endregion
|
||||
Assert.AreEqual(3, l.Length);
|
||||
Assert.AreEqual("One", l[0]);
|
||||
Assert.AreEqual("II", l[1]);
|
||||
Assert.AreEqual("3", l[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeDefaultArray()
|
||||
{
|
||||
ExceptionAssert.Throws<NullReferenceException>("Object reference not set to an instance of an object.", () =>
|
||||
JsonConvert.SerializeObject(default(ImmutableArray<int>), Formatting.Indented));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Queue
|
||||
[Test]
|
||||
|
||||
@@ -548,6 +548,109 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(RegexOptions.CultureInvariant, r2.Options);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmbedJValueStringInNewJObject()
|
||||
{
|
||||
string s = null;
|
||||
var v = new JValue(s);
|
||||
var o = JObject.FromObject(new { title = v });
|
||||
|
||||
JObject oo = new JObject
|
||||
{
|
||||
{"title", v}
|
||||
};
|
||||
|
||||
string output = o.ToString();
|
||||
|
||||
Assert.AreEqual(null, v.Value);
|
||||
Assert.AreEqual(JTokenType.String, v.Type);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""title"": null
|
||||
}", output);
|
||||
}
|
||||
|
||||
// bug: the generic member (T) that hides the base member will not
|
||||
// be used when serializing and deserializing the object,
|
||||
// resulting in unexpected behavior during serialization and deserialization.
|
||||
|
||||
public class Foo1
|
||||
{
|
||||
public object foo { get; set; }
|
||||
}
|
||||
|
||||
public class Bar1
|
||||
{
|
||||
public object bar { get; set; }
|
||||
}
|
||||
|
||||
public class Foo1<T> : Foo1
|
||||
{
|
||||
public new T foo { get; set; }
|
||||
|
||||
public T foo2 { get; set; }
|
||||
}
|
||||
|
||||
public class FooBar1 : Foo1
|
||||
{
|
||||
public new Bar1 foo { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BaseClassSerializesAsExpected()
|
||||
{
|
||||
var original = new Foo1 { foo = "value" };
|
||||
var json = JsonConvert.SerializeObject(original);
|
||||
var expectedJson = @"{""foo"":""value""}";
|
||||
Assert.AreEqual(expectedJson, json); // passes
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BaseClassDeserializesAsExpected()
|
||||
{
|
||||
var json = @"{""foo"":""value""}";
|
||||
var deserialized = JsonConvert.DeserializeObject<Foo1>(json);
|
||||
Assert.AreEqual("value", deserialized.foo); // passes
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DerivedClassHidingBasePropertySerializesAsExpected()
|
||||
{
|
||||
var original = new FooBar1 { foo = new Bar1 { bar = "value" } };
|
||||
var json = JsonConvert.SerializeObject(original);
|
||||
var expectedJson = @"{""foo"":{""bar"":""value""}}";
|
||||
Assert.AreEqual(expectedJson, json); // passes
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DerivedClassHidingBasePropertyDeserializesAsExpected()
|
||||
{
|
||||
var json = @"{""foo"":{""bar"":""value""}}";
|
||||
var deserialized = JsonConvert.DeserializeObject<FooBar1>(json);
|
||||
Assert.IsNotNull(deserialized.foo); // passes
|
||||
Assert.AreEqual("value", deserialized.foo.bar); // passes
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DerivedGenericClassHidingBasePropertySerializesAsExpected()
|
||||
{
|
||||
var original = new Foo1<Bar1> { foo = new Bar1 { bar = "value" }, foo2 = new Bar1 { bar = "value2" } };
|
||||
var json = JsonConvert.SerializeObject(original);
|
||||
var expectedJson = @"{""foo"":{""bar"":""value""},""foo2"":{""bar"":""value2""}}";
|
||||
Assert.AreEqual(expectedJson, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DerivedGenericClassHidingBasePropertyDeserializesAsExpected()
|
||||
{
|
||||
var json = @"{""foo"":{""bar"":""value""},""foo2"":{""bar"":""value2""}}";
|
||||
var deserialized = JsonConvert.DeserializeObject<Foo1<Bar1>>(json);
|
||||
Assert.IsNotNull(deserialized.foo2); // passes (bug only occurs for generics that /hide/ another property)
|
||||
Assert.AreEqual("value2", deserialized.foo2.bar); // also passes, with no issue
|
||||
Assert.IsNotNull(deserialized.foo);
|
||||
Assert.AreEqual("value", deserialized.foo.bar);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConversionOperator()
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="FSharp.Core" version="4.0.0" targetFramework="net45" />
|
||||
<package id="Microsoft.Bcl.Immutable" version="1.0.27" targetFramework="net45" />
|
||||
<package id="Microsoft.Bcl.Immutable" version="1.1.20-beta" targetFramework="net45" />
|
||||
<package id="NUnit" version="2.6.2" targetFramework="net45" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user