From 33d08f7b68ee57824c135c927bf86ca5fa3444cb Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 11 Mar 2019 11:25:32 -0700 Subject: Json parsing fix + new API for copying. --- src/unstable/json.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/unstable/json.h | 4 ++++ 2 files changed, 61 insertions(+) diff --git a/src/unstable/json.cpp b/src/unstable/json.cpp index 06df9b1..d7e84d9 100644 --- a/src/unstable/json.cpp +++ b/src/unstable/json.cpp @@ -81,6 +81,12 @@ Bu::Json::Json( char &c, Bu::Stream &sInput ) : parse( c, sInput ); } +Bu::Json::Json( const Json &rSrc ) : + eType( Invalid ) +{ + (*this) = rSrc; +} + Bu::Json::~Json() { reset(); @@ -194,6 +200,10 @@ void Bu::Json::insert( const Bu::String &sKey, Bu::Json *pObj ) { uDat.pObject->insert( sKey, pObj ); } +void Bu::Json::insert( const Bu::String &sKey, const Bu::Json &rObj ) +{ + uDat.pObject->insert( sKey, new Bu::Json( rObj ) ); +} void Bu::Json::insert( const Bu::String &sKey, const Bu::String &sValue ) { @@ -352,6 +362,7 @@ void Bu::Json::reset() break; } uDat.pObject = NULL; + eType = Invalid; } void Bu::Json::write( Bu::Stream &sOutput ) const @@ -457,6 +468,51 @@ Bu::String Bu::Json::toStringStable() const return mb.getString(); } +Bu::Json &Bu::Json::operator=( const Bu::Json &rSrc ) +{ + reset(); + + eType = rSrc.eType; + switch( eType ) + { + case Invalid: + case Null: + break; + + case String: + uDat.pString = new Bu::String( *rSrc.uDat.pString ); + break; + + case Number: + uDat.dNumber = rSrc.uDat.dNumber; + break; + + case Boolean: + uDat.bBoolean = rSrc.uDat.bBoolean; + break; + + case Object: + uDat.pObject = new JsonHash(); + for( JsonHash::const_iterator i = rSrc.uDat.pObject->begin(); + i; ++i ) + { + uDat.pObject->insert( i.getKey(), new Json( *i.getValue() ) ); + } + break; + + case Array: + uDat.pArray = new JsonList(); + for( JsonList::const_iterator i = rSrc.uDat.pArray->begin(); + i; ++i ) + { + uDat.pArray->append( new Json( *(*i) ) ); + } + break; + } + + return *this; +} + void Bu::Json::parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ) { skipWs( c, sInput ); @@ -535,6 +591,7 @@ void Bu::Json::parseObject( char &c, Bu::Stream &sInput ) uDat.pObject = new JsonHash(); next( "object" ); + skipWs( c, sInput ); // Check to see if it's an empty object. if( c == '}' ) diff --git a/src/unstable/json.h b/src/unstable/json.h index 336f1fd..4c85dd9 100644 --- a/src/unstable/json.h +++ b/src/unstable/json.h @@ -41,6 +41,7 @@ namespace Bu Json( bool bValue ); Json( Type eType ); Json( Bu::Stream &sInput ); + Json( const Json &rSrc ); virtual ~Json(); Type getType() const; @@ -59,6 +60,7 @@ namespace Bu bool has( const Bu::String &sKey ) const; void insert( const Bu::String &sKey, Bu::Json *pObj ); + void insert( const Bu::String &sKey, const Bu::Json &rObj ); void insert( const Bu::String &sKey, const Bu::String &sValue ); void insert( const Bu::String &sKey, const char *sValue ); void insert( const Bu::String &sKey, double dValue ); @@ -82,6 +84,8 @@ namespace Bu Bu::String toString() const; Bu::String toStringStable() const; + Bu::Json &operator=( const Bu::Json &rSrc ); + private: void parse( char &c, Bu::Stream &sInput ); void parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ); -- cgit v1.2.3