Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit bbdf28c

Browse files
committed
Merge pull request #445 from xplicit/fix_number
Fix number with leading zeros serialization bug
2 parents db935bd + 056313e commit bbdf28c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/ServiceStack.Text/JsonObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public string Child(string key)
117117
return base[key];
118118
}
119119

120-
static readonly Regex NumberRegEx = new Regex(@"^[0-9]*(?:\.[0-9]*)?$", PclExport.Instance.RegexOptions);
120+
static readonly Regex NumberRegEx = new Regex(@"^(0|[1-9]*)(?:\.[0-9]*)?$", PclExport.Instance.RegexOptions);
121121

122122
/// <summary>
123123
/// Write JSON Array, Object, bool or number values as raw string

tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ public void Can_parse_empty_object_with_mixed_whitespaces()
2626
Assert.That(JsonObject.Parse("{ \n\t \n\r}"), Is.Empty);
2727
}
2828

29+
[Test]
30+
public void Can_Serialize_numbers()
31+
{
32+
string notNumber = "{\"field\":\"00001\"}";
33+
Assert.AreEqual(notNumber, JsonObject.Parse(notNumber).ToJson<JsonObject>());
34+
35+
string num1 = "{\"field\":0}";
36+
Assert.AreEqual(num1, JsonObject.Parse(num1).ToJson<JsonObject>());
37+
38+
string num2 = "{\"field\":0.5}";
39+
Assert.AreEqual(num2, JsonObject.Parse(num2).ToJson<JsonObject>());
40+
41+
string num3 = "{\"field\":.5}";
42+
Assert.AreEqual(num3, JsonObject.Parse(num3).ToJson<JsonObject>());
43+
44+
string num4 = "{\"field\":12312}";
45+
Assert.AreEqual(num4, JsonObject.Parse(num4).ToJson<JsonObject>());
46+
47+
string num5 = "{\"field\":12312.1231}";
48+
Assert.AreEqual(num5, JsonObject.Parse(num5).ToJson<JsonObject>());
49+
}
2950

3051
public class Jackalope
3152
{

0 commit comments

Comments
 (0)