Added deserialization support for Double NaN, Infinity, and -Infinity

This commit is contained in:
Inderjeet Singh 2008-12-19 22:21:35 +00:00
parent f33a84c223
commit 362a94ec74
5 changed files with 1914 additions and 1581 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,64 +1,68 @@
/* Generated By:JavaCC: Do not edit this line. JsonParserConstants.java */ /* Generated By:JavaCC: Do not edit this line. JsonParserConstants.java */
package com.google.gson; package com.google.gson;
@SuppressWarnings("all") @SuppressWarnings("all")
interface JsonParserConstants { interface JsonParserConstants {
int EOF = 0; int EOF = 0;
int E = 5; int E = 5;
int DIGITS = 6; int DIGITS = 6;
int NULL = 7; int NULL = 7;
int BOOLEAN = 8; int NAN = 8;
int IDENTIFIER = 9; int INFINITY = 9;
int HEX_CHAR = 10; int BOOLEAN = 10;
int UNICODE_CHAR = 11; int IDENTIFIER = 11;
int ESCAPE_CHAR = 12; int HEX_CHAR = 12;
int SINGLE_QUOTE_LITERAL = 13; int UNICODE_CHAR = 13;
int DOUBLE_QUOTE_LITERAL = 14; int ESCAPE_CHAR = 14;
int QUOTE = 15; int SINGLE_QUOTE_LITERAL = 15;
int ENDQUOTE = 17; int DOUBLE_QUOTE_LITERAL = 16;
int CHAR = 18; int QUOTE = 17;
int CNTRL_ESC = 19; int ENDQUOTE = 19;
int HEX = 21; int CHAR = 20;
int HEX_ESC = 22; int CNTRL_ESC = 21;
int HEX = 23;
int DEFAULT = 0; int HEX_ESC = 24;
int STRING_STATE = 1;
int ESC_STATE = 2; int DEFAULT = 0;
int HEX_STATE = 3; int STRING_STATE = 1;
int ESC_STATE = 2;
String[] tokenImage = { int HEX_STATE = 3;
"<EOF>",
"\" \"", String[] tokenImage = {
"\"\\t\"", "<EOF>",
"\"\\n\"", "\" \"",
"\"\\r\"", "\"\\t\"",
"<E>", "\"\\n\"",
"<DIGITS>", "\"\\r\"",
"\"null\"", "<E>",
"<BOOLEAN>", "<DIGITS>",
"<IDENTIFIER>", "\"null\"",
"<HEX_CHAR>", "\"NaN\"",
"<UNICODE_CHAR>", "\"Infinity\"",
"<ESCAPE_CHAR>", "<BOOLEAN>",
"<SINGLE_QUOTE_LITERAL>", "<IDENTIFIER>",
"<DOUBLE_QUOTE_LITERAL>", "<HEX_CHAR>",
"\"\\\"\"", "<UNICODE_CHAR>",
"\"\\\\\"", "<ESCAPE_CHAR>",
"<ENDQUOTE>", "<SINGLE_QUOTE_LITERAL>",
"<CHAR>", "<DOUBLE_QUOTE_LITERAL>",
"<CNTRL_ESC>", "\"\\\"\"",
"\"u\"", "\"\\\\\"",
"<HEX>", "<ENDQUOTE>",
"<HEX_ESC>", "<CHAR>",
"\"{\"", "<CNTRL_ESC>",
"\"}\"", "\"u\"",
"\",\"", "<HEX>",
"\":\"", "<HEX_ESC>",
"\"[\"", "\"{\"",
"\"]\"", "\"}\"",
"\"-\"", "\",\"",
"\".\"", "\":\"",
}; "\"[\"",
"\"]\"",
} "\"-\"",
"\".\"",
};
}

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,8 @@ TOKEN : {
<E : ["e","E"](["+","-"])?> <E : ["e","E"](["+","-"])?>
| <DIGITS : (["0"-"9"])+> | <DIGITS : (["0"-"9"])+>
| <NULL : "null"> | <NULL : "null">
| <NAN : "NaN">
| <INFINITY : "Infinity">
| <BOOLEAN : ("true" | "false")> | <BOOLEAN : ("true" | "false")>
| <IDENTIFIER : ["a"-"z","A"-"Z", "_"] (["a"-"z","A"-"Z","0"-"9","_"])* > | <IDENTIFIER : ["a"-"z","A"-"Z", "_"] (["a"-"z","A"-"Z","0"-"9","_"])* >
| <#HEX_CHAR : ["a"-"f","A"-"F","0"-"9"]> | <#HEX_CHAR : ["a"-"f","A"-"F","0"-"9"]>
@ -161,9 +163,12 @@ private JsonPrimitive JsonNumber() :
String intpart = null, String intpart = null,
fracpart = null, fracpart = null,
exppart = null; exppart = null;
JsonPrimitive value;
} }
{ {
intpart=JsonInt() [ fracpart=JsonFrac() ] [ exppart=JsonExp() ] LOOKAHEAD(2)
(value=JsonSpecialNumbers()) {return value; } |
(intpart=JsonInt() [ fracpart=JsonFrac() ] [ exppart=JsonExp() ])
{ {
Number n; Number n;
if (exppart != null || fracpart != null) { if (exppart != null || fracpart != null) {
@ -177,6 +182,15 @@ private JsonPrimitive JsonNumber() :
} }
} }
private JsonPrimitive JsonSpecialNumbers() :
{
boolean negative = false;
}
{
(<NAN>) {return new JsonPrimitive(Double.NaN); } |
(["-" {negative = true;}]<INFINITY>) {return new JsonPrimitive(negative ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY); }
}
private String JsonInt() : private String JsonInt() :
{ {
String digits; String digits;

View File

@ -360,12 +360,8 @@ public class PrimitiveTest extends TestCase {
} }
public void testDoubleNaNDeserializationNotSupported() { public void testDoubleNaNDeserializationNotSupported() {
try { assertTrue(Double.isNaN(gson.fromJson("NaN", Double.class)));
gson.fromJson("NaN", Double.class); assertTrue(Double.isNaN(gson.fromJson("NaN", double.class)));
gson.fromJson("NaN", double.class);
fail("Gson should not accept NaN for deserialization");
} catch (JsonParseException expected) {
}
} }
public void testFloatNaNSerializationNotSupported() { public void testFloatNaNSerializationNotSupported() {
float nan = (float) Float.NaN; float nan = (float) Float.NaN;
@ -378,12 +374,8 @@ public class PrimitiveTest extends TestCase {
} }
public void testFloatNaNDeserializationNotSupported() { public void testFloatNaNDeserializationNotSupported() {
try { assertTrue(Float.isNaN(gson.fromJson("NaN", Float.class)));
gson.fromJson("NaN", Float.class); assertTrue(Float.isNaN(gson.fromJson("NaN", float.class)));
gson.fromJson("NaN", float.class);
fail("Gson should not accept NaN for deserialization");
} catch (JsonParseException expected) {
}
} }
public void testBigDecimalNaNDeserializationNotSupported() { public void testBigDecimalNaNDeserializationNotSupported() {
@ -405,12 +397,8 @@ public class PrimitiveTest extends TestCase {
} }
public void testDoubleInfinityDeserializationNotSupported() { public void testDoubleInfinityDeserializationNotSupported() {
try { assertTrue(Double.isInfinite(gson.fromJson("Infinity", Double.class)));
gson.fromJson("Infinity", Double.class); assertTrue(Double.isInfinite(gson.fromJson("Infinity", double.class)));
gson.fromJson("Infinity", double.class);
fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) {
}
} }
public void testFloatInfinitySerializationNotSupported() { public void testFloatInfinitySerializationNotSupported() {
@ -424,12 +412,8 @@ public class PrimitiveTest extends TestCase {
} }
public void testFloatInfinityDeserializationNotSupported() { public void testFloatInfinityDeserializationNotSupported() {
try { assertTrue(Float.isInfinite(gson.fromJson("Infinity", Float.class)));
gson.fromJson("Infinity", Float.class); assertTrue(Float.isInfinite(gson.fromJson("Infinity", float.class)));
gson.fromJson("Infinity", float.class);
fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) {
}
} }
public void testBigDecimalInfinityDeserializationNotSupported() { public void testBigDecimalInfinityDeserializationNotSupported() {
@ -451,12 +435,8 @@ public class PrimitiveTest extends TestCase {
} }
public void testNegativeInfinityDeserializationNotSupported() { public void testNegativeInfinityDeserializationNotSupported() {
try { assertTrue(Double.isInfinite(gson.fromJson("-Infinity", double.class)));
gson.fromJson("-Infinity", double.class); assertTrue(Double.isInfinite(gson.fromJson("-Infinity", Double.class)));
gson.fromJson("-Infinity", Double.class);
fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) {
}
} }
public void testNegativeInfinityFloatSerializationNotSupported() { public void testNegativeInfinityFloatSerializationNotSupported() {
@ -470,12 +450,8 @@ public class PrimitiveTest extends TestCase {
} }
public void testNegativeInfinityFloatDeserializationNotSupported() { public void testNegativeInfinityFloatDeserializationNotSupported() {
try { assertTrue(Float.isInfinite(gson.fromJson("-Infinity", float.class)));
gson.fromJson("-Infinity", float.class); assertTrue(Float.isInfinite(gson.fromJson("-Infinity", Float.class)));
gson.fromJson("-Infinity", Float.class);
fail("Gson should not accept positive infinity for deserialization");
} catch (JsonParseException expected) {
}
} }
public void testNegativeInfinityBigDecimalDeserializationNotSupported() { public void testNegativeInfinityBigDecimalDeserializationNotSupported() {