Added JsonTextReader support for NaN, PositiveInfinity and NegativeInfinity constants.
This commit is contained in:
@@ -215,5 +215,38 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
Assert.AreEqual(json, sb.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatingPointNonFiniteNumbers()
|
||||
{
|
||||
string input = @"[
|
||||
NaN,
|
||||
Infinity,
|
||||
-Infinity
|
||||
]";
|
||||
|
||||
StringReader sr = new StringReader(input);
|
||||
|
||||
using (JsonReader jsonReader = new JsonTextReader(sr))
|
||||
{
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.Float);
|
||||
Assert.AreEqual(jsonReader.Value, double.NaN);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.Float);
|
||||
Assert.AreEqual(jsonReader.Value, double.PositiveInfinity);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.Float);
|
||||
Assert.AreEqual(jsonReader.Value, double.NegativeInfinity);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,5 +172,38 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FloatingPointNonFiniteNumbers()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringWriter sw = new StringWriter(sb);
|
||||
|
||||
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
|
||||
{
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
jsonWriter.WriteStartArray();
|
||||
jsonWriter.WriteValue(double.NaN);
|
||||
jsonWriter.WriteValue(double.PositiveInfinity);
|
||||
jsonWriter.WriteValue(double.NegativeInfinity);
|
||||
jsonWriter.WriteValue(float.NaN);
|
||||
jsonWriter.WriteValue(float.PositiveInfinity);
|
||||
jsonWriter.WriteValue(float.NegativeInfinity);
|
||||
jsonWriter.WriteEndArray();
|
||||
}
|
||||
|
||||
string expected = @"[
|
||||
NaN,
|
||||
Infinity,
|
||||
-Infinity,
|
||||
NaN,
|
||||
Infinity,
|
||||
-Infinity
|
||||
]";
|
||||
string result = sb.ToString();
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,21 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
public static readonly string Undefined;
|
||||
|
||||
/// <summary>
|
||||
/// Represents JavaScript's positive infinity as a string. This field is read-only.
|
||||
/// </summary>
|
||||
public static readonly string PositiveInfinity;
|
||||
|
||||
/// <summary>
|
||||
/// Represents JavaScript's negative infinity as a string. This field is read-only.
|
||||
/// </summary>
|
||||
public static readonly string NegativeInfinity;
|
||||
|
||||
/// <summary>
|
||||
/// Represents JavaScript's NaN as a string. This field is read-only.
|
||||
/// </summary>
|
||||
public static readonly string NaN;
|
||||
|
||||
internal static long InitialJavaScriptDateTicks;
|
||||
internal static DateTime MinimumJavaScriptDate;
|
||||
|
||||
@@ -74,6 +89,9 @@ namespace Newtonsoft.Json
|
||||
False = "false";
|
||||
Null = "null";
|
||||
Undefined = "undefined";
|
||||
PositiveInfinity = "Infinity";
|
||||
NegativeInfinity = "-Infinity";
|
||||
NaN = "NaN";
|
||||
|
||||
InitialJavaScriptDateTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;
|
||||
MinimumJavaScriptDate = new DateTime(100, 1, 1);
|
||||
|
||||
@@ -477,6 +477,18 @@ namespace Newtonsoft.Json
|
||||
throw new JsonReaderException("Unexpected end");
|
||||
}
|
||||
return true;
|
||||
case 'N':
|
||||
ParseNumberNaN();
|
||||
return true;
|
||||
case 'I':
|
||||
ParseNumberPositiveInfinity();
|
||||
return true;
|
||||
case '-':
|
||||
if (PeekNext() == 'I')
|
||||
ParseNumberNegativeInfinity();
|
||||
else
|
||||
ParseNumber();
|
||||
return true;
|
||||
case '/':
|
||||
ParseComment();
|
||||
return true;
|
||||
@@ -738,6 +750,42 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseNumberNegativeInfinity()
|
||||
{
|
||||
if (MatchValue(JavaScriptConvert.NegativeInfinity, true))
|
||||
{
|
||||
SetToken(JsonToken.Float, double.NegativeInfinity);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonReaderException("Error parsing negative infinity value.");
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseNumberPositiveInfinity()
|
||||
{
|
||||
if (MatchValue(JavaScriptConvert.PositiveInfinity, true))
|
||||
{
|
||||
SetToken(JsonToken.Float, double.PositiveInfinity);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonReaderException("Error parsing positive infinity value.");
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseNumberNaN()
|
||||
{
|
||||
if (MatchValue(JavaScriptConvert.NaN, true))
|
||||
{
|
||||
SetToken(JsonToken.Float, double.NaN);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonReaderException("Error parsing NaN value.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the <see cref="State"/> to Closed.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user