Add construct public StringEnumConverter(bool camelCaseText, bool all… (#1700)

This commit is contained in:
ROY
2018-05-27 13:47:01 +12:00
committed by James Newton-King
parent 4257e63394
commit ab67fd558f
2 changed files with 40 additions and 0 deletions
@@ -139,6 +139,20 @@ namespace Newtonsoft.Json.Tests.Converters
CamelCase
}
[JsonConverter(typeof(StringEnumConverter), true, false)]
public enum NotAllowIntegerValuesEnum
{
Foo = 0,
Bar = 1
}
[JsonConverter(typeof(StringEnumConverter), true, true)]
public enum AllowIntegerValuesEnum
{
Foo = 0,
Bar = 1
}
[Test]
public void Serialize_CamelCaseFromAttribute()
{
@@ -153,6 +167,22 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(CamelCaseEnum.CamelCase, e);
}
[Test]
public void Deserialize_NotAllowIntegerValuesFromAttribute()
{
ExceptionAssert.Throws<JsonSerializationException>(() =>
{
NotAllowIntegerValuesEnum e = JsonConvert.DeserializeObject<NotAllowIntegerValuesEnum>(@"""9""");
});
}
[Test]
public void Deserialize_AllowIntegerValuesAttribute()
{
AllowIntegerValuesEnum e = JsonConvert.DeserializeObject<AllowIntegerValuesEnum>(@"""9""");
Assert.AreEqual(9, (int)e);
}
#if !NET20
[Test]
public void NamedEnumDuplicateTest()
@@ -77,6 +77,16 @@ namespace Newtonsoft.Json.Converters
CamelCaseText = camelCaseText;
}
/// <summary>
/// Initializes a new instance of the <see cref="StringEnumConverter"/> class.
/// </summary>
/// <param name="camelCaseText"><c>true</c> if the written enum text will be camel case; otherwise, <c>false</c>.</param>
/// <param name="allowIntegerValues"><c>true</c> if integers are allowed when deserializing; otherwise, <c>false</c>.</param>
public StringEnumConverter(bool camelCaseText, bool allowIntegerValues) : this(camelCaseText)
{
AllowIntegerValues = allowIntegerValues;
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>