Skip to content

Commit 1f78e26

Browse files
author
fangyidong
committed
1.0.2 get faster lexer
git-svn-id: http://json-simple.googlecode.com/svn/trunk@73 b68fe964-5755-0410-a06c-9fee2ea08261
1 parent 188858b commit 1f78e26

File tree

10 files changed

+89
-61
lines changed

10 files changed

+89
-61
lines changed

AUTHORS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fang Yidong <fangyidong@yahoo.com.cn>
1+
Fang Yidong <fangyidong@gmail.com>

ChangeLog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
ChangeLog
22

3+
Version 1.02 (2009/01/10)
4+
* Updated json.lex to improve performance of the lexer
5+
* Removed Rope.java and related junit test
6+
37
Version 1.01 (2008/08/26)
48
* License changed to a more commerce friendly and clear one, Apache License 2.0
59
* Use JFlex to generate a faster Yylex.java

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.1
1+
1.0.2

build.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<project name="json-simple" default="main" basedir=".">
2-
<property name="current-version" value="1.0.1"/>
2+
<property name="current-version" value="1.0.2"/>
33
<property name="jar-name" value="json_simple-${current-version}.jar"/>
44

55
<target name="main" depends="mkdir,compile,jar">
@@ -29,4 +29,4 @@
2929
basedir="build/main"
3030
includes="**/*.class"/>
3131
</target>
32-
</project>
32+
</project>

doc/json.lex

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.json.simple.parser;
22

3-
import org.json.simple.rope.Rope;
4-
53
%%
64

75
%{
8-
private Rope sb=new Rope();
6+
private StringBuffer sb=new StringBuffer();
97

108
%}
119

@@ -17,8 +15,11 @@ HEX_D = [a-fA-F0-9]
1715
INT = [-]?[0-9]+
1816
DOUBLE = {INT}((\.[0-9]+)?([eE][-+]?[0-9]+)?)
1917
WS = [ \t\r\n]
18+
UNESCAPED_CH = [^\"\\]
2019
%%
2120

21+
<STRING_BEGIN> \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE,sb.toString());}
22+
<STRING_BEGIN> {UNESCAPED_CH}+ { sb.append(yytext());}
2223
<STRING_BEGIN> \\\" {sb.append('"');}
2324
<STRING_BEGIN> \\\\ {sb.append('\\');}
2425
<STRING_BEGIN> \\\/ {sb.append('/');}
@@ -30,10 +31,8 @@ WS = [ \t\r\n]
3031
<STRING_BEGIN> \\u{HEX_D}{HEX_D}{HEX_D}{HEX_D} { int ch=Integer.parseInt(yytext().substring(2),16);
3132
sb.append((char)ch);
3233
}
33-
34-
<STRING_BEGIN> \" { yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE,sb.toString());}
35-
<STRING_BEGIN> . { sb.append(yytext());}
36-
<YYINITIAL> \" { sb.clear();yybegin(STRING_BEGIN);}
34+
35+
<YYINITIAL> \" { sb.delete(0, sb.length());yybegin(STRING_BEGIN);}
3736
<YYINITIAL> {INT} { Long val=Long.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);}
3837
<YYINITIAL> {DOUBLE} { Double val=Double.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);}
3938
<YYINITIAL> "true"|"false" { Boolean val=Boolean.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE,val);}
@@ -44,4 +43,4 @@ WS = [ \t\r\n]
4443
<YYINITIAL> "]" { return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE,null);}
4544
<YYINITIAL> "," { return new Yytoken(Yytoken.TYPE_COMMA,null);}
4645
<YYINITIAL> ":" { return new Yytoken(Yytoken.TYPE_COLON,null);}
47-
<YYINITIAL> {WS} {}
46+
<YYINITIAL> {WS}+ {}

lib/json_simple-1.0.2.jar

9.51 KB
Binary file not shown.

src/org/json/simple/parser/JSONParser.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,30 @@ public class JSONParser {
2222
public static final int S_PASSED_PAIR_KEY=4;
2323
public static final int S_IN_ERROR=-1;
2424

25+
private LinkedList statusStack = new LinkedList();
26+
private LinkedList valueStack = new LinkedList();
27+
private Yylex lexer = new Yylex((Reader)null);
28+
private Yytoken token = null;
29+
private int status = S_INIT;
30+
2531
private int peekStatus(LinkedList statusStack){
2632
if(statusStack.size()==0)
2733
return -1;
2834
Integer status=(Integer)statusStack.getFirst();
2935
return status.intValue();
3036
}
3137

38+
private void reset(Reader in) throws Exception{
39+
statusStack.clear();
40+
valueStack.clear();
41+
lexer.yyreset(in);
42+
token = null;
43+
status = S_INIT;
44+
}
45+
3246
public Object parse(Reader in) throws Exception{
33-
LinkedList statusStack=new LinkedList();
34-
LinkedList valueStack=new LinkedList();
35-
Yylex lexer=new Yylex(in);
36-
Yytoken token=null;
37-
int status=S_INIT;
38-
47+
reset(in);
48+
3949
try{
4050
do{
4151
token=lexer.yylex();

src/org/json/simple/parser/Yylex.java

Lines changed: 50 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
<formatter type="plain"/>
4444
<test name="org.json.simple.Test"/>
4545
<test name="org.json.simple.parser.YylexTest"/>
46-
<test name="org.json.simple.rope.RopeTest"/>
4746
</junit>
4847
</target>
49-
</project>
48+
</project>

test/org/json/simple/Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package org.json.simple;
66

7+
import java.util.List;
8+
79
import junit.framework.TestCase;
810

911

@@ -39,6 +41,11 @@ public void testDecode() throws Exception{
3941
s="[5,,2]";
4042
obj=JSONValue.parse(s);
4143
assertEquals("[5,2]",obj.toString());
44+
45+
s="[\"hello\\bworld\\\"abc\\tdef\\\\ghi\\rjkl\\n123\\u4e2d\"]";
46+
obj=JSONValue.parse(s);
47+
assertEquals("hello\bworld\"abc\tdef\\ghi\rjkl\n123中",((List)obj).get(0).toString());
48+
4249
}
4350

4451
public void testEncode() throws Exception{

0 commit comments

Comments
 (0)