null check if value is null before trying to convert into jtoken fixes #210

This commit is contained in:
Avi Romascanu
2014-02-09 19:24:52 +02:00
parent 71a91bf34f
commit 2368a8e1b1
2 changed files with 24 additions and 1 deletions
@@ -92,6 +92,29 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture]
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 T Value { get; set; }
@@ -482,7 +482,7 @@ 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))
if (isJTokenValueType && !(value is JToken) && value!=null)
value = JToken.FromObject(value);
setExtensionDataDictionaryValue(dictionary, key, value);