From c046ac36f9d75b379c37ff9aa3b28c1c3bbb7b3c Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sun, 19 Dec 2010 00:49:35 +0000 Subject: [PATCH] -Added CamelCaseText to StringEnumConverter --- .../Converters/StringEnumConverterTests.cs | 19 ++++++++++++++++++- .../TestObjects/StoreColor.cs | 3 ++- .../Converters/StringEnumConverter.cs | 14 ++++++++++++++ .../CamelCasePropertyNamesContractResolver.cs | 13 ++----------- Src/Newtonsoft.Json/Utilities/StringUtils.cs | 15 +++++++++++++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs index bbe15680..1b333322 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs @@ -47,6 +47,23 @@ namespace Newtonsoft.Json.Tests.Converters }", 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] public void SerializeEnumClassUndefined() { @@ -116,7 +133,7 @@ namespace Newtonsoft.Json.Tests.Converters string json = @"{ ""StoreColor"": ""Red, White"", ""NullableStoreColor1"": 0, - ""NullableStoreColor2"": ""Black, Red, White"" + ""NullableStoreColor2"": ""black, Red, White"" }"; EnumClass enumClass = JsonConvert.DeserializeObject(json, new StringEnumConverter()); diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs b/Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs index 7c064f48..d50a31ed 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs @@ -33,6 +33,7 @@ namespace Newtonsoft.Json.Tests.TestObjects Black = 1, Red = 2, Yellow = 4, - White = 8 + White = 8, + DarkGoldenrod = 16 } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs index a2996971..83e7b901 100644 --- a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs +++ b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs @@ -34,6 +34,11 @@ namespace Newtonsoft.Json.Converters /// public class StringEnumConverter : JsonConverter { + /// + /// Gets or sets a value indicating whether the written enum text should be camel case. + /// + /// true if the written enum text will be camel case; otherwise, false. + public bool CamelCaseText { get; set; } /// /// Writes the JSON representation of the object. @@ -53,9 +58,18 @@ namespace Newtonsoft.Json.Converters string enumName = e.ToString("G"); if (char.IsNumber(enumName[0]) || enumName[0] == '-') + { + // enum value didn't have an option against it + // fallback to writing number value writer.WriteValue(value); + } else + { + if (CamelCaseText) + enumName = StringUtils.ToCamelCase(enumName); + writer.WriteValue(enumName); + } } /// diff --git a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs index acda2435..f035bb7d 100644 --- a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs @@ -24,6 +24,7 @@ #endregion using System.Globalization; +using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json.Serialization { @@ -48,17 +49,7 @@ namespace Newtonsoft.Json.Serialization protected override string ResolvePropertyName(string propertyName) { // lower case the first letter of the passed in name - if (string.IsNullOrEmpty(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; + return StringUtils.ToCamelCase(propertyName); } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index df5b7ee1..3bb5ddc3 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -369,5 +369,20 @@ namespace Newtonsoft.Json.Utilities 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; + } } } \ No newline at end of file