From e83bb410948a2281f1bfd5b8cc023eda60b57225 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 23 Feb 2014 11:15:45 +1300 Subject: [PATCH] -Changed null values in JToken extension data dictionary to be JTokenType.Null --- .../Serialization/JsonSerializerTest.cs | 26 +++++++++++++++---- .../Serialization/DefaultContractResolver.cs | 4 +-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 23309c6a..b883f9b8 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -84,7 +84,6 @@ using System.Linq; #if !(NETFX_CORE) using System.Drawing; using System.Diagnostics; - #endif namespace Newtonsoft.Json.Tests.Serialization @@ -92,27 +91,44 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] public class JsonSerializerTest : TestFixtureBase { - [Test] public void ExtensionDataWithNull() { string json = @"{ - 'TaxRate': 0.125, - 'a':null + 'TaxRate': 0.125, + 'a':null }"; var invoice = JsonConvert.DeserializeObject(json); + Assert.AreEqual(JTokenType.Null, invoice._additionalData["a"].Type); + Assert.AreEqual(typeof(double), ((JValue)invoice._additionalData["TaxRate"]).Value.GetType()); + string result = JsonConvert.SerializeObject(invoice); Assert.AreEqual(@"{""TaxRate"":0.125,""a"":null}", result); } + [Test] + public void ExtensionDataFloatParseHandling() + { + string json = @"{ + 'TaxRate': 0.125, + 'a':null + }"; + + var invoice = JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + FloatParseHandling = FloatParseHandling.Decimal + }); + + Assert.AreEqual(typeof(decimal), ((JValue)invoice._additionalData["TaxRate"]).Value.GetType()); + } class ExtendedObject { [JsonExtensionData] - private IDictionary _additionalData; + internal IDictionary _additionalData; } public class GenericItem diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 031376dc..a0b9db09 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -482,8 +482,8 @@ namespace Newtonsoft.Json.Serialization // convert object value to JToken so it is compatible with dictionary // could happen because of primitive types, type name handling and references - if (isJTokenValueType && !(value is JToken) && value!=null) - value = JToken.FromObject(value); + if (isJTokenValueType && !(value is JToken)) + value = (value != null) ? JToken.FromObject(value) : new JValue(null, JTokenType.Null); setExtensionDataDictionaryValue(dictionary, key, value); };