-Add support for using implicit/explicit casts when converting a JSON property name to a dictionary value
-Fix DynamicReflectionDelegateFactory erroring when calling static methods -Fix CustomCreationConverter to return null when converting a null JSON value
This commit is contained in:
@@ -2000,7 +2000,7 @@ keyword such as type of business.""
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not convert string 'Newtonsoft.Json.Tests.TestObjects.Person' to dictionary key type Newtonsoft.Json.Tests.TestObjects.Person. Create a TypeConverter to convert from the string to the key type object.")]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Could not convert string 'Newtonsoft.Json.Tests.TestObjects.Person' to dictionary key type 'Newtonsoft.Json.Tests.TestObjects.Person'. Create a TypeConverter to convert from the string to the key type object.")]
|
||||
public void DeserializePersonKeyedDictionary()
|
||||
{
|
||||
string json =
|
||||
@@ -3316,5 +3316,41 @@ keyword such as type of business.""
|
||||
Assert.AreEqual("James", deserialized.Name);
|
||||
Assert.AreEqual(27, deserialized.Age);
|
||||
}
|
||||
|
||||
public class DictionaryKey
|
||||
{
|
||||
public string Value { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
|
||||
public static implicit operator DictionaryKey(string value)
|
||||
{
|
||||
return new DictionaryKey() {Value = value};
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeDeserializeDictionaryKey()
|
||||
{
|
||||
Dictionary<DictionaryKey, string> dictionary = new Dictionary<DictionaryKey, string>();
|
||||
|
||||
dictionary.Add(new DictionaryKey() { Value = "First!" }, "First");
|
||||
dictionary.Add(new DictionaryKey() { Value = "Second!" }, "Second");
|
||||
|
||||
string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""First!"": ""First"",
|
||||
""Second!"": ""Second""
|
||||
}", json);
|
||||
|
||||
Dictionary<DictionaryKey, string> newDictionary =
|
||||
JsonConvert.DeserializeObject<Dictionary<DictionaryKey, string>>(json);
|
||||
|
||||
Assert.AreEqual(2, newDictionary.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user