Merge pull request #211 from aromasca/master
null check if value is null before trying to convert into jtoken
This commit is contained in:
@@ -92,6 +92,29 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonSerializerTest : TestFixtureBase
|
public class JsonSerializerTest : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ExtensionDataWithNull()
|
||||||
|
{
|
||||||
|
string json = @"{
|
||||||
|
'TaxRate': 0.125,
|
||||||
|
'a':null
|
||||||
|
}";
|
||||||
|
|
||||||
|
var invoice = JsonConvert.DeserializeObject<ExtendedObject>(json);
|
||||||
|
|
||||||
|
string result = JsonConvert.SerializeObject(invoice);
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{""TaxRate"":0.125,""a"":null}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ExtendedObject
|
||||||
|
{
|
||||||
|
[JsonExtensionData]
|
||||||
|
private IDictionary<string, JToken> _additionalData;
|
||||||
|
}
|
||||||
|
|
||||||
public class GenericItem<T>
|
public class GenericItem<T>
|
||||||
{
|
{
|
||||||
public T Value { get; set; }
|
public T Value { get; set; }
|
||||||
|
|||||||
@@ -482,7 +482,7 @@ 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))
|
if (isJTokenValueType && !(value is JToken) && value!=null)
|
||||||
value = JToken.FromObject(value);
|
value = JToken.FromObject(value);
|
||||||
|
|
||||||
setExtensionDataDictionaryValue(dictionary, key, value);
|
setExtensionDataDictionaryValue(dictionary, key, value);
|
||||||
|
|||||||
Reference in New Issue
Block a user