From 2320d2d55e1fc70fa6370ee4d16d961ddd4383be Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sat, 1 Sep 2012 14:08:11 +1200 Subject: [PATCH] -Remove IgnoreAll and move fix to Ignore --- Doc/ReducingSerializedJSONSize.aml | 2 +- Doc/SerializationSettings.aml | 6 +----- .../Documentation/SerializationTests.cs | 2 +- .../Serialization/DefaultValueHandlingTests.cs | 16 +++------------- Src/Newtonsoft.Json/DefaultValueHandling.cs | 17 +++++------------ Src/Newtonsoft.Json/JsonSerializer.cs | 2 +- .../Serialization/DefaultContractResolver.cs | 2 +- .../JsonSerializerInternalWriter.cs | 8 +------- 8 files changed, 14 insertions(+), 41 deletions(-) diff --git a/Doc/ReducingSerializedJSONSize.aml b/Doc/ReducingSerializedJSONSize.aml index a99bedea..7a996448 100644 --- a/Doc/ReducingSerializedJSONSize.aml +++ b/Doc/ReducingSerializedJSONSize.aml @@ -84,7 +84,7 @@ T:Newtonsoft.Json.DefaultValueHandling is an option on the JsonSerializer and controls how the serializer handles - properties with a default value. Setting a value of DefaultValueHandling.IgnoreAll + properties with a default value. Setting a value of DefaultValueHandling.Ignore will make the JsonSerializer skip writing any properties that have a default value to the JSON result. For object references this will be null. For value types like int and DateTime the serializer will skip the default uninitialized diff --git a/Doc/SerializationSettings.aml b/Doc/SerializationSettings.aml index e5a89c69..6b275083 100644 --- a/Doc/SerializationSettings.aml +++ b/Doc/SerializationSettings.aml @@ -130,12 +130,8 @@ Ignore - Json.NET will skip writing a field/property value to JSON if the value is null or is the same as the value specified in field/property's T:System.ComponentModel.DefaultValueAttribute. The Json.NET deserializer will skip setting a .NET object's field/property if the JSON value is the same as the default value. - - - IgnoreAll Json.NET will skip writing a field/property value to JSON if the value is the same as the field/property's default value, or the custom - value specified in T:System.ComponentModel.DefaultValueAttribute if it is present. The Json.NET deserializer + value specified in T:System.ComponentModel.DefaultValueAttribute if the attribute is present. The Json.NET deserializer will skip setting a .NET object's field/property if the JSON value is the same as the default value. diff --git a/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs b/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs index b524bd38..2b82d61f 100644 --- a/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs +++ b/Src/Newtonsoft.Json.Tests/Documentation/SerializationTests.cs @@ -725,7 +725,7 @@ namespace Newtonsoft.Json.Tests.Documentation string ignored = JsonConvert.SerializeObject(invoice, Formatting.Indented, - new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAll }); + new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); // { // "Company": "Acme Ltd.", diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs index 7f4db1f0..5aff8ece 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DefaultValueHandlingTests.cs @@ -78,18 +78,8 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(@"{ ""Company"": ""Acme Ltd."", - ""Amount"": 50.0, - ""Paid"": false -}", ignored); - - string ignoredAll = JsonConvert.SerializeObject(invoice, - Formatting.Indented, - new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAll }); - - Assert.AreEqual(@"{ - ""Company"": ""Acme Ltd."", ""Amount"": 50.0 -}", ignoredAll); +}", ignored); } [Test] @@ -221,7 +211,7 @@ namespace Newtonsoft.Json.Tests.Serialization json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings { - DefaultValueHandling = DefaultValueHandling.IgnoreAll + DefaultValueHandling = DefaultValueHandling.Ignore }); Assert.AreEqual(@"{ @@ -247,7 +237,7 @@ namespace Newtonsoft.Json.Tests.Serialization public class DefaultValueHandlingPropertyClass { - [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAll)] + [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] public int IntIgnore { get; set; } [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)] diff --git a/Src/Newtonsoft.Json/DefaultValueHandling.cs b/Src/Newtonsoft.Json/DefaultValueHandling.cs index 908fd722..92ee7b62 100644 --- a/Src/Newtonsoft.Json/DefaultValueHandling.cs +++ b/Src/Newtonsoft.Json/DefaultValueHandling.cs @@ -45,9 +45,10 @@ namespace Newtonsoft.Json Include = 0, /// /// Ignore members where the member value is the same as the member's default value when serializing objects - /// so that is is not written to JSON. This option will only ignore null values for object and nullable types - /// unless the property explicitly has a specified. To ignore default values - /// for all types use the IgnoreAll option. + /// so that is is not written to JSON. + /// This option will ignore all default values (e.g. null for objects and nullable typesl; 0 for integers, + /// decimals and floating point numbers; and false for booleans). The default value ignored can be override by + /// the . /// Ignore = 1, /// @@ -58,14 +59,6 @@ namespace Newtonsoft.Json /// Ignore members where the member value is the same as the member's default value when serializing objects /// and sets members to their default value when deserializing. /// - IgnoreAndPopulate = Ignore | Populate, - /// - /// Ignore members where the member value is the same as the member's default value when serializing objects - /// so that is is not written to JSON, and ignores setting members when the JSON value equals the member's default value. - /// This option will ignore all default values (e.g. null for objects and nullable typesl; 0 for integers, - /// decimals and floating point numbers; and false for booleans). The default value ignored can be override by - /// the . - /// - IgnoreAll = 4 + IgnoreAndPopulate = Ignore | Populate } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 8a1b1044..f76e96b3 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -209,7 +209,7 @@ namespace Newtonsoft.Json get { return _defaultValueHandling; } set { - if (value < DefaultValueHandling.Include || value > DefaultValueHandling.IgnoreAll) + if (value < DefaultValueHandling.Include || value > DefaultValueHandling.IgnoreAndPopulate) throw new ArgumentOutOfRangeException("value"); _defaultValueHandling = value; diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index c7ac4a1a..9ef96c15 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -976,7 +976,7 @@ namespace Newtonsoft.Json.Serialization { property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; property.Order = (dataMemberAttribute.Order != -1) ? (int?) dataMemberAttribute.Order : null; - property.DefaultValueHandling = (!dataMemberAttribute.EmitDefaultValue) ? (DefaultValueHandling?) DefaultValueHandling.IgnoreAll : null; + property.DefaultValueHandling = (!dataMemberAttribute.EmitDefaultValue) ? (DefaultValueHandling?) DefaultValueHandling.Ignore : null; hasMemberAttribute = true; } #endif diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 73f3ba7b..cdfef01a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -204,13 +204,7 @@ namespace Newtonsoft.Json.Serialization memberValue == null) return false; - DefaultValueHandling defaultValueHandling = property.DefaultValueHandling.GetValueOrDefault(Serializer.DefaultValueHandling); - - if (HasFlag(defaultValueHandling, DefaultValueHandling.Ignore) - && MiscellaneousUtils.ValueEquals(memberValue, property.DefaultValue)) - return false; - - if (defaultValueHandling == DefaultValueHandling.IgnoreAll + if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer.DefaultValueHandling), DefaultValueHandling.Ignore) && MiscellaneousUtils.ValueEquals(memberValue, property.DefaultValue ?? ReflectionUtils.GetDefaultValue(property.PropertyType))) return false;