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

Commit 85b0a85

Browse files
committed
Add range check for inferring JavaScript number
1 parent 37f1ecc commit 85b0a85

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/ServiceStack.Text/Json/JsonUtils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace ServiceStack.Text.Json
88
{
99
public static class JsonUtils
1010
{
11+
public static long MaxInteger = 9007199254740992;
12+
public static long MinInteger = -9007199254740992;
13+
1114
public const char EscapeChar = '\\';
1215
public const char QuoteChar = '"';
1316
public const string Null = "null";

src/ServiceStack.Text/JsonObject.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,35 @@ public static void WriteValue(TextWriter writer, object value)
133133
|| (firstChar == JsWriter.ListStartChar && lastChar == JsWriter.ListEndChar)
134134
|| JsonUtils.True == strValue
135135
|| JsonUtils.False == strValue
136-
|| NumberRegEx.IsMatch(strValue))
136+
|| (NumberRegEx.IsMatch(strValue) && IsJavaScriptNumber(strValue)))
137137
{
138138
writer.Write(strValue);
139139
return;
140140
}
141141
}
142142
JsonUtils.WriteString(writer, strValue);
143143
}
144-
}
144+
145+
private static bool IsJavaScriptNumber(string strValue)
146+
{
147+
if (!strValue.Contains("."))
148+
{
149+
long longValue;
150+
if (long.TryParse(strValue, out longValue))
151+
{
152+
return longValue < JsonUtils.MaxInteger && longValue > JsonUtils.MinInteger;
153+
}
154+
return false;
155+
}
156+
157+
double doubleValue;
158+
if (double.TryParse(strValue, out doubleValue))
159+
{
160+
return doubleValue < JsonUtils.MaxInteger && doubleValue > JsonUtils.MinInteger;
161+
}
162+
return false;
163+
}
164+
}
145165

146166
public class JsonArrayObjects : List<JsonObject>
147167
{

0 commit comments

Comments
 (0)