Skip to content

Commit a10b195

Browse files
author
Michael Hallett
committed
Adds support for deserializing decimal numbers in exponential notation; Closes #633
1 parent 4826f2b commit a10b195

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public class JsonTests
3131

3232
private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
3333

34+
[Fact]
35+
public void Can_Deserialize_Exponential_Notation()
36+
{
37+
const string content = "{ \"Value\": 4.8e-04 }";
38+
var json = new JsonDeserializer();
39+
var output = json.Deserialize<DecimalNumber>(new RestResponse { Content = content });
40+
var expected = Decimal.Parse("4.8e-04", NumberStyles.Float);
41+
42+
Assert.NotNull(output);
43+
Assert.Equal(expected, output.Value);
44+
}
45+
3446
[Fact]
3547
public void Can_Deserialize_Into_Struct()
3648
{

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ private object ConvertValue(Type type, object value)
259259
if (value is double)
260260
return (decimal)((double)value);
261261

262+
if (stringValue.Contains("e"))
263+
return Decimal.Parse(stringValue, NumberStyles.Float, Culture);
264+
262265
return Decimal.Parse(stringValue, Culture);
263266
}
264267
else if (type == typeof(Guid))

0 commit comments

Comments
 (0)