diff --git a/AUTHORS.txt b/AUTHORS.txt index f5ff495..e896fc1 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -1 +1 @@ -Fang Yidong \ No newline at end of file +Fang Yidong \ No newline at end of file diff --git a/ChangeLog.txt b/ChangeLog.txt index 1030c68..fa4dbb2 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,5 +1,9 @@ ChangeLog +Version 1.02 (2009/01/10) +* Updated json.lex to improve performance of the lexer +* Removed Rope.java and related junit test + Version 1.01 (2008/08/26) * License changed to a more commerce friendly and clear one, Apache License 2.0 * Use JFlex to generate a faster Yylex.java diff --git a/VERSION.txt b/VERSION.txt index 7f20734..e6d5cb8 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -1.0.1 \ No newline at end of file +1.0.2 \ No newline at end of file diff --git a/build.xml b/build.xml index b7acbc7..72b1c75 100644 --- a/build.xml +++ b/build.xml @@ -1,5 +1,5 @@ - + @@ -29,4 +29,4 @@ basedir="build/main" includes="**/*.class"/> - \ No newline at end of file + diff --git a/doc/json.lex b/doc/json.lex index ec5f05c..53fcf1b 100644 --- a/doc/json.lex +++ b/doc/json.lex @@ -1,11 +1,9 @@ package org.json.simple.parser; -import org.json.simple.rope.Rope; - %% %{ -private Rope sb=new Rope(); +private StringBuffer sb=new StringBuffer(); %} @@ -17,8 +15,11 @@ HEX_D = [a-fA-F0-9] INT = [-]?[0-9]+ DOUBLE = {INT}((\.[0-9]+)?([eE][-+]?[0-9]+)?) WS = [ \t\r\n] +UNESCAPED_CH = [^\"\\] %% + \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE,sb.toString());} + {UNESCAPED_CH}+ { sb.append(yytext());} \\\" {sb.append('"');} \\\\ {sb.append('\\');} \\\/ {sb.append('/');} @@ -30,10 +31,8 @@ WS = [ \t\r\n] \\u{HEX_D}{HEX_D}{HEX_D}{HEX_D} { int ch=Integer.parseInt(yytext().substring(2),16); sb.append((char)ch); } - - \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE,sb.toString());} - . { sb.append(yytext());} - \" { sb.clear();yybegin(STRING_BEGIN);} + + \" { sb.delete(0, sb.length());yybegin(STRING_BEGIN);} {INT} { Long val=Long.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);} {DOUBLE} { Double val=Double.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);} "true"|"false" { Boolean val=Boolean.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);} @@ -44,4 +43,4 @@ WS = [ \t\r\n] "]" { return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE,null);} "," { return new Yytoken(Yytoken.TYPE_COMMA,null);} ":" { return new Yytoken(Yytoken.TYPE_COLON,null);} - {WS} {} + {WS}+ {} diff --git a/lib/json_simple-1.0.2.jar b/lib/json_simple-1.0.2.jar new file mode 100644 index 0000000..73b66bd Binary files /dev/null and b/lib/json_simple-1.0.2.jar differ diff --git a/src/org/json/simple/parser/JSONParser.java b/src/org/json/simple/parser/JSONParser.java index e58d276..392adfc 100644 --- a/src/org/json/simple/parser/JSONParser.java +++ b/src/org/json/simple/parser/JSONParser.java @@ -22,6 +22,12 @@ public class JSONParser { public static final int S_PASSED_PAIR_KEY=4; public static final int S_IN_ERROR=-1; + private LinkedList statusStack = new LinkedList(); + private LinkedList valueStack = new LinkedList(); + private Yylex lexer = new Yylex((Reader)null); + private Yytoken token = null; + private int status = S_INIT; + private int peekStatus(LinkedList statusStack){ if(statusStack.size()==0) return -1; @@ -29,13 +35,17 @@ public class JSONParser { return status.intValue(); } + private void reset(Reader in) throws Exception{ + statusStack.clear(); + valueStack.clear(); + lexer.yyreset(in); + token = null; + status = S_INIT; + } + public Object parse(Reader in) throws Exception{ - LinkedList statusStack=new LinkedList(); - LinkedList valueStack=new LinkedList(); - Yylex lexer=new Yylex(in); - Yytoken token=null; - int status=S_INIT; - + reset(in); + try{ do{ token=lexer.yylex(); diff --git a/src/org/json/simple/parser/Yylex.java b/src/org/json/simple/parser/Yylex.java index 229670d..a9d103d 100644 --- a/src/org/json/simple/parser/Yylex.java +++ b/src/org/json/simple/parser/Yylex.java @@ -1,9 +1,7 @@ -/* The following code was generated by JFlex 1.4.2 on 2008-11-25 */ +/* The following code was generated by JFlex 1.4.2 */ package org.json.simple.parser; -import org.json.simple.rope.Rope; - class Yylex { /** This character denotes the end of file */ @@ -30,11 +28,11 @@ class Yylex { * Translates characters to character classes */ private static final String ZZ_CMAP_PACKED = - "\11\0\1\7\1\21\2\0\1\7\22\0\1\7\1\0\1\11\10\0"+ - "\1\6\1\32\1\2\1\4\1\12\12\3\1\33\6\0\4\1\1\5"+ - "\1\1\24\0\1\30\1\10\1\31\3\0\1\23\1\13\2\1\1\22"+ - "\1\14\5\0\1\24\1\0\1\15\3\0\1\16\1\25\1\17\1\20"+ - "\5\0\1\26\1\0\1\27\uff82\0"; + "\11\0\2\7\2\0\1\7\22\0\1\7\1\0\1\11\10\0\1\6"+ + "\1\31\1\2\1\4\1\12\12\3\1\32\6\0\4\1\1\5\1\1"+ + "\24\0\1\27\1\10\1\30\3\0\1\22\1\13\2\1\1\21\1\14"+ + "\5\0\1\23\1\0\1\15\3\0\1\16\1\24\1\17\1\20\5\0"+ + "\1\25\1\0\1\26\uff82\0"; /** * Translates characters to character classes @@ -48,9 +46,9 @@ class Yylex { private static final String ZZ_ACTION_PACKED_0 = "\3\0\1\1\1\2\1\3\3\0\1\4\1\5\1\6"+ - "\1\7\1\10\1\11\2\12\1\13\5\0\1\14\1\15"+ - "\1\16\1\17\1\20\1\21\1\22\1\23\1\0\1\24"+ - "\1\0\1\24\4\0\1\25\1\26\2\0\1\27"; + "\1\7\1\10\1\11\1\12\1\0\1\13\5\0\1\14"+ + "\1\15\1\16\1\17\1\20\1\21\1\22\1\23\1\0"+ + "\1\24\1\0\1\24\4\0\1\25\1\26\2\0\1\27"; private static int [] zzUnpackAction() { int [] result = new int[44]; @@ -131,7 +129,7 @@ class Yylex { private boolean zzAtEOF; /* user code: */ -private Rope sb=new Rope(); +private StringBuffer sb=new StringBuffer(); @@ -165,7 +163,7 @@ private Rope sb=new Rope(); char [] map = new char[0x10000]; int i = 0; /* index in packed string */ int j = 0; /* index in unpacked array */ - while (i < 90) { + while (i < 88) { int count = packed.charAt(i++); char value = packed.charAt(i++); do map[j++] = value; while (--count > 0); @@ -420,27 +418,25 @@ private Rope sb=new Rope(); switch (zzInput) { case 2: zzState = 2; break zzForNext; case 3: zzIsFinal = true; zzState = 3; break zzForNext; - case 7: - case 17: zzIsFinal = true; zzNoLookAhead = true; zzState = 4; break zzForNext; + case 7: zzIsFinal = true; zzState = 4; break zzForNext; case 9: zzIsFinal = true; zzNoLookAhead = true; zzState = 5; break zzForNext; case 12: zzState = 6; break zzForNext; case 13: zzState = 7; break zzForNext; case 15: zzState = 8; break zzForNext; - case 22: zzIsFinal = true; zzNoLookAhead = true; zzState = 9; break zzForNext; - case 23: zzIsFinal = true; zzNoLookAhead = true; zzState = 10; break zzForNext; - case 24: zzIsFinal = true; zzNoLookAhead = true; zzState = 11; break zzForNext; - case 25: zzIsFinal = true; zzNoLookAhead = true; zzState = 12; break zzForNext; - case 26: zzIsFinal = true; zzNoLookAhead = true; zzState = 13; break zzForNext; - case 27: zzIsFinal = true; zzNoLookAhead = true; zzState = 14; break zzForNext; + case 21: zzIsFinal = true; zzNoLookAhead = true; zzState = 9; break zzForNext; + case 22: zzIsFinal = true; zzNoLookAhead = true; zzState = 10; break zzForNext; + case 23: zzIsFinal = true; zzNoLookAhead = true; zzState = 11; break zzForNext; + case 24: zzIsFinal = true; zzNoLookAhead = true; zzState = 12; break zzForNext; + case 25: zzIsFinal = true; zzNoLookAhead = true; zzState = 13; break zzForNext; + case 26: zzIsFinal = true; zzNoLookAhead = true; zzState = 14; break zzForNext; default: break zzForAction; } case 1: switch (zzInput) { - case 8: zzIsFinal = true; zzState = 16; break zzForNext; + case 8: zzState = 16; break zzForNext; case 9: zzIsFinal = true; zzNoLookAhead = true; zzState = 17; break zzForNext; - case 17: break zzForAction; - default: zzIsFinal = true; zzNoLookAhead = true; zzState = 15; break zzForNext; + default: zzIsFinal = true; zzState = 15; break zzForNext; } case 2: @@ -454,13 +450,19 @@ private Rope sb=new Rope(); case 3: zzIsFinal = true; break zzForNext; case 4: zzState = 18; break zzForNext; case 5: - case 18: zzState = 19; break zzForNext; + case 17: zzState = 19; break zzForNext; + default: break zzForAction; + } + + case 4: + switch (zzInput) { + case 7: zzIsFinal = true; break zzForNext; default: break zzForAction; } case 6: switch (zzInput) { - case 19: zzState = 20; break zzForNext; + case 18: zzState = 20; break zzForNext; default: break zzForAction; } @@ -476,6 +478,13 @@ private Rope sb=new Rope(); default: break zzForAction; } + case 15: + switch (zzInput) { + case 8: + case 9: break zzForAction; + default: zzIsFinal = true; break zzForNext; + } + case 16: switch (zzInput) { case 8: zzIsFinal = true; zzNoLookAhead = true; zzState = 23; break zzForNext; @@ -506,13 +515,13 @@ private Rope sb=new Rope(); case 20: switch (zzInput) { - case 20: zzState = 35; break zzForNext; + case 19: zzState = 35; break zzForNext; default: break zzForAction; } case 21: switch (zzInput) { - case 20: zzState = 36; break zzForNext; + case 19: zzState = 36; break zzForNext; default: break zzForAction; } @@ -529,15 +538,15 @@ private Rope sb=new Rope(); case 5: case 11: case 12: - case 18: - case 19: zzState = 38; break zzForNext; + case 17: + case 18: zzState = 38; break zzForNext; default: break zzForAction; } case 32: switch (zzInput) { case 5: - case 18: zzState = 19; break zzForNext; + case 17: zzState = 19; break zzForNext; case 3: zzIsFinal = true; break zzForNext; default: break zzForAction; } @@ -556,19 +565,19 @@ private Rope sb=new Rope(); case 35: switch (zzInput) { - case 21: zzState = 37; break zzForNext; + case 20: zzState = 37; break zzForNext; default: break zzForAction; } case 36: switch (zzInput) { - case 20: zzIsFinal = true; zzNoLookAhead = true; zzState = 39; break zzForNext; + case 19: zzIsFinal = true; zzNoLookAhead = true; zzState = 39; break zzForNext; default: break zzForAction; } case 37: switch (zzInput) { - case 18: zzIsFinal = true; zzNoLookAhead = true; zzState = 40; break zzForNext; + case 17: zzIsFinal = true; zzNoLookAhead = true; zzState = 40; break zzForNext; default: break zzForAction; } @@ -579,8 +588,8 @@ private Rope sb=new Rope(); case 5: case 11: case 12: - case 18: - case 19: zzState = 41; break zzForNext; + case 17: + case 18: zzState = 41; break zzForNext; default: break zzForAction; } @@ -591,8 +600,8 @@ private Rope sb=new Rope(); case 5: case 11: case 12: - case 18: - case 19: zzState = 42; break zzForNext; + case 17: + case 18: zzState = 42; break zzForNext; default: break zzForAction; } @@ -603,8 +612,8 @@ private Rope sb=new Rope(); case 5: case 11: case 12: - case 18: - case 19: zzIsFinal = true; zzNoLookAhead = true; zzState = 43; break zzForNext; + case 17: + case 18: zzIsFinal = true; zzNoLookAhead = true; zzState = 43; break zzForNext; default: break zzForAction; } @@ -632,7 +641,7 @@ private Rope sb=new Rope(); } case 24: break; case 3: - { sb.clear();yybegin(STRING_BEGIN); + { sb.delete(0, sb.length());yybegin(STRING_BEGIN); } case 25: break; case 15: diff --git a/test.xml b/test.xml index 2265e9f..1af55bf 100644 --- a/test.xml +++ b/test.xml @@ -43,7 +43,6 @@ - - \ No newline at end of file + diff --git a/test/org/json/simple/Test.java b/test/org/json/simple/Test.java index be2ed57..0b44f68 100644 --- a/test/org/json/simple/Test.java +++ b/test/org/json/simple/Test.java @@ -4,6 +4,8 @@ */ package org.json.simple; +import java.util.List; + import junit.framework.TestCase; @@ -39,6 +41,11 @@ public class Test extends TestCase{ s="[5,,2]"; obj=JSONValue.parse(s); assertEquals("[5,2]",obj.toString()); + + s="[\"hello\\bworld\\\"abc\\tdef\\\\ghi\\rjkl\\n123\\u4e2d\"]"; + obj=JSONValue.parse(s); + assertEquals("hello\bworld\"abc\tdef\\ghi\rjkl\n123δΈ­",((List)obj).get(0).toString()); + } public void testEncode() throws Exception{