Added JsonTextReader support for NaN, PositiveInfinity and NegativeInfinity constants.

This commit is contained in:
JamesNK
2008-03-02 04:04:04 +00:00
parent 8a64988cb6
commit abd8da25a7
4 changed files with 132 additions and 0 deletions
+18
View File
@@ -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);
+48
View File
@@ -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>