summaryrefslogtreecommitdiff
path: root/src/unstable/json.h
blob: 217a69a7bccd9af9475298cd8d8b6188c13237ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef BU_JSON_H
#define BU_JSON_H

#include "bu/hash.h"
#include "bu/list.h"
#include "bu/array.h"
#include "bu/string.h"
#include "bu/utfstring.h"

namespace Bu
{
    class Stream;

    class Json
    {
    private:
        Json( char &c, Bu::Stream &sInput );
        typedef Bu::Hash<Bu::String, Json *> JsonHash;
        typedef Bu::Array<Json *> JsonList;

    public:
        typedef JsonList::iterator iterator;
        typedef JsonList::const_iterator const_iterator;
        enum Type
        {
            Invalid,
            Object,
            Array,
            String,
            Number,
            Boolean,
            Null
        };

    public:
        Json();
        Json( const Bu::UtfString &sValue );
        Json( const Bu::String &sValue );
        Json( const char *sValue );
        Json( double dValue );
        Json( bool bValue );
        Json( Type eType );
        Json( Bu::Stream &sInput );
        virtual ~Json();

        Type getType() const;
        Bu::String getString() const;
        double getNumber() const;
        bool getBoolean() const;
        bool isNull() const;
        Json &operator[]( const Bu::String &sKey ) const;
        Json &operator[]( int iIndex ) const;
        int getSize() const;
        iterator begin();
        const_iterator begin() const;
        iterator end();
        const_iterator end() const;

        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 );
        void reset();

        void write( Bu::Stream &sOutput ) const;
        void writeStable( Bu::Stream &sOutput ) const;
        Bu::String toString() const;
        Bu::String toStringStable() const;

    private:
        void parse( char &c, Bu::Stream &sInput );
        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 );
        void skipWs( char &c, Bu::Stream &sInput );
        void writeStr( const Bu::String &sStr, Bu::Stream &sOutput ) const;

    private:
        Type eType;
        union DatUnion
        {
            DatUnion() : pObject( NULL ) { }
            DatUnion( const Bu::String &sValue ) :
                pString( new Bu::String( sValue ) ) { }
            DatUnion( const char *sValue ) :
                pString( new Bu::String( sValue ) ) { }
            DatUnion( double dValue ) : dNumber( dValue ) { }
            DatUnion( bool bValue ) : bBoolean( bValue ) { }
            JsonHash *pObject;
            JsonList *pArray;
            Bu::String *pString;
            double dNumber;
            bool bBoolean;
        } uDat;
    };
};

#endif