Mercurial Hosting > luan
annotate src/luan/modules/parsers/Json.java @ 1111:88b5b81cad4a
move Parser to luan.lib.parser
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Aug 2017 15:19:47 -0600 |
parents | 1a68fc55a80c |
children | 490f77bb2ad1 |
rev | line source |
---|---|
720 | 1 package luan.modules.parsers; |
2 | |
3 import java.util.List; | |
4 import java.util.ArrayList; | |
5 import java.util.Map; | |
6 import java.util.LinkedHashMap; | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
7 import java.util.Iterator; |
720 | 8 import luan.LuanTable; |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
9 import luan.LuanException; |
1111
88b5b81cad4a
move Parser to luan.lib.parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
10 import luan.lib.parser.Parser; |
88b5b81cad4a
move Parser to luan.lib.parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
11 import luan.lib.parser.ParseException; |
720 | 12 |
13 | |
14 public final class Json { | |
15 | |
16 public static Object parse(String text) throws ParseException { | |
17 return new Json(text).parse(); | |
18 } | |
19 | |
20 private final Parser parser; | |
21 | |
22 private Json(String text) { | |
23 this.parser = new Parser(text); | |
24 } | |
25 | |
26 private ParseException exception(String msg) { | |
27 return new ParseException(parser,msg); | |
28 } | |
29 | |
30 private Object parse() throws ParseException { | |
31 spaces(); | |
32 Object value = value(); | |
33 spaces(); | |
34 if( !parser.endOfInput() ) | |
35 throw exception("unexpected text"); | |
36 return value; | |
37 } | |
38 | |
39 private Object value() throws ParseException { | |
40 if( parser.match("null") ) | |
41 return null; | |
42 if( parser.match("true") ) | |
43 return Boolean.TRUE; | |
44 if( parser.match("false") ) | |
45 return Boolean.FALSE; | |
46 String s = string(); | |
47 if( s != null ) | |
48 return s; | |
49 Number n = number(); | |
50 if( n != null ) | |
51 return n; | |
52 LuanTable a = array(); | |
53 if( a != null ) | |
54 return a; | |
55 LuanTable o = object(); | |
56 if( o != null ) | |
57 return o; | |
58 throw exception("invalid value"); | |
59 } | |
60 | |
61 private String string() throws ParseException { | |
62 parser.begin(); | |
63 if( !parser.match('"') ) | |
64 return parser.failure(null); | |
65 StringBuilder sb = new StringBuilder(); | |
66 while( parser.anyChar() ) { | |
67 char c = parser.lastChar(); | |
68 switch(c) { | |
69 case '"': | |
70 return parser.success(sb.toString()); | |
71 case '\\': | |
72 if( parser.anyChar() ) { | |
73 c = parser.lastChar(); | |
74 switch(c) { | |
75 case '"': | |
76 case '\\': | |
77 case '/': | |
78 sb.append(c); | |
79 continue; | |
80 case 'b': | |
81 sb.append('\b'); | |
82 continue; | |
83 case 'f': | |
84 sb.append('\f'); | |
85 continue; | |
86 case 'n': | |
87 sb.append('\n'); | |
88 continue; | |
89 case 'r': | |
90 sb.append('\r'); | |
91 continue; | |
92 case 't': | |
93 sb.append('\t'); | |
94 continue; | |
95 case 'u': | |
96 int n = 0; | |
97 for( int i=0; i<4; i++ ) { | |
98 int d; | |
99 if( parser.inCharRange('0','9') ) { | |
100 d = parser.lastChar() - '0'; | |
101 } else if( parser.inCharRange('a','f') ) { | |
102 d = parser.lastChar() - 'a' + 10; | |
103 } else if( parser.inCharRange('A','F') ) { | |
104 d = parser.lastChar() - 'A' + 10; | |
105 } else { | |
106 throw exception("invalid hex digit"); | |
107 } | |
108 n = 16*n + d; | |
109 } | |
110 sb.append((char)n); | |
111 continue; | |
112 } | |
113 } | |
114 throw exception("invalid escape char"); | |
115 default: | |
116 sb.append(c); | |
117 } | |
118 } | |
119 parser.failure(); | |
120 throw exception("unclosed string"); | |
121 } | |
122 | |
123 private Number number() { | |
124 int start = parser.begin(); | |
759
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
125 boolean isFloat = false; |
720 | 126 parser.match('-'); |
127 if( !parser.match('0') ) { | |
128 if( !parser.inCharRange('1','9') ) | |
129 return parser.failure(null); | |
130 while( parser.inCharRange('0','9') ); | |
131 } | |
132 if( parser.match('.') ) { | |
133 if( !parser.inCharRange('0','9') ) | |
134 return parser.failure(null); | |
135 while( parser.inCharRange('0','9') ); | |
759
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
136 isFloat = true; |
720 | 137 } |
138 if( parser.anyOf("eE") ) { | |
139 parser.anyOf("+-"); | |
140 if( !parser.inCharRange('0','9') ) | |
141 return parser.failure(null); | |
142 while( parser.inCharRange('0','9') ); | |
759
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
143 isFloat = true; |
720 | 144 } |
145 String s = parser.textFrom(start); | |
759
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
146 Number n; |
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
147 if(isFloat) |
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
148 n = Double.valueOf(s); |
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
149 else |
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
150 n = Long.valueOf(s); |
ae612dfc57cb
better handling of longs in rpc and json
Franklin Schmidt <fschmidt@gmail.com>
parents:
758
diff
changeset
|
151 return parser.success(n); |
720 | 152 } |
153 | |
154 private LuanTable array() throws ParseException { | |
155 parser.begin(); | |
156 if( !parser.match('[') ) | |
157 return parser.failure(null); | |
158 spaces(); | |
159 if( parser.match(']') ) | |
160 return parser.success(new LuanTable()); | |
161 List list = new ArrayList(); | |
162 list.add( value() ); | |
163 spaces(); | |
164 while( parser.match(',') ) { | |
165 spaces(); | |
166 list.add( value() ); | |
167 spaces(); | |
168 } | |
169 if( parser.match(']') ) | |
170 return parser.success(new LuanTable(list)); | |
171 if( parser.endOfInput() ) { | |
172 parser.failure(); | |
173 throw exception("unclosed array"); | |
174 } | |
175 throw exception("unexpected text in array"); | |
176 } | |
177 | |
178 private LuanTable object() throws ParseException { | |
179 parser.begin(); | |
180 if( !parser.match('{') ) | |
181 return parser.failure(null); | |
182 spaces(); | |
183 if( parser.match('}') ) | |
184 return parser.success(new LuanTable()); | |
185 Map map = new LinkedHashMap(); | |
186 addEntry(map); | |
187 while( parser.match(',') ) { | |
188 spaces(); | |
189 addEntry(map); | |
190 } | |
191 if( parser.match('}') ) | |
192 return parser.success(new LuanTable(map)); | |
193 if( parser.endOfInput() ) { | |
194 parser.failure(); | |
195 throw exception("unclosed object"); | |
196 } | |
197 throw exception("unexpected text in object"); | |
198 } | |
199 | |
200 private void addEntry(Map map) throws ParseException { | |
201 String key = string(); | |
202 if( key==null ) | |
203 throw exception("invalid object key"); | |
204 spaces(); | |
205 if( !parser.match(':') ) | |
206 throw exception("':' expected"); | |
207 spaces(); | |
208 Object value = value(); | |
209 spaces(); | |
210 map.put(key,value); | |
211 } | |
212 | |
213 private void spaces() { | |
214 while( parser.anyOf(" \t\r\n") ); | |
215 } | |
758
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
216 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
217 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
218 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
219 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
220 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
221 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
222 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
223 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
224 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
225 public static String toString(Object obj) throws LuanException { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
226 StringBuilder sb = new StringBuilder(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
227 toString(obj,sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
228 return sb.toString(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
229 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
230 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
231 private static void toString(Object obj,StringBuilder sb) throws LuanException { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
232 if( obj == null || obj instanceof Boolean || obj instanceof Number ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
233 sb.append(obj); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
234 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
235 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
236 if( obj instanceof String ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
237 toString((String)obj,sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
238 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
239 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
240 if( obj instanceof LuanTable ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
241 toString((LuanTable)obj,sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
242 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
243 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
244 throw new LuanException("can't handle type "+obj.getClass().getName()); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
245 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
246 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
247 private static void toString(final String s,StringBuilder sb) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
248 sb.append('"'); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
249 for( int i=0; i<s.length(); i++ ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
250 char c = s.charAt(i); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
251 switch(c) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
252 case '"': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
253 sb.append("\\\""); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
254 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
255 case '\\': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
256 sb.append("\\\\"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
257 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
258 case '\b': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
259 sb.append("\\b"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
260 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
261 case '\f': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
262 sb.append("\\f"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
263 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
264 case '\n': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
265 sb.append("\\n"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
266 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
267 case '\r': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
268 sb.append("\\r"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
269 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
270 case '\t': |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
271 sb.append("\\t"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
272 break; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
273 default: |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
274 sb.append(c); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
275 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
276 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
277 sb.append('"'); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
278 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
279 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
280 private static void toString(LuanTable t,StringBuilder sb) throws LuanException { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
281 if( t.isList() ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
282 final List list = t.asList(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
283 if( list.isEmpty() ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
284 sb.append("{}"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
285 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
286 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
287 sb.append('['); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
288 toString(list.get(0),sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
289 for( int i=1; i<list.size(); i++ ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
290 sb.append(','); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
291 toString(list.get(i),sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
292 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
293 sb.append(']'); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
294 return; |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
295 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
296 sb.append('{'); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
297 Iterator<Map.Entry<Object,Object>> i = t.rawIterator(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
298 toString(i.next(),sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
299 while( i.hasNext() ) { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
300 sb.append(','); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
301 toString(i.next(),sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
302 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
303 sb.append('}'); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
304 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
305 |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
306 private static void toString(Map.Entry<Object,Object> entry,StringBuilder sb) throws LuanException { |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
307 Object key = entry.getKey(); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
308 if( !(key instanceof String) ) |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
309 throw new LuanException("table keys must be strings"); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
310 toString((String)key,sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
311 sb.append(':'); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
312 toString(entry.getValue(),sb); |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
313 } |
c29d11d675fd
added Json.toString() and rpc now sends tables as json
Franklin Schmidt <fschmidt@gmail.com>
parents:
720
diff
changeset
|
314 |
720 | 315 } |