Fix for Issue 55 where GSON was unable to deserialize JSON with single quotes

(') used for field name/values. Gson now supports different fields with-in the
JSON to use either single quote or double quote. However, a single field name
or value must use ' or "" to delimit itself.
This commit is contained in:
Inderjeet Singh 2008-10-10 02:53:54 +00:00
parent 427c17a732
commit 1abf693b70
5 changed files with 239 additions and 154 deletions

View File

@ -18,20 +18,21 @@ final class JsonParser implements JsonParserConstants {
final public JsonElement parse() throws ParseException {
JsonElement json = null;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 15:
case 17:
json = JsonObject();
break;
case 20:
case 22:
json = JsonArray();
break;
case DIGITS:
case SINGLE_QUOTE_LITERAL:
case QUOTE:
case 22:
case 23:
case 24:
case 25:
case 26:
json = JsonPrimitive();
break;
case 17:
case 19:
json = JsonNull();
break;
default:
@ -45,8 +46,9 @@ final class JsonParser implements JsonParserConstants {
final private JsonObject JsonObject() throws ParseException {
JsonObject o = new JsonObject();
jj_consume_token(15);
jj_consume_token(17);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SINGLE_QUOTE_LITERAL:
case QUOTE:
Members(o);
break;
@ -54,14 +56,14 @@ final class JsonParser implements JsonParserConstants {
jj_la1[1] = jj_gen;
;
}
jj_consume_token(16);
jj_consume_token(18);
{if (true) return o;}
throw new Error("Missing return statement in function");
}
final private JsonNull JsonNull() throws ParseException {
JsonNull json = new JsonNull();
jj_consume_token(17);
jj_consume_token(19);
{if (true) return json;}
throw new Error("Missing return statement in function");
}
@ -69,8 +71,8 @@ final class JsonParser implements JsonParserConstants {
final private void Members(JsonObject o) throws ParseException {
Pair(o);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 18:
jj_consume_token(18);
case 20:
jj_consume_token(20);
Members(o);
break;
default:
@ -83,30 +85,31 @@ final class JsonParser implements JsonParserConstants {
JsonPrimitive property;
JsonElement value;
property = JsonString();
jj_consume_token(19);
jj_consume_token(21);
value = JsonValue();
o.add(property.getAsString(), value);
}
final private JsonArray JsonArray() throws ParseException {
JsonArray array = new JsonArray();
jj_consume_token(20);
jj_consume_token(22);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case DIGITS:
case SINGLE_QUOTE_LITERAL:
case QUOTE:
case 15:
case 17:
case 20:
case 19:
case 22:
case 23:
case 24:
case 25:
case 26:
Elements(array);
break;
default:
jj_la1[3] = jj_gen;
;
}
jj_consume_token(21);
jj_consume_token(23);
array.reverse();
{if (true) return array;}
throw new Error("Missing return statement in function");
@ -116,8 +119,8 @@ final class JsonParser implements JsonParserConstants {
JsonElement element;
element = JsonValue();
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 18:
jj_consume_token(18);
case 20:
jj_consume_token(20);
Elements(array);
break;
default:
@ -130,29 +133,30 @@ final class JsonParser implements JsonParserConstants {
final private JsonElement JsonValue() throws ParseException {
JsonElement o = null;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SINGLE_QUOTE_LITERAL:
case QUOTE:
o = JsonString();
break;
case DIGITS:
case 24:
case 26:
o = JsonNumber();
break;
case 15:
case 17:
o = JsonObject();
break;
case 20:
case 22:
o = JsonArray();
break;
case 22:
jj_consume_token(22);
case 24:
jj_consume_token(24);
o = new JsonPrimitive(true);
break;
case 23:
jj_consume_token(23);
case 25:
jj_consume_token(25);
o = new JsonPrimitive(false);
break;
case 17:
jj_consume_token(17);
case 19:
jj_consume_token(19);
break;
default:
jj_la1[5] = jj_gen;
@ -166,21 +170,22 @@ final class JsonParser implements JsonParserConstants {
final private JsonPrimitive JsonPrimitive() throws ParseException {
JsonPrimitive value;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SINGLE_QUOTE_LITERAL:
case QUOTE:
value = JsonString();
{if (true) return value;}
break;
case DIGITS:
case 24:
case 26:
value = JsonNumber();
{if (true) return value;}
break;
case 22:
jj_consume_token(22);
case 24:
jj_consume_token(24);
{if (true) return new JsonPrimitive(true);}
break;
case 23:
jj_consume_token(23);
case 25:
jj_consume_token(25);
{if (true) return new JsonPrimitive(false);}
break;
default:
@ -197,7 +202,7 @@ final class JsonParser implements JsonParserConstants {
exppart = null;
intpart = JsonInt();
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 25:
case 27:
fracpart = JsonFrac();
break;
default:
@ -236,8 +241,8 @@ final class JsonParser implements JsonParserConstants {
String digits;
boolean negative = false;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 24:
jj_consume_token(24);
case 26:
jj_consume_token(26);
negative = true;
break;
default:
@ -253,7 +258,7 @@ final class JsonParser implements JsonParserConstants {
final private String JsonFrac() throws ParseException {
String digits;
jj_consume_token(25);
jj_consume_token(27);
digits = Digits();
{if (true) return "." + digits;}
throw new Error("Missing return statement in function");
@ -276,20 +281,34 @@ final class JsonParser implements JsonParserConstants {
}
final private JsonPrimitive JsonString() throws ParseException {
StringBuffer strbuf = new StringBuffer();
jj_consume_token(QUOTE);
StringBuffer strbuf = new StringBuffer(); Token t;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CHAR:
case CNTRL_ESC:
case HEX_ESC:
Chars(strbuf);
case SINGLE_QUOTE_LITERAL:
t = jj_consume_token(SINGLE_QUOTE_LITERAL);
String value = t.image;
String valueWithInQuotes = value.substring(1, value.length()-1);
{if (true) return new JsonPrimitive(valueWithInQuotes);}
break;
case QUOTE:
jj_consume_token(QUOTE);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CHAR:
case CNTRL_ESC:
case HEX_ESC:
Chars(strbuf);
break;
default:
jj_la1[10] = jj_gen;
;
}
jj_consume_token(ENDQUOTE);
{if (true) return new JsonPrimitive(strbuf.toString());}
break;
default:
jj_la1[10] = jj_gen;
;
jj_la1[11] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jj_consume_token(ENDQUOTE);
{if (true) return new JsonPrimitive(strbuf.toString());}
throw new Error("Missing return statement in function");
}
@ -303,7 +322,7 @@ final class JsonParser implements JsonParserConstants {
Chars(strbuf);
break;
default:
jj_la1[11] = jj_gen;
jj_la1[12] = jj_gen;
;
}
strbuf.insert(0, c);
@ -322,7 +341,7 @@ final class JsonParser implements JsonParserConstants {
t = jj_consume_token(HEX_ESC);
break;
default:
jj_la1[12] = jj_gen;
jj_la1[13] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
@ -355,13 +374,13 @@ final class JsonParser implements JsonParserConstants {
public Token token, jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[13];
final private int[] jj_la1 = new int[14];
static private int[] jj_la1_0;
static {
jj_la1_0();
}
private static void jj_la1_0() {
jj_la1_0 = new int[] {0x1d280c0,0x80,0x40000,0x1d280c0,0x40000,0x1d280c0,0x1c000c0,0x2000000,0x20,0x1000000,0x4c00,0x4c00,0x4c00,};
jj_la1_0 = new int[] {0x74a0340,0x300,0x100000,0x74a0340,0x100000,0x74a0340,0x7000340,0x8000000,0x20,0x4000000,0x13000,0x300,0x13000,0x13000,};
}
public JsonParser(java.io.InputStream stream) {
@ -373,7 +392,7 @@ final class JsonParser implements JsonParserConstants {
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 13; i++) jj_la1[i] = -1;
for (int i = 0; i < 14; i++) jj_la1[i] = -1;
}
public void ReInit(java.io.InputStream stream) {
@ -385,7 +404,7 @@ final class JsonParser implements JsonParserConstants {
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 13; i++) jj_la1[i] = -1;
for (int i = 0; i < 14; i++) jj_la1[i] = -1;
}
public JsonParser(java.io.Reader stream) {
@ -394,7 +413,7 @@ final class JsonParser implements JsonParserConstants {
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 13; i++) jj_la1[i] = -1;
for (int i = 0; i < 14; i++) jj_la1[i] = -1;
}
public void ReInit(java.io.Reader stream) {
@ -403,7 +422,7 @@ final class JsonParser implements JsonParserConstants {
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 13; i++) jj_la1[i] = -1;
for (int i = 0; i < 14; i++) jj_la1[i] = -1;
}
public JsonParser(JsonParserTokenManager tm) {
@ -411,7 +430,7 @@ final class JsonParser implements JsonParserConstants {
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 13; i++) jj_la1[i] = -1;
for (int i = 0; i < 14; i++) jj_la1[i] = -1;
}
public void ReInit(JsonParserTokenManager tm) {
@ -419,7 +438,7 @@ final class JsonParser implements JsonParserConstants {
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 13; i++) jj_la1[i] = -1;
for (int i = 0; i < 14; i++) jj_la1[i] = -1;
}
final private Token jj_consume_token(int kind) throws ParseException {
@ -466,15 +485,15 @@ final class JsonParser implements JsonParserConstants {
public ParseException generateParseException() {
jj_expentries.removeAllElements();
boolean[] la1tokens = new boolean[26];
for (int i = 0; i < 26; i++) {
boolean[] la1tokens = new boolean[28];
for (int i = 0; i < 28; i++) {
la1tokens[i] = false;
}
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 13; i++) {
for (int i = 0; i < 14; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<<j)) != 0) {
@ -483,7 +502,7 @@ final class JsonParser implements JsonParserConstants {
}
}
}
for (int i = 0; i < 26; i++) {
for (int i = 0; i < 28; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;

View File

@ -7,12 +7,14 @@ interface JsonParserConstants {
int EOF = 0;
int E = 5;
int DIGITS = 6;
int QUOTE = 7;
int ENDQUOTE = 9;
int CHAR = 10;
int CNTRL_ESC = 11;
int HEX = 13;
int HEX_ESC = 14;
int ESCAPE_CHAR = 7;
int SINGLE_QUOTE_LITERAL = 8;
int QUOTE = 9;
int ENDQUOTE = 11;
int CHAR = 12;
int CNTRL_ESC = 13;
int HEX = 15;
int HEX_ESC = 16;
int DEFAULT = 0;
int STRING_STATE = 1;
@ -27,6 +29,8 @@ interface JsonParserConstants {
"\"\\r\"",
"<E>",
"<DIGITS>",
"<ESCAPE_CHAR>",
"<SINGLE_QUOTE_LITERAL>",
"\"\\\"\"",
"\"\\\\\"",
"<ENDQUOTE>",

View File

@ -80,8 +80,8 @@ private final int jjMoveNfa_3(int startState, int curPos)
jjstateSet[jjnewStateCnt++] = 3;
break;
case 3:
if ((0x3ff000000000000L & l) != 0L && kind > 14)
kind = 14;
if ((0x3ff000000000000L & l) != 0L && kind > 16)
kind = 16;
break;
default : break;
}
@ -107,8 +107,8 @@ private final int jjMoveNfa_3(int startState, int curPos)
jjstateSet[jjnewStateCnt++] = 3;
break;
case 3:
if ((0x7e0000007eL & l) != 0L && kind > 14)
kind = 14;
if ((0x7e0000007eL & l) != 0L && kind > 16)
kind = 16;
break;
default : break;
}
@ -173,29 +173,29 @@ private final int jjMoveStringLiteralDfa0_0()
switch(curChar)
{
case 34:
return jjStopAtPos(0, 7);
return jjStopAtPos(0, 9);
case 44:
return jjStopAtPos(0, 18);
case 45:
return jjStopAtPos(0, 24);
case 46:
return jjStopAtPos(0, 25);
case 58:
return jjStopAtPos(0, 19);
case 91:
return jjStopAtPos(0, 20);
case 93:
case 45:
return jjStopAtPos(0, 26);
case 46:
return jjStopAtPos(0, 27);
case 58:
return jjStopAtPos(0, 21);
case 91:
return jjStopAtPos(0, 22);
case 93:
return jjStopAtPos(0, 23);
case 102:
return jjMoveStringLiteralDfa1_0(0x800000L);
return jjMoveStringLiteralDfa1_0(0x2000000L);
case 110:
return jjMoveStringLiteralDfa1_0(0x20000L);
return jjMoveStringLiteralDfa1_0(0x80000L);
case 116:
return jjMoveStringLiteralDfa1_0(0x400000L);
return jjMoveStringLiteralDfa1_0(0x1000000L);
case 123:
return jjStopAtPos(0, 15);
return jjStopAtPos(0, 17);
case 125:
return jjStopAtPos(0, 16);
return jjStopAtPos(0, 18);
default :
return jjMoveNfa_0(0, 0);
}
@ -210,11 +210,11 @@ private final int jjMoveStringLiteralDfa1_0(long active0)
switch(curChar)
{
case 97:
return jjMoveStringLiteralDfa2_0(active0, 0x800000L);
return jjMoveStringLiteralDfa2_0(active0, 0x2000000L);
case 114:
return jjMoveStringLiteralDfa2_0(active0, 0x400000L);
return jjMoveStringLiteralDfa2_0(active0, 0x1000000L);
case 117:
return jjMoveStringLiteralDfa2_0(active0, 0x20000L);
return jjMoveStringLiteralDfa2_0(active0, 0x80000L);
default :
break;
}
@ -223,7 +223,7 @@ private final int jjMoveStringLiteralDfa1_0(long active0)
private final int jjMoveStringLiteralDfa2_0(long old0, long active0)
{
if (((active0 &= old0)) == 0L)
return jjStartNfa_0(0, old0);
return jjStartNfa_0(0, old0);
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) {
jjStopStringLiteralDfa_0(1, active0);
@ -232,9 +232,9 @@ private final int jjMoveStringLiteralDfa2_0(long old0, long active0)
switch(curChar)
{
case 108:
return jjMoveStringLiteralDfa3_0(active0, 0x820000L);
return jjMoveStringLiteralDfa3_0(active0, 0x2080000L);
case 117:
return jjMoveStringLiteralDfa3_0(active0, 0x400000L);
return jjMoveStringLiteralDfa3_0(active0, 0x1000000L);
default :
break;
}
@ -243,7 +243,7 @@ private final int jjMoveStringLiteralDfa2_0(long old0, long active0)
private final int jjMoveStringLiteralDfa3_0(long old0, long active0)
{
if (((active0 &= old0)) == 0L)
return jjStartNfa_0(1, old0);
return jjStartNfa_0(1, old0);
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) {
jjStopStringLiteralDfa_0(2, active0);
@ -252,15 +252,15 @@ private final int jjMoveStringLiteralDfa3_0(long old0, long active0)
switch(curChar)
{
case 101:
if ((active0 & 0x400000L) != 0L)
return jjStopAtPos(3, 22);
if ((active0 & 0x1000000L) != 0L)
return jjStopAtPos(3, 24);
break;
case 108:
if ((active0 & 0x20000L) != 0L)
return jjStopAtPos(3, 17);
if ((active0 & 0x80000L) != 0L)
return jjStopAtPos(3, 19);
break;
case 115:
return jjMoveStringLiteralDfa4_0(active0, 0x800000L);
return jjMoveStringLiteralDfa4_0(active0, 0x2000000L);
default :
break;
}
@ -269,7 +269,7 @@ private final int jjMoveStringLiteralDfa3_0(long old0, long active0)
private final int jjMoveStringLiteralDfa4_0(long old0, long active0)
{
if (((active0 &= old0)) == 0L)
return jjStartNfa_0(2, old0);
return jjStartNfa_0(2, old0);
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) {
jjStopStringLiteralDfa_0(3, active0);
@ -278,19 +278,25 @@ private final int jjMoveStringLiteralDfa4_0(long old0, long active0)
switch(curChar)
{
case 101:
if ((active0 & 0x800000L) != 0L)
return jjStopAtPos(4, 23);
if ((active0 & 0x2000000L) != 0L)
return jjStopAtPos(4, 25);
break;
default :
break;
}
return jjStartNfa_0(3, active0);
}
static final long[] jjbitVec0 = {
0xfffffffffffffffeL, 0xffffffffffffffffL, 0xffffffffffffffffL, 0xffffffffffffffffL
};
static final long[] jjbitVec2 = {
0x0L, 0x0L, 0xffffffffffffffffL, 0xffffffffffffffffL
};
private final int jjMoveNfa_0(int startState, int curPos)
{
int[] nextStates;
int startsAt = 0;
jjnewStateCnt = 3;
jjnewStateCnt = 8;
int i = 1;
jjstateSet[0] = startState;
int j, kind = 0x7fffffff;
@ -306,15 +312,41 @@ private final int jjMoveNfa_0(int startState, int curPos)
switch(jjstateSet[--i])
{
case 0:
if ((0x3ff000000000000L & l) != 0L)
{
if (kind > 6)
kind = 6;
jjCheckNAdd(2);
}
else if (curChar == 39)
jjCheckNAddStates(0, 2);
break;
case 1:
if ((0x280000000000L & l) != 0L && kind > 5)
kind = 5;
break;
case 2:
if ((0x3ff000000000000L & l) == 0L)
break;
kind = 6;
if (kind > 6)
kind = 6;
jjCheckNAdd(2);
break;
case 1:
if ((0x280000000000L & l) != 0L)
kind = 5;
case 3:
if (curChar == 39)
jjCheckNAddStates(0, 2);
break;
case 4:
if ((0xffffff7fffffdbffL & l) != 0L)
jjCheckNAddStates(0, 2);
break;
case 6:
if ((0x8400000000L & l) != 0L)
jjCheckNAddStates(0, 2);
break;
case 7:
if (curChar == 39 && kind > 8)
kind = 8;
break;
default : break;
}
@ -330,9 +362,22 @@ private final int jjMoveNfa_0(int startState, int curPos)
case 0:
if ((0x2000000020L & l) == 0L)
break;
kind = 5;
if (kind > 5)
kind = 5;
jjstateSet[jjnewStateCnt++] = 1;
break;
case 4:
if ((0xffffffffefffffffL & l) != 0L)
jjCheckNAddStates(0, 2);
break;
case 5:
if (curChar == 92)
jjstateSet[jjnewStateCnt++] = 6;
break;
case 6:
if ((0x14404410000000L & l) != 0L)
jjCheckNAddStates(0, 2);
break;
default : break;
}
} while(i != startsAt);
@ -348,6 +393,10 @@ private final int jjMoveNfa_0(int startState, int curPos)
{
switch(jjstateSet[--i])
{
case 4:
if (jjCanMove_0(hiByte, i1, i2, l1, l2))
jjAddStates(0, 2);
break;
default : break;
}
} while(i != startsAt);
@ -359,7 +408,7 @@ private final int jjMoveNfa_0(int startState, int curPos)
kind = 0x7fffffff;
}
++curPos;
if ((i = jjnewStateCnt) == (startsAt = 3 - (jjnewStateCnt = startsAt)))
if ((i = jjnewStateCnt) == (startsAt = 8 - (jjnewStateCnt = startsAt)))
return curPos;
try { curChar = input_stream.readChar(); }
catch(java.io.IOException e) { return curPos; }
@ -390,7 +439,7 @@ private final int jjMoveStringLiteralDfa0_2()
switch(curChar)
{
case 117:
return jjStopAtPos(0, 12);
return jjStopAtPos(0, 14);
default :
return jjMoveNfa_2(0, 0);
}
@ -416,7 +465,7 @@ private final int jjMoveNfa_2(int startState, int curPos)
{
case 0:
if ((0x800400000000L & l) != 0L)
kind = 11;
kind = 13;
break;
default : break;
}
@ -431,7 +480,7 @@ private final int jjMoveNfa_2(int startState, int curPos)
{
case 0:
if ((0x14404410000000L & l) != 0L)
kind = 11;
kind = 13;
break;
default : break;
}
@ -490,17 +539,11 @@ private final int jjMoveStringLiteralDfa0_1()
switch(curChar)
{
case 92:
return jjStopAtPos(0, 8);
return jjStopAtPos(0, 10);
default :
return jjMoveNfa_1(0, 0);
}
}
static final long[] jjbitVec0 = {
0xfffffffffffffffeL, 0xffffffffffffffffL, 0xffffffffffffffffL, 0xffffffffffffffffL
};
static final long[] jjbitVec2 = {
0x0L, 0x0L, 0xffffffffffffffffL, 0xffffffffffffffffL
};
private final int jjMoveNfa_1(int startState, int curPos)
{
int[] nextStates;
@ -523,18 +566,18 @@ private final int jjMoveNfa_1(int startState, int curPos)
case 0:
if ((0xfffffffbffffffffL & l) != 0L)
{
if (kind > 10)
kind = 10;
if (kind > 12)
kind = 12;
}
else if (curChar == 34)
{
if (kind > 9)
kind = 9;
if (kind > 11)
kind = 11;
}
break;
case 1:
if ((0xfffffffbffffffffL & l) != 0L)
kind = 10;
kind = 12;
break;
default : break;
}
@ -549,7 +592,7 @@ private final int jjMoveNfa_1(int startState, int curPos)
{
case 0:
if ((0xffffffffefffffffL & l) != 0L)
kind = 10;
kind = 12;
break;
default : break;
}
@ -567,8 +610,8 @@ private final int jjMoveNfa_1(int startState, int curPos)
switch(jjstateSet[--i])
{
case 0:
if (jjCanMove_0(hiByte, i1, i2, l1, l2) && kind > 10)
kind = 10;
if (jjCanMove_0(hiByte, i1, i2, l1, l2) && kind > 12)
kind = 12;
break;
default : break;
}
@ -588,6 +631,7 @@ private final int jjMoveNfa_1(int startState, int curPos)
}
}
static final int[] jjnextStates = {
4, 5, 7,
};
private static final boolean jjCanMove_0(int hiByte, int i1, int i2, long l1, long l2)
{
@ -595,38 +639,38 @@ private static final boolean jjCanMove_0(int hiByte, int i1, int i2, long l1, lo
{
case 0:
return ((jjbitVec2[i2] & l2) != 0L);
default :
default :
if ((jjbitVec0[i1] & l1) != 0L)
return true;
return false;
}
}
public static final String[] jjstrLiteralImages = {
"", null, null, null, null, null, null, "\42", null, null, null, null, null,
null, null, "\173", "\175", "\156\165\154\154", "\54", "\72", "\133", "\135",
"\164\162\165\145", "\146\141\154\163\145", "\55", "\56", };
"", null, null, null, null, null, null, null, null, "\42", null, null, null,
null, null, null, null, "\173", "\175", "\156\165\154\154", "\54", "\72", "\133",
"\135", "\164\162\165\145", "\146\141\154\163\145", "\55", "\56", };
public static final String[] lexStateNames = {
"DEFAULT",
"STRING_STATE",
"ESC_STATE",
"HEX_STATE",
"DEFAULT",
"STRING_STATE",
"ESC_STATE",
"HEX_STATE",
};
public static final int[] jjnewLexState = {
-1, -1, -1, -1, -1, -1, -1, 1, 2, 0, -1, 1, 3, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 0, -1, 1, 3, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1,
};
static final long[] jjtoToken = {
0x3ffcee1L,
0xfff3b61L,
};
static final long[] jjtoSkip = {
0x1eL,
0x1eL,
};
static final long[] jjtoMore = {
0x1100L,
0x4400L,
};
protected SimpleCharStream input_stream;
private final int[] jjrounds = new int[4];
private final int[] jjstateSet = new int[8];
private final int[] jjrounds = new int[8];
private final int[] jjstateSet = new int[16];
protected char curChar;
public JsonParserTokenManager(SimpleCharStream stream){
if (SimpleCharStream.staticFlag)
@ -648,7 +692,7 @@ private final void ReInitRounds()
{
int i;
jjround = 0x80000001;
for (i = 4; i-- > 0;)
for (i = 8; i-- > 0;)
jjrounds[i] = 0x80000000;
}
public void ReInit(SimpleCharStream stream, int lexState)
@ -684,7 +728,7 @@ int jjround;
int jjmatchedPos;
int jjmatchedKind;
public Token getNextToken()
public Token getNextToken()
{
int kind;
Token specialToken = null;
@ -693,13 +737,13 @@ public Token getNextToken()
EOFLoop :
for (;;)
{
try
{
{
try
{
curChar = input_stream.BeginToken();
}
}
catch(java.io.IOException e)
{
{
jjmatchedKind = 0;
matchedToken = jjFillToken();
return matchedToken;

View File

@ -38,8 +38,10 @@ SKIP : { " " | "\t" | "\n" | "\r" }
*/
TOKEN : {
<E : ["e","E"](["+","-"])?>
| <DIGITS : (["0"-"9"])+>
| <QUOTE : "\""> : STRING_STATE
| <DIGITS : (["0"-"9"])+>
| <#ESCAPE_CHAR: "\\" ["n","t","b","r","f","\\","'","\""] >
| <SINGLE_QUOTE_LITERAL: "\'" ( (~["\'","\\","\n","\r"]) | <ESCAPE_CHAR>)* "\'" >
| <QUOTE : "\""> : STRING_STATE
}
<STRING_STATE> MORE : { "\\" : ESC_STATE }
<STRING_STATE> TOKEN : {
@ -214,10 +216,16 @@ private String Digits() :
}
private JsonPrimitive JsonString() :
{ StringBuffer strbuf = new StringBuffer(); }
{ StringBuffer strbuf = new StringBuffer(); Token t; }
{
<QUOTE> [ Chars(strbuf) ] <ENDQUOTE>
{ return new JsonPrimitive(strbuf.toString()); }
t=<SINGLE_QUOTE_LITERAL> {
String value = t.image;
String valueWithInQuotes = value.substring(1, value.length()-1);
return new JsonPrimitive(valueWithInQuotes);
}
| <QUOTE> [ Chars(strbuf) ] <ENDQUOTE> {
return new JsonPrimitive(strbuf.toString());
}
}
private void Chars(StringBuffer strbuf) :

View File

@ -56,10 +56,20 @@ public class ObjectTest extends TestCase {
gson = new Gson();
}
public void testJsonInSingleQuotes() {
String json = "{'stringValue':'no message'}";
public void testJsonInSingleQuotesDeserialization() {
String json = "{'stringValue':'no message','intValue':10,'longValue':20}";
BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
assertEquals("no message", target.stringValue);
assertEquals(10, target.intValue);
assertEquals(20, target.longValue);
}
public void testJsonInMixedQuotesDeserialization() {
String json = "{\"stringValue\":'no message','intValue':10,'longValue':20}";
BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
assertEquals("no message", target.stringValue);
assertEquals(10, target.intValue);
assertEquals(20, target.longValue);
}
public void testBagOfPrimitivesSerialization() throws Exception {