annotate src/goodjava/json/JsonParser.java @ 1474:13cbce740e1e

LowercaseAnalyzer
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 17 Apr 2020 21:53:09 -0600
parents 27efb1fcbcb5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1307
diff changeset
1 package goodjava.json;
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.List;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.ArrayList;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Map;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.LinkedHashMap;
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
7 import java.util.Collections;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1307
diff changeset
8 import goodjava.parser.Parser;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1307
diff changeset
9 import goodjava.parser.ParseException;
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
12 public final class JsonParser {
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 public static Object parse(String text) throws ParseException {
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
15 return new JsonParser(text).parse();
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 private final Parser parser;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
20 private JsonParser(String text) {
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this.parser = new Parser(text);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 private ParseException exception(String msg) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return new ParseException(parser,msg);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 private Object parse() throws ParseException {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 Object value = value();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if( !parser.endOfInput() )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 throw exception("unexpected text");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return value;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 private Object value() throws ParseException {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 if( parser.match("null") )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 return null;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 if( parser.match("true") )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 return Boolean.TRUE;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 if( parser.match("false") )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 return Boolean.FALSE;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 String s = string();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 if( s != null )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 return s;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 Number n = number();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 if( n != null )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 return n;
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
50 List a = array();
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 if( a != null )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 return a;
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
53 Map o = object();
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 if( o != null )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 return o;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 throw exception("invalid value");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 private String string() throws ParseException {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 parser.begin();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 if( !parser.match('"') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 return parser.failure(null);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 StringBuilder sb = new StringBuilder();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 while( parser.anyChar() ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 char c = parser.lastChar();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 switch(c) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 case '"':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 return parser.success(sb.toString());
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 case '\\':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 if( parser.anyChar() ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 c = parser.lastChar();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 switch(c) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 case '"':
1307
6c8ceca4a10b json fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1112
diff changeset
74 case '\'': // not in spec
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 case '\\':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 case '/':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 sb.append(c);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 case 'b':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 sb.append('\b');
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 case 'f':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 sb.append('\f');
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 case 'n':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 sb.append('\n');
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 case 'r':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 sb.append('\r');
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 case 't':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 sb.append('\t');
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 case 'u':
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 int n = 0;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 for( int i=0; i<4; i++ ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 int d;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 if( parser.inCharRange('0','9') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 d = parser.lastChar() - '0';
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 } else if( parser.inCharRange('a','f') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 d = parser.lastChar() - 'a' + 10;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 } else if( parser.inCharRange('A','F') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 d = parser.lastChar() - 'A' + 10;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 } else {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 throw exception("invalid hex digit");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 n = 16*n + d;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 sb.append((char)n);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 continue;
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 throw exception("invalid escape char");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 default:
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 sb.append(c);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 parser.failure();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 throw exception("unclosed string");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 private Number number() {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 int start = parser.begin();
759
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
124 boolean isFloat = false;
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 parser.match('-');
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 if( !parser.match('0') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 if( !parser.inCharRange('1','9') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 return parser.failure(null);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 while( parser.inCharRange('0','9') );
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 if( parser.match('.') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 if( !parser.inCharRange('0','9') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 return parser.failure(null);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 while( parser.inCharRange('0','9') );
759
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
135 isFloat = true;
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 if( parser.anyOf("eE") ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 parser.anyOf("+-");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 if( !parser.inCharRange('0','9') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 return parser.failure(null);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 while( parser.inCharRange('0','9') );
759
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
142 isFloat = true;
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 String s = parser.textFrom(start);
759
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
145 Number n;
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
146 if(isFloat)
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
147 n = Double.valueOf(s);
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
148 else
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
149 n = Long.valueOf(s);
ae612dfc57cb better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents: 758
diff changeset
150 return parser.success(n);
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
153 private List array() throws ParseException {
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 parser.begin();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 if( !parser.match('[') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 return parser.failure(null);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 if( parser.match(']') )
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
159 return parser.success(Collections.emptyList());
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 List list = new ArrayList();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 list.add( value() );
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 while( parser.match(',') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 list.add( value() );
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 if( parser.match(']') )
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
169 return parser.success(list);
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 if( parser.endOfInput() ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 parser.failure();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 throw exception("unclosed array");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 throw exception("unexpected text in array");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
177 private Map object() throws ParseException {
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 parser.begin();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 if( !parser.match('{') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 return parser.failure(null);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 if( parser.match('}') )
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
183 return parser.success(Collections.emptyMap());
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 Map map = new LinkedHashMap();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 addEntry(map);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 while( parser.match(',') ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 addEntry(map);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 if( parser.match('}') )
1112
490f77bb2ad1 add JsonParser
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
191 return parser.success(map);
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 if( parser.endOfInput() ) {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 parser.failure();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 throw exception("unclosed object");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 throw exception("unexpected text in object");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 private void addEntry(Map map) throws ParseException {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 String key = string();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 if( key==null )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 throw exception("invalid object key");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 if( !parser.match(':') )
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 throw exception("':' expected");
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 Object value = value();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 spaces();
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 map.put(key,value);
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 }
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212 private void spaces() {
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 while( parser.anyOf(" \t\r\n") );
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 }
758
c29d11d675fd added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents: 720
diff changeset
215
720
bdd766df1c17 add json parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 }