From 516fa9045af1e9c85ce524535d0ea5bc395e86cb Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 18 Jan 2018 16:08:24 -0800 Subject: Made json much more helpful. Fixed array iterators. --- src/stable/array.h | 21 ++++++++++++- src/unstable/json.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/unstable/json.h | 18 ++++++++++-- 3 files changed, 115 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/stable/array.h b/src/stable/array.h index a662705..8c17cc4 100644 --- a/src/stable/array.h +++ b/src/stable/array.h @@ -362,6 +362,18 @@ namespace Bu long iPos; public: + iterator() : + src( NULL ), + iPos( -1 ) + { + } + + iterator( const iterator &rSrc ) : + src( rSrc.src ), + iPos( rSrc.iPos ) + { + } + iterator operator++( int ) { if( iPos < 0 ) @@ -480,11 +492,18 @@ namespace Bu long iPos; public: - const_iterator( iterator &rSrc ) : + const_iterator( const iterator &rSrc ) : src( rSrc.src ), iPos( rSrc.iPos ) { } + + const_iterator( const const_iterator &rSrc ) : + src( rSrc.src ), + iPos( rSrc.iPos ) + { + } + const_iterator operator++( int ) { if( iPos < 0 ) diff --git a/src/unstable/json.cpp b/src/unstable/json.cpp index 6159ef3..3627214 100644 --- a/src/unstable/json.cpp +++ b/src/unstable/json.cpp @@ -126,15 +126,24 @@ bool Bu::Json::isNull() const return eType == Null; } -Bu::Json *Bu::Json::operator[]( const Bu::String &sKey ) const +Bu::Json &Bu::Json::operator[]( const Bu::String &sKey ) const { if( eType != Object ) throw Bu::ExceptionBase( "Object entry requested from non-object json object." ); - return uDat.pObject->get( sKey ); + return *uDat.pObject->get( sKey ); +} + +Bu::Json &Bu::Json::operator[]( int iIndex ) const +{ + if( eType != Array ) + throw Bu::ExceptionBase( + "Object entry requested from non-array json object." + ); + return *uDat.pArray->get( iIndex ); } int Bu::Json::getSize() const @@ -181,11 +190,79 @@ 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::String &sValue ) +{ + uDat.pObject->insert( sKey, new Json( sValue ) ); +} + +void Bu::Json::insert( const Bu::String &sKey, const char *sValue ) +{ + uDat.pObject->insert( sKey, new Json( sValue ) ); +} + +void Bu::Json::insert( const Bu::String &sKey, double dValue ) +{ + uDat.pObject->insert( sKey, new Json( dValue ) ); +} + +void Bu::Json::insert( const Bu::String &sKey, bool bValue ) +{ + uDat.pObject->insert( sKey, new Json( bValue ) ); +} + +Bu::Json &Bu::Json::insertObject( const Bu::String &sKey ) +{ + Json *pOb = new Json( Object ); + uDat.pObject->insert( sKey, pOb ); + return *pOb; +} + +Bu::Json &Bu::Json::insertArray( const Bu::String &sKey ) +{ + Json *pAr = new Json( Array ); + uDat.pObject->insert( sKey, pAr ); + return *pAr; +} + void Bu::Json::append( Bu::Json *pObj ) { uDat.pArray->append( pObj ); } +void Bu::Json::append( const Bu::String &sValue ) +{ + uDat.pArray->append( new Json( sValue ) ); +} + +void Bu::Json::append( const char *sValue ) +{ + uDat.pArray->append( new Json( sValue ) ); +} + +void Bu::Json::append( double dValue ) +{ + uDat.pArray->append( new Json( dValue ) ); +} + +void Bu::Json::append( bool bValue ) +{ + uDat.pArray->append( new Json( bValue ) ); +} + +Bu::Json &Bu::Json::appendObject() +{ + Json *pOb = new Json( Object ); + uDat.pArray->append( pOb ); + return *pOb; +} + +Bu::Json &Bu::Json::appendArray() +{ + Json *pAr = new Json( Array ); + uDat.pArray->append( pAr ); + return *pAr; +} + void Bu::Json::parse( Bu::Stream &sInput ) { reset(); diff --git a/src/unstable/json.h b/src/unstable/json.h index 86074bc..217a69a 100644 --- a/src/unstable/json.h +++ b/src/unstable/json.h @@ -3,6 +3,7 @@ #include "bu/hash.h" #include "bu/list.h" +#include "bu/array.h" #include "bu/string.h" #include "bu/utfstring.h" @@ -15,7 +16,7 @@ namespace Bu private: Json( char &c, Bu::Stream &sInput ); typedef Bu::Hash JsonHash; - typedef Bu::List JsonList; + typedef Bu::Array JsonList; public: typedef JsonList::iterator iterator; @@ -47,7 +48,8 @@ namespace Bu double getNumber() const; bool getBoolean() const; bool isNull() const; - Json *operator[]( const Bu::String &sKey ) const; + Json &operator[]( const Bu::String &sKey ) const; + Json &operator[]( int iIndex ) const; int getSize() const; iterator begin(); const_iterator begin() const; @@ -56,7 +58,19 @@ 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::String &sValue ); + void insert( const Bu::String &sKey, const char *sValue ); + void insert( const Bu::String &sKey, double dValue ); + void insert( const Bu::String &sKey, bool bValue ); + Json &insertObject( const Bu::String &sKey ); + Json &insertArray( const Bu::String &sKey ); void append( Bu::Json *pObj ); + void append( const Bu::String &sValue ); + void append( const char *sValue ); + void append( double dValue ); + void append( bool bValue ); + Json &appendObject(); + Json &appendArray(); void parse( Bu::Stream &sInput ); void parse( const Bu::String &sInput ); -- cgit v1.2.3