-Changed null values in JToken extension data dictionary to be JTokenType.Null
This commit is contained in:
@@ -84,7 +84,6 @@ using System.Linq;
|
|||||||
#if !(NETFX_CORE)
|
#if !(NETFX_CORE)
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Newtonsoft.Json.Tests.Serialization
|
namespace Newtonsoft.Json.Tests.Serialization
|
||||||
@@ -92,27 +91,44 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonSerializerTest : TestFixtureBase
|
public class JsonSerializerTest : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ExtensionDataWithNull()
|
public void ExtensionDataWithNull()
|
||||||
{
|
{
|
||||||
string json = @"{
|
string json = @"{
|
||||||
'TaxRate': 0.125,
|
'TaxRate': 0.125,
|
||||||
'a':null
|
'a':null
|
||||||
}";
|
}";
|
||||||
|
|
||||||
var invoice = JsonConvert.DeserializeObject<ExtendedObject>(json);
|
var invoice = JsonConvert.DeserializeObject<ExtendedObject>(json);
|
||||||
|
|
||||||
|
Assert.AreEqual(JTokenType.Null, invoice._additionalData["a"].Type);
|
||||||
|
Assert.AreEqual(typeof(double), ((JValue)invoice._additionalData["TaxRate"]).Value.GetType());
|
||||||
|
|
||||||
string result = JsonConvert.SerializeObject(invoice);
|
string result = JsonConvert.SerializeObject(invoice);
|
||||||
|
|
||||||
Assert.AreEqual(@"{""TaxRate"":0.125,""a"":null}", result);
|
Assert.AreEqual(@"{""TaxRate"":0.125,""a"":null}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ExtensionDataFloatParseHandling()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
'TaxRate': 0.125,
|
||||||
|
'a':null
|
||||||
|
}";
|
||||||
|
|
||||||
|
var invoice = JsonConvert.DeserializeObject<ExtendedObject>(json, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
FloatParseHandling = FloatParseHandling.Decimal
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.AreEqual(typeof(decimal), ((JValue)invoice._additionalData["TaxRate"]).Value.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
class ExtendedObject
|
class ExtendedObject
|
||||||
{
|
{
|
||||||
[JsonExtensionData]
|
[JsonExtensionData]
|
||||||
private IDictionary<string, JToken> _additionalData;
|
internal IDictionary<string, JToken> _additionalData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GenericItem<T>
|
public class GenericItem<T>
|
||||||
|
|||||||
@@ -482,8 +482,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
// convert object value to JToken so it is compatible with dictionary
|
// convert object value to JToken so it is compatible with dictionary
|
||||||
// could happen because of primitive types, type name handling and references
|
// could happen because of primitive types, type name handling and references
|
||||||
if (isJTokenValueType && !(value is JToken) && value!=null)
|
if (isJTokenValueType && !(value is JToken))
|
||||||
value = JToken.FromObject(value);
|
value = (value != null) ? JToken.FromObject(value) : new JValue(null, JTokenType.Null);
|
||||||
|
|
||||||
setExtensionDataDictionaryValue(dictionary, key, value);
|
setExtensionDataDictionaryValue(dictionary, key, value);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user