Skip to content
Browse files

Merge branch 'JsonDecimalBugFix' of https://github.com/PedroLamas/Res…

  • Loading branch information...
2 parents 734be6a + c150402 commit 837f6a823f4eed16ad2ecbbf87f1d04045314d10 @ayoung ayoung committed Apr 9, 2012
View
35 RestSharp.Tests/CultureChange.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Globalization;
+using System.Threading;
+
+namespace RestSharp.Tests
+{
+ public class CultureChange : IDisposable
+ {
+ public CultureInfo PreviousCulture { get; private set; }
+
+ public CultureChange(string culture)
+ {
+ if (culture == null)
+ throw new ArgumentNullException("culture");
+
+ PreviousCulture = Thread.CurrentThread.CurrentCulture;
+
+ Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
+ }
+
+ #region IDisposable Members
+
+ public void Dispose()
+ {
+ if (PreviousCulture != null)
+ {
+ Thread.CurrentThread.CurrentCulture = PreviousCulture;
+
+ PreviousCulture = null;
+ }
+ }
+
+ #endregion
+ }
+}
View
31 RestSharp.Tests/JsonTests.cs
@@ -30,7 +30,9 @@ namespace RestSharp.Tests
{
public class JsonTests
{
- private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
+ private const string AlternativeCulture = "pt-PT";
+
+ private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
[Fact]
public void Can_Deserialize_4sq_Json_With_Root_Element_Specified()
@@ -233,6 +235,15 @@ public void Can_Deserialize_With_Default_Root()
Assert.Equal("Foe 2", p.Foes["dict2"].Nickname);
}
+ [Fact]
+ public void Can_Deserialize_With_Default_Root_Alternative_Culture()
+ {
+ using (new CultureChange(AlternativeCulture))
+ {
+ Can_Deserialize_With_Default_Root();
+ }
+ }
+
[Fact]
public void Can_Deserialize_Names_With_Underscores_With_Default_Root()
{
@@ -262,6 +273,15 @@ public void Can_Deserialize_Names_With_Underscores_With_Default_Root()
Assert.Equal("Foe 2", p.Foes["dict2"].Nickname);
}
+ [Fact]
+ public void Can_Deserialize_Names_With_Underscores_With_Default_Root_Alternative_Culture()
+ {
+ using (new CultureChange(AlternativeCulture))
+ {
+ Can_Deserialize_Names_With_Underscores_With_Default_Root();
+ }
+ }
+
[Fact]
public void Can_Deserialize_Names_With_Dashes_With_Default_Root()
{
@@ -291,6 +311,15 @@ public void Can_Deserialize_Names_With_Dashes_With_Default_Root()
Assert.Equal("Foe 2", p.Foes["dict2"].Nickname);
}
+ [Fact]
+ public void Can_Deserialize_Names_With_Dashes_With_Default_Root_Alternative_Culture()
+ {
+ using (new CultureChange(AlternativeCulture))
+ {
+ Can_Deserialize_Names_With_Dashes_With_Default_Root();
+ }
+ }
+
[Fact]
public void Ignore_Protected_Property_That_Exists_In_Data()
{
View
1 RestSharp.Tests/RestSharp.Tests.csproj
@@ -78,6 +78,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="CultureChange.cs" />
<Compile Include="JsonTests.cs" />
<Compile Include="Fakes\NullHttp.cs" />
<Compile Include="NamespacedXmlTests.cs" />
View
24 RestSharp/Deserializers/JsonDeserializer.cs
@@ -78,6 +78,8 @@ private void Map(object target, IDictionary<string, object> data)
if (value == null) continue;
+ var stringValue = Convert.ToString(value, Culture);
+
// check for nullable and extract underlying type
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
@@ -88,37 +90,34 @@ private void Map(object target, IDictionary<string, object> data)
{
// no primitives can contain quotes so we can safely remove them
// allows converting a json value like {"index": "1"} to an int
- var tmpVal = value.ToString().Replace("\"", string.Empty);
+ var tmpVal = stringValue.Replace("\"", string.Empty);
prop.SetValue(target, tmpVal.ChangeType(type, Culture), null);
}
else if (type.IsEnum)
{
- var converted = type.FindEnumValue(value.ToString(), Culture);
+ var converted = type.FindEnumValue(stringValue, Culture);
prop.SetValue(target, converted, null);
}
else if (type == typeof(Uri))
{
- string raw = value.ToString();
- var uri = new Uri(raw, UriKind.RelativeOrAbsolute);
+ var uri = new Uri(stringValue, UriKind.RelativeOrAbsolute);
prop.SetValue(target, uri, null);
}
else if (type == typeof(string))
{
- string raw = value.ToString();
- prop.SetValue(target, raw, null);
+ prop.SetValue(target, stringValue, null);
}
else if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
{
DateTime dt;
- var clean = value.ToString();
if (DateFormat.HasValue())
{
- dt = DateTime.ParseExact(clean, DateFormat, Culture);
+ dt = DateTime.ParseExact(stringValue, DateFormat, Culture);
}
else
{
// try parsing instead
- dt = clean.ParseJsonDate(Culture);
+ dt = stringValue.ParseJsonDate(Culture);
}
if (type == typeof(DateTime))
@@ -132,18 +131,17 @@ private void Map(object target, IDictionary<string, object> data)
}
else if (type == typeof(Decimal))
{
- var dec = Decimal.Parse(value.ToString(), Culture);
+ var dec = Decimal.Parse(stringValue, Culture);
prop.SetValue(target, dec, null);
}
else if (type == typeof(Guid))
{
- string raw = value.ToString();
- var guid = string.IsNullOrEmpty(raw) ? Guid.Empty : new Guid(raw);
+ var guid = string.IsNullOrEmpty(stringValue) ? Guid.Empty : new Guid(stringValue);
prop.SetValue(target, guid, null);
}
else if (type == typeof(TimeSpan))
{
- var timeSpan = TimeSpan.Parse(value.ToString());
+ var timeSpan = TimeSpan.Parse(stringValue);
prop.SetValue(target, timeSpan, null);
}
else if (type.IsGenericType)

0 comments on commit 837f6a8

Please sign in to comment.
Something went wrong with that request. Please try again.