-Added IgnoreAll to DefaultValueHandling which ignores all default class/struct/privative values
-Added support for DataMemberAttribute's EmitDefaultValue -Fixed losing decimal precision when writing JValues -Fixed error when calling ToList on empty JObjects and JArrays
This commit is contained in:
@@ -725,7 +725,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
||||
|
||||
string ignored = JsonConvert.SerializeObject(invoice,
|
||||
Formatting.Indented,
|
||||
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
|
||||
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAll });
|
||||
|
||||
// {
|
||||
// "Company": "Acme Ltd.",
|
||||
|
||||
@@ -701,7 +701,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
// "Enabled": true,
|
||||
// "Price": 2.45,
|
||||
// "StockCount": 9000,
|
||||
// "StockValue": 22050.0
|
||||
// "StockValue": 22050.00
|
||||
// }
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
@@ -709,7 +709,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
""Enabled"": true,
|
||||
""Price"": 2.45,
|
||||
""StockCount"": 9000,
|
||||
""StockValue"": 22050.0
|
||||
""StockValue"": 22050.00
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -561,5 +561,21 @@ Parameter name: index",
|
||||
JArray.Parse(json);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToListOnEmptyArray()
|
||||
{
|
||||
string json = @"{""decks"":[]}";
|
||||
|
||||
JArray decks = (JArray)JObject.Parse(json)["decks"];
|
||||
IList<JToken> l = decks.ToList();
|
||||
Assert.AreEqual(0, l.Count);
|
||||
|
||||
json = @"{""decks"":[1]}";
|
||||
|
||||
decks = (JArray)JObject.Parse(json)["decks"];
|
||||
l = decks.ToList();
|
||||
Assert.AreEqual(1, l.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1846,5 +1846,24 @@ Parameter name: arrayIndex",
|
||||
|
||||
Assert.IsFalse(o1.DeepEquals(null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToListOnEmptyObject()
|
||||
{
|
||||
JObject o = JObject.Parse(@"{}");
|
||||
IList<JToken> l1 = o.ToList<JToken>();
|
||||
Assert.AreEqual(0, l1.Count);
|
||||
|
||||
IList<KeyValuePair<string, JToken>> l2 = o.ToList<KeyValuePair<string, JToken>>();
|
||||
Assert.AreEqual(0, l2.Count);
|
||||
|
||||
o = JObject.Parse(@"{'hi':null}");
|
||||
|
||||
l1 = o.ToList<JToken>();
|
||||
Assert.AreEqual(1, l1.Count);
|
||||
|
||||
l2 = o.ToList<KeyValuePair<string, JToken>>();
|
||||
Assert.AreEqual(1, l2.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -314,5 +314,33 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
Assert.AreEqual("5.2", json);
|
||||
}
|
||||
|
||||
public class Rate
|
||||
{
|
||||
public decimal Compoundings { get; set; }
|
||||
}
|
||||
|
||||
private readonly Rate rate = new Rate { Compoundings = 12.166666666666666666666666667m };
|
||||
|
||||
[Test]
|
||||
public void WriteFullDecimalPrecision()
|
||||
{
|
||||
var jTokenWriter = new JTokenWriter();
|
||||
new JsonSerializer().Serialize(jTokenWriter, rate);
|
||||
string json = jTokenWriter.Token.ToString();
|
||||
Assert.AreEqual(@"{
|
||||
""Compoundings"": 12.166666666666666666666666667
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RoundTripDecimal()
|
||||
{
|
||||
var jTokenWriter = new JTokenWriter();
|
||||
new JsonSerializer().Serialize(jTokenWriter, rate);
|
||||
var rate2 = new JsonSerializer().Deserialize<Rate>(new JTokenReader(jTokenWriter.Token));
|
||||
|
||||
Assert.AreEqual(rate.Compoundings, rate2.Compoundings);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,5 +76,5 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("4.5.0.0")]
|
||||
#if !PocketPC
|
||||
[assembly: AssemblyFileVersion("4.5.8.15215")]
|
||||
[assembly: AssemblyFileVersion("4.5.8.15301")]
|
||||
#endif
|
||||
|
||||
@@ -25,6 +25,12 @@
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
#if !(SILVERLIGHT || PocketPC || NET20 || NET35 || NETFX_CORE || PORTABLE)
|
||||
using System.Runtime.Serialization.Json;
|
||||
#endif
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
@@ -57,26 +63,33 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Formatting.Indented,
|
||||
new JsonSerializerSettings { });
|
||||
|
||||
// {
|
||||
// "Company": "Acme Ltd.",
|
||||
// "Amount": 50.0,
|
||||
// "Paid": false,
|
||||
// "PaidDate": null,
|
||||
// "FollowUpDays": 30,
|
||||
// "FollowUpEmailAddress": ""
|
||||
// }
|
||||
Assert.AreEqual(@"{
|
||||
""Company"": ""Acme Ltd."",
|
||||
""Amount"": 50.0,
|
||||
""Paid"": false,
|
||||
""PaidDate"": null,
|
||||
""FollowUpDays"": 30,
|
||||
""FollowUpEmailAddress"": """"
|
||||
}", included);
|
||||
|
||||
string ignored = JsonConvert.SerializeObject(invoice,
|
||||
Formatting.Indented,
|
||||
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
|
||||
|
||||
// {
|
||||
// "Company": "Acme Ltd.",
|
||||
// "Amount": 50.0
|
||||
// }
|
||||
Assert.AreEqual(@"{
|
||||
""Company"": ""Acme Ltd."",
|
||||
""Amount"": 50.0,
|
||||
""Paid"": false
|
||||
}", ignored);
|
||||
|
||||
Console.WriteLine(included);
|
||||
Console.WriteLine(ignored);
|
||||
string ignoredAll = JsonConvert.SerializeObject(invoice,
|
||||
Formatting.Indented,
|
||||
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAll });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""Company"": ""Acme Ltd."",
|
||||
""Amount"": 50.0
|
||||
}", ignoredAll);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -172,5 +185,126 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.IsFalse(MathUtils.ApproxEquals(1000.0, 1000.000000000001));
|
||||
Assert.IsFalse(MathUtils.ApproxEquals(0.0, 0.00001));
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[Test]
|
||||
public void EmitDefaultValueTest()
|
||||
{
|
||||
EmitDefaultValueClass c = new EmitDefaultValueClass();
|
||||
|
||||
#if !(SILVERLIGHT || PocketPC || NET20 || NET35 || NETFX_CORE || PORTABLE)
|
||||
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(EmitDefaultValueClass));
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
jsonSerializer.WriteObject(ms, c);
|
||||
|
||||
Assert.AreEqual("{}", Encoding.UTF8.GetString(ms.ToArray()));
|
||||
#endif
|
||||
|
||||
string json = JsonConvert.SerializeObject(c);
|
||||
|
||||
Assert.AreEqual("{}", json);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void DefaultValueHandlingPropertyTest()
|
||||
{
|
||||
DefaultValueHandlingPropertyClass c = new DefaultValueHandlingPropertyClass();
|
||||
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""IntInclude"": 0,
|
||||
""IntDefault"": 0
|
||||
}", json);
|
||||
|
||||
json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DefaultValueHandling = DefaultValueHandling.IgnoreAll
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""IntInclude"": 0
|
||||
}", json);
|
||||
|
||||
json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DefaultValueHandling = DefaultValueHandling.Include
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""IntInclude"": 0,
|
||||
""IntDefault"": 0
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultStruct
|
||||
{
|
||||
public string Default { get; set; }
|
||||
}
|
||||
|
||||
public class DefaultValueHandlingPropertyClass
|
||||
{
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAll)]
|
||||
public int IntIgnore { get; set; }
|
||||
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
|
||||
public int IntInclude { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
public int IntDefault { get; set; }
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[DataContract]
|
||||
public class EmitDefaultValueClass
|
||||
{
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public Guid Guid { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public TimeSpan TimeSpan { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public DateTime DateTime { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public DateTimeOffset DateTimeOffset { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public decimal Decimal { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public int Integer { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public double Double { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public bool Boolean { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public DefaultStruct Struct { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public StringComparison Enum { get; set; }
|
||||
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public Guid? NullableGuid { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public TimeSpan? NullableTimeSpan { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public DateTime? NullableDateTime { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public DateTimeOffset? NullableDateTimeOffset { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public decimal? NullableDecimal { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public int? NullableInteger { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public double? NullableDouble { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public bool? NullableBoolean { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public DefaultStruct? NullableStruct { get; set; }
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public StringComparison? NullableEnum { get; set; }
|
||||
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
public object Object { get; set; }
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user