diff options
Diffstat (limited to 'src/unstable/json.cpp')
-rw-r--r-- | src/unstable/json.cpp | 364 |
1 files changed, 310 insertions, 54 deletions
diff --git a/src/unstable/json.cpp b/src/unstable/json.cpp index c31c7f9..0b47da4 100644 --- a/src/unstable/json.cpp +++ b/src/unstable/json.cpp | |||
@@ -1,5 +1,8 @@ | |||
1 | #include "bu/json.h" | 1 | #include "bu/json.h" |
2 | #include "bu/staticmembuf.h" | 2 | #include "bu/staticmembuf.h" |
3 | #include "bu/membuf.h" | ||
4 | |||
5 | #include "bu/sio.h" | ||
3 | 6 | ||
4 | #include <stdlib.h> | 7 | #include <stdlib.h> |
5 | 8 | ||
@@ -23,46 +26,146 @@ Bu::Json::Json( Bu::Stream &sInput ) : | |||
23 | parse( sInput ); | 26 | parse( sInput ); |
24 | } | 27 | } |
25 | 28 | ||
29 | Bu::Json::Json( char &c, Bu::Stream &sInput ) : | ||
30 | eType( Invalid ) | ||
31 | { | ||
32 | parse( c, sInput ); | ||
33 | } | ||
34 | |||
26 | Bu::Json::~Json() | 35 | Bu::Json::~Json() |
27 | { | 36 | { |
28 | reset(); | 37 | reset(); |
29 | } | 38 | } |
30 | 39 | ||
40 | Bu::Json::Type Bu::Json::getType() const | ||
41 | { | ||
42 | return eType; | ||
43 | } | ||
44 | |||
45 | Bu::String Bu::Json::getString() const | ||
46 | { | ||
47 | if( eType != String ) | ||
48 | throw Bu::ExceptionBase( | ||
49 | "String requested from non-string json object." | ||
50 | ); | ||
51 | |||
52 | return *uDat.pString; | ||
53 | } | ||
54 | |||
55 | double Bu::Json::getNumber() const | ||
56 | { | ||
57 | if( eType != Number ) | ||
58 | throw Bu::ExceptionBase( | ||
59 | "Number requested from non-number json object." | ||
60 | ); | ||
61 | |||
62 | return uDat.dNumber; | ||
63 | } | ||
64 | |||
65 | bool Bu::Json::getBoolean() const | ||
66 | { | ||
67 | if( eType != Boolean ) | ||
68 | throw Bu::ExceptionBase( | ||
69 | "Boolean requested from non-boolean json object." | ||
70 | ); | ||
71 | |||
72 | return uDat.bBoolean; | ||
73 | } | ||
74 | |||
75 | bool Bu::Json::isNull() const | ||
76 | { | ||
77 | return eType == Null; | ||
78 | } | ||
79 | |||
80 | Bu::Json *Bu::Json::operator[]( const Bu::String &sKey ) const | ||
81 | { | ||
82 | if( eType != Boolean ) | ||
83 | throw Bu::ExceptionBase( | ||
84 | "Object entry requested from non-object json object." | ||
85 | ); | ||
86 | |||
87 | return uDat.pObject->get( sKey ); | ||
88 | |||
89 | } | ||
90 | |||
91 | int Bu::Json::getSize() const | ||
92 | { | ||
93 | if( eType == Object ) | ||
94 | return uDat.pObject->getSize(); | ||
95 | else if( eType == Array ) | ||
96 | return uDat.pArray->getSize(); | ||
97 | else if( eType == String ) | ||
98 | return uDat.pString->getSize(); | ||
99 | else | ||
100 | throw Bu::ExceptionBase( | ||
101 | "Size requseted from json type that doesn't support it." | ||
102 | ); | ||
103 | } | ||
104 | |||
105 | Bu::Json::iterator Bu::Json::begin() | ||
106 | { | ||
107 | return uDat.pArray->begin(); | ||
108 | } | ||
109 | |||
110 | Bu::Json::const_iterator Bu::Json::begin() const | ||
111 | { | ||
112 | return uDat.pArray->begin(); | ||
113 | } | ||
114 | |||
115 | Bu::Json::iterator Bu::Json::end() | ||
116 | { | ||
117 | return uDat.pArray->end(); | ||
118 | } | ||
119 | |||
120 | Bu::Json::const_iterator Bu::Json::end() const | ||
121 | { | ||
122 | return uDat.pArray->end(); | ||
123 | } | ||
124 | |||
31 | void Bu::Json::parse( Bu::Stream &sInput ) | 125 | void Bu::Json::parse( Bu::Stream &sInput ) |
32 | { | 126 | { |
33 | reset(); | 127 | reset(); |
34 | 128 | ||
35 | char c; | 129 | char c; |
36 | for(;;) | 130 | next("json"); |
131 | |||
132 | parse( c, sInput ); | ||
133 | } | ||
134 | |||
135 | void Bu::Json::parse( char &c, Bu::Stream &sInput ) | ||
136 | { | ||
137 | while( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) | ||
37 | { | 138 | { |
38 | next( "json" ); | 139 | next( "json" ); |
39 | if( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) | 140 | } |
40 | continue; | 141 | if( c == '"' ) |
41 | if( c == '"' ) | 142 | { |
42 | { | 143 | // String |
43 | // String | 144 | parseString( c, sInput ); |
44 | parseString( c, sInput ); | 145 | } |
45 | } | 146 | else if( c == '{' ) |
46 | else if( c == '{' ) | 147 | { |
47 | { | 148 | // Object |
48 | // Object | 149 | parseObject( c, sInput ); |
49 | parseObject( c, sInput ); | 150 | } |
50 | } | 151 | else if( c == '[' ) |
51 | else if( c == '[' ) | 152 | { |
52 | { | 153 | // Array |
53 | // Array | 154 | parseArray( c, sInput ); |
54 | parseArray( c, sInput ); | 155 | } |
55 | } | 156 | else if( c == '-' || (c >= '0' && c <= '9') ) |
56 | else if( c == '-' || (c >= '0' && c <= '9') ) | 157 | { |
57 | { | 158 | // Number -- apparently they can't start with a period |
58 | // Number -- apparently they can't start with a period | 159 | parseNumber( c, sInput ); |
59 | parseNumber( c, sInput ); | 160 | } |
60 | } | 161 | else if( c == 't' || c == 'f' || c == 'n' ) |
61 | else if( c == 't' || c == 'f' || c == 'n' ) | 162 | { |
62 | { | 163 | // True / false / null |
63 | // True / false / null | 164 | parseLiteral( c, sInput ); |
64 | parseLiteral( c, sInput ); | 165 | } |
65 | } | 166 | else |
167 | { | ||
168 | throw Bu::ExceptionBase("Invalid characters in json stream."); | ||
66 | } | 169 | } |
67 | } | 170 | } |
68 | 171 | ||
@@ -99,12 +202,112 @@ void Bu::Json::reset() | |||
99 | uDat.pObject = NULL; | 202 | uDat.pObject = NULL; |
100 | } | 203 | } |
101 | 204 | ||
102 | void Bu::Json::parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ) | 205 | void Bu::Json::write( Bu::Stream &sOutput ) const |
103 | { | 206 | { |
104 | while( c != '"' ) | 207 | switch( eType ) |
105 | { | 208 | { |
106 | next( "string" ); | 209 | case Invalid: |
210 | throw Bu::ExceptionBase("Invalid type in json"); | ||
211 | break; | ||
212 | |||
213 | case Object: | ||
214 | { | ||
215 | sOutput.write("{", 1 ); | ||
216 | bool bFirst = true; | ||
217 | for( JsonHash::iterator i = uDat.pObject->begin(); i; i++ ) | ||
218 | { | ||
219 | if( bFirst == true ) | ||
220 | bFirst = false; | ||
221 | else | ||
222 | sOutput.write(","); | ||
223 | writeStr( i.getKey(), sOutput ); | ||
224 | sOutput.write(":", 1 ); | ||
225 | (*i)->write( sOutput ); | ||
226 | } | ||
227 | sOutput.write("}", 1 ); | ||
228 | } | ||
229 | break; | ||
230 | |||
231 | case Array: | ||
232 | { | ||
233 | sOutput.write("[", 1); | ||
234 | bool bFirst = true; | ||
235 | for( JsonList::iterator i = uDat.pArray->begin(); i; i++ ) | ||
236 | { | ||
237 | if( bFirst == true ) | ||
238 | bFirst = false; | ||
239 | else | ||
240 | sOutput.write(",", 1 ); | ||
241 | (*i)->write( sOutput ); | ||
242 | } | ||
243 | sOutput.write("]", 1); | ||
244 | } | ||
245 | break; | ||
246 | |||
247 | case String: | ||
248 | writeStr( *uDat.pString, sOutput ); | ||
249 | break; | ||
250 | |||
251 | case Number: | ||
252 | sOutput.write(Bu::String("%1").arg( uDat.dNumber )); | ||
253 | break; | ||
254 | |||
255 | case Boolean: | ||
256 | if( uDat.bBoolean ) | ||
257 | sOutput.write("true", 4 ); | ||
258 | else | ||
259 | sOutput.write("false", 5 ); | ||
260 | break; | ||
261 | |||
262 | case Null: | ||
263 | sOutput.write("null", 4 ); | ||
264 | break; | ||
107 | } | 265 | } |
266 | } | ||
267 | |||
268 | void Bu::Json::writeStable( Bu::Stream &sOutput ) const | ||
269 | { | ||
270 | if( eType == Object ) | ||
271 | { | ||
272 | sOutput.write("{", 1 ); | ||
273 | bool bFirst = true; | ||
274 | Bu::List<Bu::String> lKey = uDat.pObject->getKeys(); | ||
275 | lKey.sort(); | ||
276 | for( Bu::List<Bu::String>::iterator i = lKey.begin(); i; i++ ) | ||
277 | { | ||
278 | if( bFirst == true ) | ||
279 | bFirst = false; | ||
280 | else | ||
281 | sOutput.write(","); | ||
282 | writeStr( *i, sOutput ); | ||
283 | sOutput.write(":", 1 ); | ||
284 | uDat.pObject->get( *i )->write( sOutput ); | ||
285 | } | ||
286 | sOutput.write("}", 1 ); | ||
287 | } | ||
288 | else | ||
289 | { | ||
290 | write( sOutput ); | ||
291 | } | ||
292 | } | ||
293 | |||
294 | Bu::String Bu::Json::toString() const | ||
295 | { | ||
296 | Bu::MemBuf mb; | ||
297 | write( mb ); | ||
298 | return mb.getString(); | ||
299 | } | ||
300 | |||
301 | Bu::String Bu::Json::toStringStable() const | ||
302 | { | ||
303 | Bu::MemBuf mb; | ||
304 | writeStable( mb ); | ||
305 | return mb.getString(); | ||
306 | } | ||
307 | |||
308 | void Bu::Json::parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ) | ||
309 | { | ||
310 | skipWs( c, sInput ); | ||
108 | bool bEscape = false; | 311 | bool bEscape = false; |
109 | for(;;) | 312 | for(;;) |
110 | { | 313 | { |
@@ -157,6 +360,7 @@ void Bu::Json::parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ) | |||
157 | bEscape = true; | 360 | bEscape = true; |
158 | if( c == '"' ) | 361 | if( c == '"' ) |
159 | { | 362 | { |
363 | readChar( c, sInput ); | ||
160 | break; | 364 | break; |
161 | } | 365 | } |
162 | sOut += c; | 366 | sOut += c; |
@@ -173,11 +377,7 @@ void Bu::Json::parseString( char &c, Bu::Stream &sInput ) | |||
173 | 377 | ||
174 | void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) | 378 | void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) |
175 | { | 379 | { |
176 | while( c != '{' ) | 380 | skipWs( c, sInput ); |
177 | { | ||
178 | next( "object" ); | ||
179 | } | ||
180 | |||
181 | eType = Object; | 381 | eType = Object; |
182 | uDat.pObject = new JsonHash(); | 382 | uDat.pObject = new JsonHash(); |
183 | 383 | ||
@@ -186,17 +386,21 @@ void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) | |||
186 | { | 386 | { |
187 | Bu::String sKey; | 387 | Bu::String sKey; |
188 | parseString( c, sInput, sKey ); | 388 | parseString( c, sInput, sKey ); |
189 | next( "object" ); | 389 | skipWs( c, sInput ); |
190 | if( c != ':' ) | 390 | if( c != ':' ) |
191 | { | 391 | { |
192 | throw Bu::ExceptionBase( | 392 | throw Bu::ExceptionBase( |
193 | "Invalid json, expected colon after key in object." | 393 | "Invalid json, expected colon after key in object." |
194 | ); | 394 | ); |
195 | } | 395 | } |
196 | uDat.pObject->insert( sKey, new Json( sInput ) ); | 396 | next("object"); |
197 | next( "object" ); | 397 | uDat.pObject->insert( sKey, new Json( c, sInput ) ); |
398 | skipWs( c, sInput ); | ||
198 | if( c == '}' ) | 399 | if( c == '}' ) |
400 | { | ||
401 | readChar( c, sInput ); | ||
199 | break; | 402 | break; |
403 | } | ||
200 | else if( c == ',' ) | 404 | else if( c == ',' ) |
201 | next( "object" ); | 405 | next( "object" ); |
202 | else | 406 | else |
@@ -208,23 +412,27 @@ void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) | |||
208 | 412 | ||
209 | void Bu::Json::parseArray( char &c, Bu::Stream &sInput ) | 413 | void Bu::Json::parseArray( char &c, Bu::Stream &sInput ) |
210 | { | 414 | { |
211 | while( c != '[' ) | 415 | skipWs( c, sInput ); |
212 | { | ||
213 | next( "array" ); | ||
214 | } | ||
215 | 416 | ||
216 | eType = Array; | 417 | eType = Array; |
217 | uDat.pArray = new JsonList(); | 418 | uDat.pArray = new JsonList(); |
218 | 419 | ||
219 | next( "array" ); | 420 | next("array"); |
421 | |||
220 | for(;;) | 422 | for(;;) |
221 | { | 423 | { |
222 | uDat.pArray->append( new Json( sInput ) ); | 424 | uDat.pArray->append( new Json( c, sInput ) ); |
223 | next( "array" ); | 425 | skipWs( c, sInput ); |
224 | if( c == ']' ) | 426 | if( c == ']' ) |
427 | { | ||
428 | readChar( c, sInput ); | ||
225 | break; | 429 | break; |
430 | } | ||
226 | else if( c == ',' ) | 431 | else if( c == ',' ) |
432 | { | ||
433 | next("array"); | ||
227 | continue; | 434 | continue; |
435 | } | ||
228 | else | 436 | else |
229 | { | 437 | { |
230 | throw Bu::ExceptionBase( | 438 | throw Bu::ExceptionBase( |
@@ -236,10 +444,7 @@ void Bu::Json::parseArray( char &c, Bu::Stream &sInput ) | |||
236 | 444 | ||
237 | void Bu::Json::parseNumber( char &c, Bu::Stream &sInput ) | 445 | void Bu::Json::parseNumber( char &c, Bu::Stream &sInput ) |
238 | { | 446 | { |
239 | while( c != '-' && c < '0' && c > '9' ) | 447 | skipWs( c, sInput ); |
240 | { | ||
241 | next( "number" ); | ||
242 | } | ||
243 | 448 | ||
244 | Bu::String sBuf; | 449 | Bu::String sBuf; |
245 | if( c == '-' ) | 450 | if( c == '-' ) |
@@ -274,10 +479,7 @@ void Bu::Json::parseNumber( char &c, Bu::Stream &sInput ) | |||
274 | 479 | ||
275 | void Bu::Json::parseLiteral( char &c, Bu::Stream &sInput ) | 480 | void Bu::Json::parseLiteral( char &c, Bu::Stream &sInput ) |
276 | { | 481 | { |
277 | while( c != 'f' && c != 't' && c != 'n' ) | 482 | skipWs( c, sInput ); |
278 | { | ||
279 | next( "literal" ); | ||
280 | } | ||
281 | 483 | ||
282 | Bu::String s; | 484 | Bu::String s; |
283 | do | 485 | do |
@@ -329,3 +531,57 @@ bool Bu::Json::isWs( char c ) | |||
329 | return c == ' ' || c == '\t' || c == '\r' || c == '\n'; | 531 | return c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
330 | } | 532 | } |
331 | 533 | ||
534 | void Bu::Json::skipWs( char &c, Bu::Stream &sInput ) | ||
535 | { | ||
536 | while( isWs( c ) ) | ||
537 | { | ||
538 | next("whitespace"); | ||
539 | } | ||
540 | } | ||
541 | |||
542 | void Bu::Json::writeStr( const Bu::String &sStr, Bu::Stream &sOutput ) const | ||
543 | { | ||
544 | sOutput.write("\"", 1 ); | ||
545 | for( Bu::String::const_iterator i = sStr.begin(); i; i++ ) | ||
546 | { | ||
547 | switch( *i ) | ||
548 | { | ||
549 | case '"': | ||
550 | sOutput.write("\\\"", 2 ); | ||
551 | break; | ||
552 | |||
553 | case '\\': | ||
554 | sOutput.write("\\\\", 2 ); | ||
555 | break; | ||
556 | |||
557 | case '/': | ||
558 | sOutput.write("\\/", 2 ); | ||
559 | break; | ||
560 | |||
561 | case '\b': | ||
562 | sOutput.write("\\b", 2 ); | ||
563 | break; | ||
564 | |||
565 | case '\f': | ||
566 | sOutput.write("\\f", 2 ); | ||
567 | break; | ||
568 | |||
569 | case '\n': | ||
570 | sOutput.write("\\n", 2 ); | ||
571 | break; | ||
572 | |||
573 | case '\r': | ||
574 | sOutput.write("\\r", 2 ); | ||
575 | break; | ||
576 | |||
577 | case '\t': | ||
578 | sOutput.write("\\t", 2 ); | ||
579 | break; | ||
580 | |||
581 | default: | ||
582 | sOutput.write( &(*i), 1 ); | ||
583 | } | ||
584 | } | ||
585 | sOutput.write("\"", 1 ); | ||
586 | } | ||
587 | |||