-Added CamelCaseText to StringEnumConverter
This commit is contained in:
@@ -47,6 +47,23 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
}", json);
|
}", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SerializeEnumClassWithCamelCase()
|
||||||
|
{
|
||||||
|
EnumClass enumClass = new EnumClass();
|
||||||
|
enumClass.StoreColor = StoreColor.Red;
|
||||||
|
enumClass.NullableStoreColor1 = StoreColor.DarkGoldenrod;
|
||||||
|
enumClass.NullableStoreColor2 = null;
|
||||||
|
|
||||||
|
string json = JsonConvert.SerializeObject(enumClass, Formatting.Indented, new StringEnumConverter { CamelCaseText = true });
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""StoreColor"": ""red"",
|
||||||
|
""NullableStoreColor1"": ""darkGoldenrod"",
|
||||||
|
""NullableStoreColor2"": null
|
||||||
|
}", json);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void SerializeEnumClassUndefined()
|
public void SerializeEnumClassUndefined()
|
||||||
{
|
{
|
||||||
@@ -116,7 +133,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
|||||||
string json = @"{
|
string json = @"{
|
||||||
""StoreColor"": ""Red, White"",
|
""StoreColor"": ""Red, White"",
|
||||||
""NullableStoreColor1"": 0,
|
""NullableStoreColor1"": 0,
|
||||||
""NullableStoreColor2"": ""Black, Red, White""
|
""NullableStoreColor2"": ""black, Red, White""
|
||||||
}";
|
}";
|
||||||
|
|
||||||
EnumClass enumClass = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
|
EnumClass enumClass = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
|||||||
Black = 1,
|
Black = 1,
|
||||||
Red = 2,
|
Red = 2,
|
||||||
Yellow = 4,
|
Yellow = 4,
|
||||||
White = 8
|
White = 8,
|
||||||
|
DarkGoldenrod = 16
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,11 @@ namespace Newtonsoft.Json.Converters
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class StringEnumConverter : JsonConverter
|
public class StringEnumConverter : JsonConverter
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether the written enum text should be camel case.
|
||||||
|
/// </summary>
|
||||||
|
/// <value><c>true</c> if the written enum text will be camel case; otherwise, <c>false</c>.</value>
|
||||||
|
public bool CamelCaseText { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes the JSON representation of the object.
|
/// Writes the JSON representation of the object.
|
||||||
@@ -53,9 +58,18 @@ namespace Newtonsoft.Json.Converters
|
|||||||
string enumName = e.ToString("G");
|
string enumName = e.ToString("G");
|
||||||
|
|
||||||
if (char.IsNumber(enumName[0]) || enumName[0] == '-')
|
if (char.IsNumber(enumName[0]) || enumName[0] == '-')
|
||||||
|
{
|
||||||
|
// enum value didn't have an option against it
|
||||||
|
// fallback to writing number value
|
||||||
writer.WriteValue(value);
|
writer.WriteValue(value);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
if (CamelCaseText)
|
||||||
|
enumName = StringUtils.ToCamelCase(enumName);
|
||||||
|
|
||||||
writer.WriteValue(enumName);
|
writer.WriteValue(enumName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using Newtonsoft.Json.Utilities;
|
||||||
|
|
||||||
namespace Newtonsoft.Json.Serialization
|
namespace Newtonsoft.Json.Serialization
|
||||||
{
|
{
|
||||||
@@ -48,17 +49,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
protected override string ResolvePropertyName(string propertyName)
|
protected override string ResolvePropertyName(string propertyName)
|
||||||
{
|
{
|
||||||
// lower case the first letter of the passed in name
|
// lower case the first letter of the passed in name
|
||||||
if (string.IsNullOrEmpty(propertyName))
|
return StringUtils.ToCamelCase(propertyName);
|
||||||
return propertyName;
|
|
||||||
|
|
||||||
if (!char.IsUpper(propertyName[0]))
|
|
||||||
return propertyName;
|
|
||||||
|
|
||||||
string camelCaseName = char.ToLower(propertyName[0], CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
|
|
||||||
if (propertyName.Length > 1)
|
|
||||||
camelCaseName += propertyName.Substring(1);
|
|
||||||
|
|
||||||
return camelCaseName;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -369,5 +369,20 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return caseSensitiveResults.SingleOrDefault();
|
return caseSensitiveResults.SingleOrDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string ToCamelCase(string s)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(s))
|
||||||
|
return s;
|
||||||
|
|
||||||
|
if (!char.IsUpper(s[0]))
|
||||||
|
return s;
|
||||||
|
|
||||||
|
string camelCase = char.ToLower(s[0], CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
|
||||||
|
if (s.Length > 1)
|
||||||
|
camelCase += s.Substring(1);
|
||||||
|
|
||||||
|
return camelCase;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user