Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/Src
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2010-12-19 03:49:35 +0300
committerJamesNK <james@newtonking.com>2010-12-19 03:49:35 +0300
commitc046ac36f9d75b379c37ff9aa3b28c1c3bbb7b3c (patch)
tree98aacf2d9d329052d57108b2726003245354dc28 /Src
parent5ccd8269515c0943d51cfd79e32e31b6a82b3c48 (diff)
-Added CamelCaseText to StringEnumConverter
Diffstat (limited to 'Src')
-rw-r--r--Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs19
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs3
-rw-r--r--Src/Newtonsoft.Json/Converters/StringEnumConverter.cs14
-rw-r--r--Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs13
-rw-r--r--Src/Newtonsoft.Json/Utilities/StringUtils.cs15
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 bbe1568..1b33332 100644
--- a/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
@@ -48,6 +48,23 @@ namespace Newtonsoft.Json.Tests.Converters
}
[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()
{
EnumClass enumClass = new EnumClass();
@@ -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<EnumClass>(json, new StringEnumConverter());
diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs b/Src/Newtonsoft.Json.Tests/TestObjects/StoreColor.cs
index 7c064f4..d50a31e 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 a299697..83e7b90 100644
--- a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs
@@ -34,6 +34,11 @@ namespace Newtonsoft.Json.Converters
/// </summary>
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>
/// 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);
+ }
}
/// <summary>
diff --git a/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs b/Src/Newtonsoft.Json/Serialization/CamelCasePropertyNamesContractResolver.cs
index acda243..f035bb7 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 df5b7ee..3bb5ddc 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