diff options
author | Mike Buland <eichlan@xagasoft.com> | 2019-05-13 19:47:19 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2019-05-13 19:47:19 -0700 |
commit | d605d6c3c04c1e26121f9b1c5c1d2dbcc5f7bc37 (patch) | |
tree | 0cd21d420fc67ae757ec2475610c4624fd714363 /src/unstable/json.cpp | |
parent | 62753c815b5ec34ebfae37a3c89187a01cc17160 (diff) | |
download | libbu++-d605d6c3c04c1e26121f9b1c5c1d2dbcc5f7bc37.tar.gz libbu++-d605d6c3c04c1e26121f9b1c5c1d2dbcc5f7bc37.tar.bz2 libbu++-d605d6c3c04c1e26121f9b1c5c1d2dbcc5f7bc37.tar.xz libbu++-d605d6c3c04c1e26121f9b1c5c1d2dbcc5f7bc37.zip |
UtfString & Json overhaul.
UtfString supports a load of new stuff, and Json uses UtfString
exclusively now.
Diffstat (limited to 'src/unstable/json.cpp')
-rw-r--r-- | src/unstable/json.cpp | 95 |
1 files changed, 53 insertions, 42 deletions
diff --git a/src/unstable/json.cpp b/src/unstable/json.cpp index d7e84d9..b1414a9 100644 --- a/src/unstable/json.cpp +++ b/src/unstable/json.cpp | |||
@@ -15,7 +15,7 @@ Bu::Json::Json() : | |||
15 | 15 | ||
16 | Bu::Json::Json( const Bu::UtfString &sValue ) : | 16 | Bu::Json::Json( const Bu::UtfString &sValue ) : |
17 | eType( String ), | 17 | eType( String ), |
18 | uDat( sValue.get() ) | 18 | uDat( sValue ) |
19 | { | 19 | { |
20 | } | 20 | } |
21 | 21 | ||
@@ -57,7 +57,7 @@ Bu::Json::Json( Type eType ) : | |||
57 | break; | 57 | break; |
58 | 58 | ||
59 | case String: | 59 | case String: |
60 | uDat.pString = new Bu::String(); | 60 | uDat.pString = new Bu::UtfString(); |
61 | break; | 61 | break; |
62 | 62 | ||
63 | case Number: | 63 | case Number: |
@@ -75,7 +75,7 @@ Bu::Json::Json( Bu::Stream &sInput ) : | |||
75 | parse( sInput ); | 75 | parse( sInput ); |
76 | } | 76 | } |
77 | 77 | ||
78 | Bu::Json::Json( char &c, Bu::Stream &sInput ) : | 78 | Bu::Json::Json( Bu::UtfChar &c, Bu::Stream &sInput ) : |
79 | eType( Invalid ) | 79 | eType( Invalid ) |
80 | { | 80 | { |
81 | parse( c, sInput ); | 81 | parse( c, sInput ); |
@@ -97,7 +97,7 @@ Bu::Json::Type Bu::Json::getType() const | |||
97 | return eType; | 97 | return eType; |
98 | } | 98 | } |
99 | 99 | ||
100 | Bu::String Bu::Json::getString() const | 100 | Bu::UtfString Bu::Json::getString() const |
101 | { | 101 | { |
102 | if( eType != String ) | 102 | if( eType != String ) |
103 | throw Bu::ExceptionBase( | 103 | throw Bu::ExceptionBase( |
@@ -132,7 +132,7 @@ bool Bu::Json::isNull() const | |||
132 | return eType == Null; | 132 | return eType == Null; |
133 | } | 133 | } |
134 | 134 | ||
135 | Bu::Json &Bu::Json::operator[]( const Bu::String &sKey ) const | 135 | Bu::Json &Bu::Json::operator[]( const Bu::UtfString &sKey ) const |
136 | { | 136 | { |
137 | if( eType != Object ) | 137 | if( eType != Object ) |
138 | throw Bu::ExceptionBase( | 138 | throw Bu::ExceptionBase( |
@@ -158,15 +158,13 @@ int Bu::Json::getSize() const | |||
158 | return uDat.pObject->getSize(); | 158 | return uDat.pObject->getSize(); |
159 | else if( eType == Array ) | 159 | else if( eType == Array ) |
160 | return uDat.pArray->getSize(); | 160 | return uDat.pArray->getSize(); |
161 | else if( eType == String ) | ||
162 | return uDat.pString->getSize(); | ||
163 | else | 161 | else |
164 | throw Bu::ExceptionBase( | 162 | throw Bu::ExceptionBase( |
165 | "Size requseted from json type that doesn't support it." | 163 | "Size requseted from json type that doesn't support it." |
166 | ); | 164 | ); |
167 | } | 165 | } |
168 | 166 | ||
169 | Bu::StringList Bu::Json::getKeys() const | 167 | Bu::UtfStringList Bu::Json::getKeys() const |
170 | { | 168 | { |
171 | return uDat.pObject->getKeys(); | 169 | return uDat.pObject->getKeys(); |
172 | } | 170 | } |
@@ -196,33 +194,40 @@ bool Bu::Json::has( const Bu::String &sKey ) const | |||
196 | return uDat.pObject->has( sKey ); | 194 | return uDat.pObject->has( sKey ); |
197 | } | 195 | } |
198 | 196 | ||
199 | void Bu::Json::insert( const Bu::String &sKey, Bu::Json *pObj ) | 197 | Bu::Json &Bu::Json::insert( const Bu::String &sKey, Bu::Json *pObj ) |
200 | { | 198 | { |
201 | uDat.pObject->insert( sKey, pObj ); | 199 | uDat.pObject->insert( sKey, pObj ); |
200 | return *this; | ||
202 | } | 201 | } |
203 | void Bu::Json::insert( const Bu::String &sKey, const Bu::Json &rObj ) | 202 | |
203 | Bu::Json &Bu::Json::insert( const Bu::String &sKey, const Bu::Json &rObj ) | ||
204 | { | 204 | { |
205 | uDat.pObject->insert( sKey, new Bu::Json( rObj ) ); | 205 | uDat.pObject->insert( sKey, new Bu::Json( rObj ) ); |
206 | return *this; | ||
206 | } | 207 | } |
207 | 208 | ||
208 | void Bu::Json::insert( const Bu::String &sKey, const Bu::String &sValue ) | 209 | Bu::Json &Bu::Json::insert( const Bu::String &sKey, const Bu::String &sValue ) |
209 | { | 210 | { |
210 | uDat.pObject->insert( sKey, new Json( sValue ) ); | 211 | uDat.pObject->insert( sKey, new Json( sValue ) ); |
212 | return *this; | ||
211 | } | 213 | } |
212 | 214 | ||
213 | void Bu::Json::insert( const Bu::String &sKey, const char *sValue ) | 215 | Bu::Json &Bu::Json::insert( const Bu::String &sKey, const char *sValue ) |
214 | { | 216 | { |
215 | uDat.pObject->insert( sKey, new Json( sValue ) ); | 217 | uDat.pObject->insert( sKey, new Json( sValue ) ); |
218 | return *this; | ||
216 | } | 219 | } |
217 | 220 | ||
218 | void Bu::Json::insert( const Bu::String &sKey, double dValue ) | 221 | Bu::Json &Bu::Json::insert( const Bu::String &sKey, double dValue ) |
219 | { | 222 | { |
220 | uDat.pObject->insert( sKey, new Json( dValue ) ); | 223 | uDat.pObject->insert( sKey, new Json( dValue ) ); |
224 | return *this; | ||
221 | } | 225 | } |
222 | 226 | ||
223 | void Bu::Json::insert( const Bu::String &sKey, bool bValue ) | 227 | Bu::Json &Bu::Json::insert( const Bu::String &sKey, bool bValue ) |
224 | { | 228 | { |
225 | uDat.pObject->insert( sKey, new Json( bValue ) ); | 229 | uDat.pObject->insert( sKey, new Json( bValue ) ); |
230 | return *this; | ||
226 | } | 231 | } |
227 | 232 | ||
228 | Bu::Json &Bu::Json::insertObject( const Bu::String &sKey ) | 233 | Bu::Json &Bu::Json::insertObject( const Bu::String &sKey ) |
@@ -239,29 +244,34 @@ Bu::Json &Bu::Json::insertArray( const Bu::String &sKey ) | |||
239 | return *pAr; | 244 | return *pAr; |
240 | } | 245 | } |
241 | 246 | ||
242 | void Bu::Json::append( Bu::Json *pObj ) | 247 | Bu::Json &Bu::Json::append( Bu::Json *pObj ) |
243 | { | 248 | { |
244 | uDat.pArray->append( pObj ); | 249 | uDat.pArray->append( pObj ); |
250 | return *this; | ||
245 | } | 251 | } |
246 | 252 | ||
247 | void Bu::Json::append( const Bu::String &sValue ) | 253 | Bu::Json &Bu::Json::append( const Bu::String &sValue ) |
248 | { | 254 | { |
249 | uDat.pArray->append( new Json( sValue ) ); | 255 | uDat.pArray->append( new Json( sValue ) ); |
256 | return *this; | ||
250 | } | 257 | } |
251 | 258 | ||
252 | void Bu::Json::append( const char *sValue ) | 259 | Bu::Json &Bu::Json::append( const char *sValue ) |
253 | { | 260 | { |
254 | uDat.pArray->append( new Json( sValue ) ); | 261 | uDat.pArray->append( new Json( sValue ) ); |
262 | return *this; | ||
255 | } | 263 | } |
256 | 264 | ||
257 | void Bu::Json::append( double dValue ) | 265 | Bu::Json &Bu::Json::append( double dValue ) |
258 | { | 266 | { |
259 | uDat.pArray->append( new Json( dValue ) ); | 267 | uDat.pArray->append( new Json( dValue ) ); |
268 | return *this; | ||
260 | } | 269 | } |
261 | 270 | ||
262 | void Bu::Json::append( bool bValue ) | 271 | Bu::Json &Bu::Json::append( bool bValue ) |
263 | { | 272 | { |
264 | uDat.pArray->append( new Json( bValue ) ); | 273 | uDat.pArray->append( new Json( bValue ) ); |
274 | return *this; | ||
265 | } | 275 | } |
266 | 276 | ||
267 | Bu::Json &Bu::Json::appendObject() | 277 | Bu::Json &Bu::Json::appendObject() |
@@ -282,7 +292,7 @@ void Bu::Json::parse( Bu::Stream &sInput ) | |||
282 | { | 292 | { |
283 | reset(); | 293 | reset(); |
284 | 294 | ||
285 | char c; | 295 | Bu::UtfChar c; |
286 | next("json"); | 296 | next("json"); |
287 | 297 | ||
288 | parse( c, sInput ); | 298 | parse( c, sInput ); |
@@ -294,7 +304,7 @@ void Bu::Json::parse( const Bu::String &sInput ) | |||
294 | parse( mb ); | 304 | parse( mb ); |
295 | } | 305 | } |
296 | 306 | ||
297 | void Bu::Json::parse( char &c, Bu::Stream &sInput ) | 307 | void Bu::Json::parse( Bu::UtfChar &c, Bu::Stream &sInput ) |
298 | { | 308 | { |
299 | while( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) | 309 | while( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) |
300 | { | 310 | { |
@@ -434,9 +444,9 @@ void Bu::Json::writeStable( Bu::Stream &sOutput ) const | |||
434 | { | 444 | { |
435 | sOutput.write("{", 1 ); | 445 | sOutput.write("{", 1 ); |
436 | bool bFirst = true; | 446 | bool bFirst = true; |
437 | Bu::List<Bu::String> lKey = uDat.pObject->getKeys(); | 447 | Bu::List<Bu::UtfString> lKey = uDat.pObject->getKeys(); |
438 | lKey.sort(); | 448 | lKey.sort(); |
439 | for( Bu::List<Bu::String>::iterator i = lKey.begin(); i; i++ ) | 449 | for( Bu::List<Bu::UtfString>::iterator i = lKey.begin(); i; i++ ) |
440 | { | 450 | { |
441 | if( bFirst == true ) | 451 | if( bFirst == true ) |
442 | bFirst = false; | 452 | bFirst = false; |
@@ -480,7 +490,7 @@ Bu::Json &Bu::Json::operator=( const Bu::Json &rSrc ) | |||
480 | break; | 490 | break; |
481 | 491 | ||
482 | case String: | 492 | case String: |
483 | uDat.pString = new Bu::String( *rSrc.uDat.pString ); | 493 | uDat.pString = new Bu::UtfString( *rSrc.uDat.pString ); |
484 | break; | 494 | break; |
485 | 495 | ||
486 | case Number: | 496 | case Number: |
@@ -513,7 +523,8 @@ Bu::Json &Bu::Json::operator=( const Bu::Json &rSrc ) | |||
513 | return *this; | 523 | return *this; |
514 | } | 524 | } |
515 | 525 | ||
516 | void Bu::Json::parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ) | 526 | void Bu::Json::parseString( Bu::UtfChar &c, Bu::Stream &sInput, |
527 | Bu::UtfString &sOut ) | ||
517 | { | 528 | { |
518 | skipWs( c, sInput ); | 529 | skipWs( c, sInput ); |
519 | bool bEscape = false; | 530 | bool bEscape = false; |
@@ -577,14 +588,14 @@ void Bu::Json::parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ) | |||
577 | } | 588 | } |
578 | } | 589 | } |
579 | 590 | ||
580 | void Bu::Json::parseString( char &c, Bu::Stream &sInput ) | 591 | void Bu::Json::parseString( Bu::UtfChar &c, Bu::Stream &sInput ) |
581 | { | 592 | { |
582 | eType = String; | 593 | eType = String; |
583 | uDat.pString = new Bu::String(); | 594 | uDat.pString = new Bu::UtfString(); |
584 | parseString( c, sInput, *uDat.pString ); | 595 | parseString( c, sInput, *uDat.pString ); |
585 | } | 596 | } |
586 | 597 | ||
587 | void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) | 598 | void Bu::Json::parseObject( Bu::UtfChar &c, Bu::Stream &sInput ) |
588 | { | 599 | { |
589 | skipWs( c, sInput ); | 600 | skipWs( c, sInput ); |
590 | eType = Object; | 601 | eType = Object; |
@@ -602,7 +613,7 @@ void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) | |||
602 | 613 | ||
603 | for(;;) | 614 | for(;;) |
604 | { | 615 | { |
605 | Bu::String sKey; | 616 | Bu::UtfString sKey; |
606 | parseString( c, sInput, sKey ); | 617 | parseString( c, sInput, sKey ); |
607 | skipWs( c, sInput ); | 618 | skipWs( c, sInput ); |
608 | if( c != ':' ) | 619 | if( c != ':' ) |
@@ -628,7 +639,7 @@ void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) | |||
628 | } | 639 | } |
629 | } | 640 | } |
630 | 641 | ||
631 | void Bu::Json::parseArray( char &c, Bu::Stream &sInput ) | 642 | void Bu::Json::parseArray( Bu::UtfChar &c, Bu::Stream &sInput ) |
632 | { | 643 | { |
633 | skipWs( c, sInput ); | 644 | skipWs( c, sInput ); |
634 | 645 | ||
@@ -667,7 +678,7 @@ void Bu::Json::parseArray( char &c, Bu::Stream &sInput ) | |||
667 | } | 678 | } |
668 | } | 679 | } |
669 | 680 | ||
670 | void Bu::Json::parseNumber( char &c, Bu::Stream &sInput ) | 681 | void Bu::Json::parseNumber( Bu::UtfChar &c, Bu::Stream &sInput ) |
671 | { | 682 | { |
672 | skipWs( c, sInput ); | 683 | skipWs( c, sInput ); |
673 | 684 | ||
@@ -702,7 +713,7 @@ void Bu::Json::parseNumber( char &c, Bu::Stream &sInput ) | |||
702 | uDat.dNumber = atof( sBuf.getStr() ); | 713 | uDat.dNumber = atof( sBuf.getStr() ); |
703 | } | 714 | } |
704 | 715 | ||
705 | void Bu::Json::parseLiteral( char &c, Bu::Stream &sInput ) | 716 | void Bu::Json::parseLiteral( Bu::UtfChar &c, Bu::Stream &sInput ) |
706 | { | 717 | { |
707 | skipWs( c, sInput ); | 718 | skipWs( c, sInput ); |
708 | 719 | ||
@@ -736,27 +747,27 @@ void Bu::Json::parseLiteral( char &c, Bu::Stream &sInput ) | |||
736 | } | 747 | } |
737 | } | 748 | } |
738 | 749 | ||
739 | bool Bu::Json::readChar( char &c, Bu::Stream &sInput ) | 750 | bool Bu::Json::readChar( Bu::UtfChar &c, Bu::Stream &sInput ) |
740 | { | 751 | { |
741 | if( sInput.read( &c, 1 ) == 0 ) | 752 | if( Bu::UtfString::readPoint( sInput, c ) == 0 && sInput.isEos() ) |
742 | return false; | 753 | return false; |
743 | return true; | 754 | return true; |
744 | } | 755 | } |
745 | 756 | ||
746 | void Bu::Json::readChar( char &c, Bu::Stream &sInput, const char *sSection ) | 757 | void Bu::Json::readChar( Bu::UtfChar &c, Bu::Stream &sInput, const char *sSection ) |
747 | { | 758 | { |
748 | if( sInput.read( &c, 1 ) == 0 ) | 759 | if( Bu::UtfString::readPoint( sInput, c ) == 0 && sInput.isEos() ) |
749 | { | 760 | { |
750 | throw Bu::ExceptionBase( sSection ); | 761 | throw Bu::ExceptionBase( sSection ); |
751 | } | 762 | } |
752 | } | 763 | } |
753 | 764 | ||
754 | bool Bu::Json::isWs( char c ) | 765 | bool Bu::Json::isWs( Bu::UtfChar c ) |
755 | { | 766 | { |
756 | return c == ' ' || c == '\t' || c == '\r' || c == '\n'; | 767 | return c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
757 | } | 768 | } |
758 | 769 | ||
759 | void Bu::Json::skipWs( char &c, Bu::Stream &sInput ) | 770 | void Bu::Json::skipWs( Bu::UtfChar &c, Bu::Stream &sInput ) |
760 | { | 771 | { |
761 | while( isWs( c ) ) | 772 | while( isWs( c ) ) |
762 | { | 773 | { |
@@ -764,10 +775,10 @@ void Bu::Json::skipWs( char &c, Bu::Stream &sInput ) | |||
764 | } | 775 | } |
765 | } | 776 | } |
766 | 777 | ||
767 | void Bu::Json::writeStr( const Bu::String &sStr, Bu::Stream &sOutput ) const | 778 | void Bu::Json::writeStr( const Bu::UtfString &sStr, Bu::Stream &sOutput ) const |
768 | { | 779 | { |
769 | sOutput.write("\"", 1 ); | 780 | sOutput.write("\"", 1 ); |
770 | for( Bu::String::const_iterator i = sStr.begin(); i; i++ ) | 781 | for( Bu::UtfString::const_iterator i = sStr.begin(); i; i++ ) |
771 | { | 782 | { |
772 | switch( *i ) | 783 | switch( *i ) |
773 | { | 784 | { |
@@ -807,12 +818,12 @@ void Bu::Json::writeStr( const Bu::String &sStr, Bu::Stream &sOutput ) const | |||
807 | if( *i < 32 ) | 818 | if( *i < 32 ) |
808 | sOutput.write( | 819 | sOutput.write( |
809 | Bu::String("\\u%1"). | 820 | Bu::String("\\u%1"). |
810 | arg( (int32_t)*i, Bu::Fmt::hex(4).fill('0') ). | 821 | arg( (uint32_t)*i, Bu::Fmt::hex(4).fill('0') ). |
811 | end().getStr(), | 822 | end().getStr(), |
812 | 6 | 823 | 6 |
813 | ); | 824 | ); |
814 | else | 825 | else |
815 | sOutput.write( &(*i), 1 ); | 826 | Bu::UtfString::writePoint( sOutput, *i ); |
816 | break; | 827 | break; |
817 | } | 828 | } |
818 | } | 829 | } |