From b7751f0136502592e9b1897b51859b1133339a93 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 6 Jun 2017 12:30:52 -0700 Subject: Hey! This is a much better structure for the Json class. This new setup is one class for everything, the values are kept in a union, and the instance knows what type it is. The tree of objects is a tree of instantiations of the same class. It's much simpler, it's much easier to write, and maybe even easier to use. --- src/unstable/json.h | 113 ++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 70 deletions(-) (limited to 'src/unstable/json.h') diff --git a/src/unstable/json.h b/src/unstable/json.h index 660d1c6..47009cb 100644 --- a/src/unstable/json.h +++ b/src/unstable/json.h @@ -1,89 +1,62 @@ #ifndef BU_JSON_H #define BU_JSON_H +#include "bu/hash.h" +#include "bu/list.h" +#include "bu/string.h" + namespace Bu { + class Stream; + class Json { - public: - Json(); - virtual ~Json(); - public: enum Type { - tObject, - tArray, - tString, - tNumber, - tBoolean, - tNull - }; - - class Base - { - public: - Base(); - virtual ~Base(); - - virtual Type getType()=0; - }; - - class Object : public Base - { - public: - Object(); - virtual ~Object(); - - virtual Type getType(); - }; - - class Array : public Base - { - public: - Array(); - virtual ~Array(); - - virtual Type getType(); + Invalid, + Object, + Array, + String, + Number, + Boolean, + Null }; - class String : public Base - { - public: - String(); - virtual ~String(); - - virtual Type getType(); - }; - - class Number : public Base - { - public: - Number(); - virtual ~Number(); - - virtual Type getType(); - }; - - class Boolean : public Base - { - public: - Boolean(); - virtual ~Boolean(); - - virtual Type getType(); - }; + public: + Json(); + Json( const Bu::String &sJson ); + Json( Bu::Stream &sInput ); + virtual ~Json(); - class Null : public Base - { - public: - Null(); - virtual ~Null(); + void parse( Bu::Stream &sInput ); + void reset(); - virtual Type getType(); - }; + private: + void parseString( char &c, Bu::Stream &sInput, Bu::String &sOut ); + void parseString( char &c, Bu::Stream &sInput ); + void parseObject( char &c, Bu::Stream &sInput ); + void parseArray( char &c, Bu::Stream &sInput ); + void parseNumber( char &c, Bu::Stream &sInput ); + void parseLiteral( char &c, Bu::Stream &sInput ); + bool readChar( char &c, Bu::Stream &sInput ); + void readChar( char &c, Bu::Stream &sInput, const char *sSection ); + bool isWs( char c ); private: + typedef Bu::Hash JsonHash; + typedef Bu::List JsonList; + + Type eType; + union DatUnion + { + DatUnion() : pObject( NULL ) { } + JsonHash *pObject; + JsonList *pArray; + Bu::String *pString; + double dNumber; + bool bBoolean; + } uDat; }; }; -- cgit v1.2.3