-Added ReadAsDouble

This commit is contained in:
James Newton-King
2015-12-26 15:53:27 +13:00
parent 7f290b5a85
commit 3773eb5be7
11 changed files with 390 additions and 15 deletions
@@ -3716,9 +3716,9 @@ Path '', line 1, position 1.");
{
string json = @"[]";
ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<double>(json); }, @"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.");
ExceptionAssert.Throws<JsonReaderException>(
() => { JsonConvert.DeserializeObject<double>(json); },
@"Unexpected character encountered while parsing value: [. Path '', line 1, position 1.");
}
#if !(NET35 || NET20 || PORTABLE40)
@@ -532,7 +532,7 @@ namespace Newtonsoft.Json.Tests.Serialization
public class ItemWithTypedPayload
{
public double Payload { get; set; }
public float Payload { get; set; }
}
[Test]
@@ -540,19 +540,19 @@ namespace Newtonsoft.Json.Tests.Serialization
{
ItemWithTypedPayload actual = JsonConvert.DeserializeObject<ItemWithTypedPayload>(@"{
""Payload"": {
""$type"": ""System.Double, mscorlib"",
""$type"": ""System.Single, mscorlib"",
""$value"": ""5""
}
}",
new JsonSerializerSettings());
Assert.AreEqual(5d, actual.Payload);
Assert.AreEqual(5f, actual.Payload);
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.DeserializeObject<ItemWithTypedPayload>(@"{
""Payload"": {
""$type"": ""System.Double, mscorlib"",
""$type"": ""System.Single, mscorlib"",
""$value"": ""5""
}
}",
@@ -560,7 +560,7 @@ namespace Newtonsoft.Json.Tests.Serialization
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore
});
}, @"Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
}, @"Cannot deserialize the current JSON object (e.g. {""name"":""value""}) into type 'System.Single' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Payload.$type', line 3, position 12.");
}