Permalink
Browse files

standardize on InvariantCulture but make it overridable in Deserializ…

…ers via Culture property
  • Loading branch information...
1 parent 0558520 commit 9e2e21c159ae42f98a05e728d004d9fb1858d2b4 @johnsheehan johnsheehan committed Mar 21, 2011
@@ -115,7 +115,7 @@ public void Can_Deserialize_Custom_Formatted_Date()
var formatted = new
{
- StartDate = date.ToString(format, CultureInfo.InvariantCulture)
+ StartDate = date.ToString(format)
};
var data = JsonConvert.SerializeObject(formatted);
@@ -30,6 +30,12 @@ public class JsonDeserializer : IDeserializer
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
+ public CultureInfo Culture { get; set; }
+
+ public JsonDeserializer()
+ {
+ Culture = CultureInfo.InvariantCulture;
+ }
public T Deserialize<T>(RestResponse response) where T : new()
{
@@ -91,7 +97,7 @@ private void Map(object x, JToken json)
if (value == null)
{
// try camel cased name
- actualName = name.ToCamelCase();
+ actualName = name.ToCamelCase(Culture);
value = json[actualName];
}
@@ -171,19 +177,19 @@ private void Map(object x, JToken json)
if (DateFormat.HasValue())
{
var clean = value.ToString().RemoveSurroundingQuotes();
- dt = DateTime.ParseExact(clean, DateFormat, CultureInfo.InvariantCulture);
+ dt = DateTime.ParseExact(clean, DateFormat, Culture);
}
else
{
// try parsing instead
- dt = value.ToString().ParseJsonDate();
+ dt = value.ToString().ParseJsonDate(Culture);
}
prop.SetValue(x, dt, null);
}
else if (type == typeof(Decimal))
{
- var dec = Decimal.Parse(value.ToString());
+ var dec = Decimal.Parse(value.ToString(), Culture);
prop.SetValue(x, dec, null);
}
else if (type == typeof(Guid))
@@ -29,6 +29,12 @@ public class XmlAttributeDeserializer : IDeserializer
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
+ public CultureInfo Culture { get; set; }
+
+ public XmlAttributeDeserializer()
+ {
+ Culture = CultureInfo.InvariantCulture;
+ }
public T Deserialize<T>(RestResponse response) where T : new()
{
@@ -104,35 +110,35 @@ private void Map(object x, XElement root)
if (value == null)
{
- // special case for inline list items
- if (type.IsGenericType)
- {
- var genericType = type.GetGenericArguments()[0];
-
- var first = GetElementByName(root, genericType.Name);
- if (first != null)
- {
- var elements = root.Elements(first.Name);
-
- var list = (IList)Activator.CreateInstance(type);
- PopulateListFromElements(genericType, elements, list);
- prop.SetValue(x, list, null);
-
- }
- }
- continue;
+ // special case for inline list items
+ if (type.IsGenericType)
+ {
+ var genericType = type.GetGenericArguments()[0];
+
+ var first = GetElementByName(root, genericType.Name);
+ if (first != null)
+ {
+ var elements = root.Elements(first.Name);
+
+ var list = (IList)Activator.CreateInstance(type);
+ PopulateListFromElements(genericType, elements, list);
+ prop.SetValue(x, list, null);
+
+ }
+ }
+ continue;
}
- // check for nullable and extract underlying type
- if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
- {
- type = type.GetGenericArguments()[0];
+ // check for nullable and extract underlying type
+ if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
+ {
+ type = type.GetGenericArguments()[0];
- if (string.IsNullOrEmpty(value.ToString()))
- {
- continue;
- }
- }
+ if (string.IsNullOrEmpty(value.ToString()))
+ {
+ continue;
+ }
+ }
if (type.IsPrimitive)
{
@@ -156,7 +162,7 @@ private void Map(object x, XElement root)
{
if (DateFormat.HasValue())
{
- value = DateTime.ParseExact(value.ToString(), DateFormat, CultureInfo.CurrentCulture);
+ value = DateTime.ParseExact(value.ToString(), DateFormat, Culture);
}
else
{
@@ -233,7 +239,7 @@ private object HandleListDerivative(object x, XElement root, string propName, Ty
}
var lowerName = name.ToLower();
- var camelName = name.ToCamelCase();
+ var camelName = name.ToCamelCase(Culture);
var list = (IList)Activator.CreateInstance(type);
@@ -311,7 +317,7 @@ private object GetValueFromXml(XElement root, XName name, bool attribute)
private XElement GetElementByName(XElement root, XName name)
{
var lowerName = XName.Get(name.LocalName.ToLower(), name.NamespaceName);
- var camelName = XName.Get(name.LocalName.ToCamelCase(), name.NamespaceName);
+ var camelName = XName.Get(name.LocalName.ToCamelCase(Culture), name.NamespaceName);
if (root.Element(name) != null)
{
@@ -346,7 +352,7 @@ private XElement GetElementByName(XElement root, XName name)
private XAttribute GetAttributeByName(XElement root, XName name)
{
var lowerName = XName.Get(name.LocalName.ToLower(), name.NamespaceName);
- var camelName = XName.Get(name.LocalName.ToCamelCase(), name.NamespaceName);
+ var camelName = XName.Get(name.LocalName.ToCamelCase(Culture), name.NamespaceName);
if (root.Attribute(name) != null)
{
@@ -30,6 +30,12 @@ public class XmlDeserializer : IDeserializer
public string RootElement { get; set; }
public string Namespace { get; set; }
public string DateFormat { get; set; }
+ public CultureInfo Culture { get; set; }
+
+ public XmlDeserializer()
+ {
+ Culture = CultureInfo.InvariantCulture;
+ }
public T Deserialize<T>(RestResponse response) where T : new()
{
@@ -143,18 +149,18 @@ private void Map(object x, XElement root)
{
if (DateFormat.HasValue())
{
- value = DateTime.ParseExact(value.ToString(), DateFormat, CultureInfo.CurrentCulture);
+ value = DateTime.ParseExact(value.ToString(), DateFormat, Culture);
}
else
{
- value = DateTime.Parse(value.ToString());
+ value = DateTime.Parse(value.ToString(), Culture);
}
prop.SetValue(x, value, null);
}
else if (type == typeof(Decimal))
{
- value = Decimal.Parse(value.ToString());
+ value = Decimal.Parse(value.ToString(), Culture);
prop.SetValue(x, value, null);
}
else if (type == typeof(Guid))
@@ -235,7 +241,7 @@ private object HandleListDerivative(object x, XElement root, string propName, Ty
if (!elements.Any())
{
- var camelName = name.ToCamelCase().AsNamespaced(Namespace);
+ var camelName = name.ToCamelCase(Culture).AsNamespaced(Namespace);
elements = root.Descendants(camelName);
}
@@ -293,7 +299,7 @@ private object GetValueFromXml(XElement root, XName name)
private XElement GetElementByName(XElement root, XName name)
{
var lowerName = name.LocalName.ToLower().AsNamespaced(name.NamespaceName);
- var camelName = name.LocalName.ToCamelCase().AsNamespaced(name.NamespaceName);
+ var camelName = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName);
if (root.Element(name) != null)
{
@@ -334,7 +340,7 @@ private XElement GetElementByName(XElement root, XName name)
private XAttribute GetAttributeByName(XElement root, XName name)
{
var lowerName = name.LocalName.ToLower().AsNamespaced(name.NamespaceName);
- var camelName = name.LocalName.ToCamelCase().AsNamespaced(name.NamespaceName);
+ var camelName = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName);
if (root.Attribute(name) != null)
{
@@ -94,7 +94,7 @@ public static string RemoveUnderscoresAndDashes(this string input)
/// </summary>
/// <param name="input">JSON value to parse</param>
/// <returns>DateTime</returns>
- public static DateTime ParseJsonDate(this string input)
+ public static DateTime ParseJsonDate(this string input, CultureInfo culture)
{
input = input.Replace("\n", "");
input = input.Replace("\r", "");
@@ -103,17 +103,17 @@ public static DateTime ParseJsonDate(this string input)
if (input.Contains("/Date("))
{
- return ExtractDate(input, @"\\/Date\((-?\d+)(-|\+)?([0-9]{4})?\)\\/");
+ return ExtractDate(input, @"\\/Date\((-?\d+)(-|\+)?([0-9]{4})?\)\\/", culture);
}
if (input.Contains("new Date("))
{
input = input.Replace(" ", "");
// because all whitespace is removed, match against newDate( instead of new Date(
- return ExtractDate(input, @"newDate\((-?\d+)*\)");
+ return ExtractDate(input, @"newDate\((-?\d+)*\)", culture);
}
- return ParseFormattedDate(input);
+ return ParseFormattedDate(input, culture);
}
/// <summary>
@@ -131,7 +131,7 @@ public static string RemoveSurroundingQuotes(this string input)
return input;
}
- private static DateTime ParseFormattedDate(string input)
+ private static DateTime ParseFormattedDate(string input, CultureInfo culture)
{
var formats = new[] {
"u",
@@ -144,17 +144,15 @@ private static DateTime ParseFormattedDate(string input)
};
DateTime date;
- if (DateTime.TryParseExact(input, formats,
- CultureInfo.InvariantCulture,
- DateTimeStyles.None, out date))
+ if (DateTime.TryParseExact(input, formats, culture, DateTimeStyles.None, out date))
{
return date;
}
return default(DateTime);
}
- private static DateTime ExtractDate(string input, string pattern)
+ private static DateTime ExtractDate(string input, string pattern, CultureInfo culture)
{
DateTime dt = DateTime.MinValue;
var regex = new Regex(pattern);
@@ -169,7 +167,7 @@ private static DateTime ExtractDate(string input, string pattern)
// adjust if time zone modifier present
if (match.Groups.Count > 2 && !String.IsNullOrEmpty(match.Groups[3].Value))
{
- var mod = DateTime.ParseExact(match.Groups[3].Value, "HHmm", CultureInfo.InvariantCulture);
+ var mod = DateTime.ParseExact(match.Groups[3].Value, "HHmm", culture);
if (match.Groups[2].Value == "+")
{
dt = dt.Add(mod.TimeOfDay);
@@ -200,9 +198,9 @@ public static bool Matches(this string input, string pattern)
/// </summary>
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
/// <returns>string</returns>
- public static string ToPascalCase(this string lowercaseAndUnderscoredWord)
+ public static string ToPascalCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
{
- return ToPascalCase(lowercaseAndUnderscoredWord, true);
+ return ToPascalCase(lowercaseAndUnderscoredWord, true, culture);
}
/// <summary>
@@ -211,7 +209,7 @@ public static string ToPascalCase(this string lowercaseAndUnderscoredWord)
/// <param name="text">String to convert</param>
/// <param name="removeUnderscores">Option to remove underscores</param>
/// <returns></returns>
- public static string ToPascalCase(this string text, bool removeUnderscores)
+ public static string ToPascalCase(this string text, bool removeUnderscores, CultureInfo culture)
{
if (String.IsNullOrEmpty(text))
return text;
@@ -229,25 +227,25 @@ public static string ToPascalCase(this string text, bool removeUnderscores)
string restOfWord = word.Substring(1);
if (restOfWord.IsUpperCase())
- restOfWord = restOfWord.ToLower(CultureInfo.CurrentUICulture);
+ restOfWord = restOfWord.ToLower(culture);
- char firstChar = char.ToUpper(word[0], CultureInfo.CurrentUICulture);
+ char firstChar = char.ToUpper(word[0], culture);
words[i] = String.Concat(firstChar, restOfWord);
}
}
return String.Join(joinString, words);
}
- return String.Concat(words[0].Substring(0, 1).ToUpper(CultureInfo.CurrentUICulture), words[0].Substring(1));
+ return String.Concat(words[0].Substring(0, 1).ToUpper(culture), words[0].Substring(1));
}
/// <summary>
/// Converts a string to camel case
/// </summary>
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
/// <returns>String</returns>
- public static string ToCamelCase(this string lowercaseAndUnderscoredWord)
+ public static string ToCamelCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
{
- return MakeInitialLowerCase(ToPascalCase(lowercaseAndUnderscoredWord));
+ return MakeInitialLowerCase(ToPascalCase(lowercaseAndUnderscoredWord, culture));
}
/// <summary>
@@ -16,6 +16,7 @@
using System;
using RestSharp.Extensions;
+using System.Globalization;
namespace RestSharp.Serializers
{
@@ -30,6 +31,7 @@ public sealed class SerializeAsAttribute : Attribute
public SerializeAsAttribute() {
NameStyle = NameStyle.AsIs;
Index = int.MaxValue;
+ Culture = CultureInfo.InvariantCulture;
}
/// <summary>
@@ -43,6 +45,11 @@ public sealed class SerializeAsAttribute : Attribute
public bool Attribute { get; set; }
/// <summary>
+ /// The culture to use when serializing
+ /// </summary>
+ public CultureInfo Culture { get; set; }
+
+ /// <summary>
/// Transforms the casing of the name based on the selected value.
/// </summary>
public NameStyle NameStyle { get; set; }
@@ -61,9 +68,9 @@ public sealed class SerializeAsAttribute : Attribute
var name = Name ?? input;
switch (NameStyle) {
case NameStyle.CamelCase:
- return name.ToCamelCase();
+ return name.ToCamelCase(Culture);
case NameStyle.PascalCase:
- return name.ToPascalCase();
+ return name.ToPascalCase(Culture);
case NameStyle.LowerCase:
return name.ToLower();
}

0 comments on commit 9e2e21c

Please sign in to comment.