-Added constructor to StringEnumConverter for setting camelcase from attribute

This commit is contained in:
James Newton-King
2016-02-06 16:27:33 +13:00
parent 04f7bb5d65
commit 0311bc0f24
2 changed files with 32 additions and 0 deletions
@@ -104,6 +104,28 @@ namespace Newtonsoft.Json.Tests.Converters
public NegativeEnum Value2 { get; set; }
}
[JsonConverter(typeof(StringEnumConverter), true)]
public enum CamelCaseEnum
{
This,
Is,
CamelCase
}
[Test]
public void Serialize_CamelCaseFromAttribute()
{
string json = JsonConvert.SerializeObject(CamelCaseEnum.CamelCase);
Assert.AreEqual(@"""camelCase""", json);
}
[Test]
public void Deserialize_CamelCaseFromAttribute()
{
CamelCaseEnum e = JsonConvert.DeserializeObject<CamelCaseEnum>(@"""camelCase""");
Assert.AreEqual(CamelCaseEnum.CamelCase, e);
}
#if !NET20
[Test]
public void NamedEnumDuplicateTest()
@@ -63,6 +63,16 @@ namespace Newtonsoft.Json.Converters
AllowIntegerValues = true;
}
/// <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>
public StringEnumConverter(bool camelCaseText)
: this()
{
CamelCaseText = camelCaseText;
}
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>