From 2fc7fcd7161121ab48cda40687d7d152e03b3079 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 7 Jun 2017 00:11:51 -0700 Subject: Changed interface slightly, it's easier to create json programmatically. --- src/unstable/json.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++----- src/unstable/json.h | 10 +++++++++- 2 files changed, 57 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/unstable/json.cpp b/src/unstable/json.cpp index 0b47da4..f5a1696 100644 --- a/src/unstable/json.cpp +++ b/src/unstable/json.cpp @@ -9,15 +9,52 @@ #define next( txt ) readChar( c, sInput, "Unexpected end of stream while reading " txt "." ) Bu::Json::Json() : - eType( Invalid ) + eType( Null ) { } -Bu::Json::Json( const Bu::String &sJson ) : - eType( Invalid ) +Bu::Json::Json( const Bu::String &sValue ) : + eType( String ), + uDat( sValue ) +{ +} + +Bu::Json::Json( double dValue ) : + eType( Number ), + uDat( dValue ) +{ +} + +Bu::Json::Json( bool bValue ) : + eType( Boolean ), + uDat( bValue ) { - Bu::StaticMemBuf mIn( sJson.getStr(), sJson.getSize() ); - parse( mIn ); +} + +Bu::Json::Json( Type eType ) : + eType( eType ) +{ + switch( eType ) + { + case Object: + uDat.pObject = new JsonHash(); + break; + + case Array: + uDat.pArray = new JsonList(); + break; + + case String: + uDat.pString = new Bu::String(); + break; + + case Number: + case Boolean: + case Null: + case Invalid: + uDat.pObject = NULL; + break; + } } Bu::Json::Json( Bu::Stream &sInput ) : @@ -132,6 +169,12 @@ void Bu::Json::parse( Bu::Stream &sInput ) parse( c, sInput ); } +void Bu::Json::parse( const Bu::String &sInput ) +{ + Bu::StaticMemBuf mb( sInput.getStr(), sInput.getSize() ); + parse( mb ); +} + void Bu::Json::parse( char &c, Bu::Stream &sInput ) { while( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) diff --git a/src/unstable/json.h b/src/unstable/json.h index 2d1770d..76d96be 100644 --- a/src/unstable/json.h +++ b/src/unstable/json.h @@ -32,7 +32,10 @@ namespace Bu public: Json(); - Json( const Bu::String &sJson ); + Json( const Bu::String &sValue ); + Json( double dValue ); + Json( bool bValue ); + Json( Type eType ); Json( Bu::Stream &sInput ); virtual ~Json(); @@ -49,6 +52,7 @@ namespace Bu const_iterator end() const; void parse( Bu::Stream &sInput ); + void parse( const Bu::String &sInput ); void reset(); void write( Bu::Stream &sOutput ) const; @@ -75,6 +79,10 @@ namespace Bu union DatUnion { DatUnion() : pObject( NULL ) { } + DatUnion( const Bu::String &sValue ) : + pString( new Bu::String( sValue ) ) { } + DatUnion( double dValue ) : dNumber( dValue ) { } + DatUnion( bool bValue ) : bBoolean( bValue ) { } JsonHash *pObject; JsonList *pArray; Bu::String *pString; -- cgit v1.2.3