-Fixed empty string being changed to null on object properties

This commit is contained in:
JamesNK
2012-02-05 23:20:29 +00:00
parent 6ce847411a
commit ea72817034
2 changed files with 13 additions and 1 deletions
@@ -5286,6 +5286,18 @@ keyword such as type of business.""
Assert.AreEqual(new WidgetId1 { Value = 1234 }, w.Id);
}
[Test]
public void SerializeDictionaryStringStringAndStringObject()
{
var serializer = JsonSerializer.Create(new JsonSerializerSettings());
var dict = serializer.Deserialize<Dictionary<string, string>>(new JsonTextReader(new StringReader("{'k1':'','k2':'v2'}")));
var reader = new JsonTextReader(new StringReader("{'k1':'','k2':'v2'}"));
var dict2 = serializer.Deserialize<Dictionary<string, object>>(reader);
Assert.AreEqual(dict["k1"], dict2["k1"]);
}
}
public class Widget1
@@ -244,7 +244,7 @@ namespace Newtonsoft.Json.Serialization
return EnsureType(reader, reader.Value, CultureInfo.InvariantCulture, contract, objectType);
case JsonToken.String:
// convert empty string to null automatically for nullable types
if (string.IsNullOrEmpty((string) reader.Value) && objectType != typeof(string) && contract.IsNullable)
if (string.IsNullOrEmpty((string)reader.Value) && objectType != typeof(string) && objectType != typeof(object) && contract.IsNullable)
return null;
// string that needs to be returned as a byte array should be base 64 decoded