-Fixed StringEnumConverter to throw a JsonSerializerException like the serializer throws for invalid enums

-Changed Windows Phone and Portable builds to use Microsoft.CSharp.dll directly
This commit is contained in:
James Newton-King
2013-03-23 12:48:41 +13:00
parent 271ce7f523
commit c4d375dd69
9 changed files with 137 additions and 30 deletions
@@ -24,6 +24,7 @@
#endregion
using System;
using System.IO;
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
#if !NETFX_CORE
@@ -334,5 +335,30 @@ namespace Newtonsoft.Json.Tests.Converters
EnumClass c = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
Assert.IsNull(c.NullableStoreColor2);
}
public void DeserializeInvalidString()
{
string json = "{ \"Value\" : \"Three\" }";
ExceptionAssert.Throws<JsonSerializationException>(
@"Error converting value ""Three"" to type 'Newtonsoft.Json.Tests.Converters.StringEnumConverterTests+MyEnum'. Path 'Value', line 1, position 19.",
() =>
{
var serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
serializer.Deserialize<Bucket>(new JsonTextReader(new StringReader(json)));
});
}
public class Bucket
{
public MyEnum Value;
}
public enum MyEnum
{
Alpha,
Beta,
}
}
}