summaryrefslogtreecommitdiff
path: root/src/unstable
diff options
context:
space:
mode:
authorMike Buland <mbuland@penny-arcade.com>2017-06-07 00:11:51 -0700
committerMike Buland <mbuland@penny-arcade.com>2017-06-07 00:11:51 -0700
commit2fc7fcd7161121ab48cda40687d7d152e03b3079 (patch)
tree2cfeff13b83605f9411798de34fffe6d0087c809 /src/unstable
parente98644cd777ddd4ae769da2d8bdcbc6fa8bbb30a (diff)
downloadlibbu++-2fc7fcd7161121ab48cda40687d7d152e03b3079.tar.gz
libbu++-2fc7fcd7161121ab48cda40687d7d152e03b3079.tar.bz2
libbu++-2fc7fcd7161121ab48cda40687d7d152e03b3079.tar.xz
libbu++-2fc7fcd7161121ab48cda40687d7d152e03b3079.zip
Changed interface slightly, it's easier to create json programmatically.
Diffstat (limited to 'src/unstable')
-rw-r--r--src/unstable/json.cpp53
-rw-r--r--src/unstable/json.h10
2 files changed, 57 insertions, 6 deletions
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 @@
9#define next( txt ) readChar( c, sInput, "Unexpected end of stream while reading " txt "." ) 9#define next( txt ) readChar( c, sInput, "Unexpected end of stream while reading " txt "." )
10 10
11Bu::Json::Json() : 11Bu::Json::Json() :
12 eType( Invalid ) 12 eType( Null )
13{ 13{
14} 14}
15 15
16Bu::Json::Json( const Bu::String &sJson ) : 16Bu::Json::Json( const Bu::String &sValue ) :
17 eType( Invalid ) 17 eType( String ),
18 uDat( sValue )
19{
20}
21
22Bu::Json::Json( double dValue ) :
23 eType( Number ),
24 uDat( dValue )
25{
26}
27
28Bu::Json::Json( bool bValue ) :
29 eType( Boolean ),
30 uDat( bValue )
18{ 31{
19 Bu::StaticMemBuf mIn( sJson.getStr(), sJson.getSize() ); 32}
20 parse( mIn ); 33
34Bu::Json::Json( Type eType ) :
35 eType( eType )
36{
37 switch( eType )
38 {
39 case Object:
40 uDat.pObject = new JsonHash();
41 break;
42
43 case Array:
44 uDat.pArray = new JsonList();
45 break;
46
47 case String:
48 uDat.pString = new Bu::String();
49 break;
50
51 case Number:
52 case Boolean:
53 case Null:
54 case Invalid:
55 uDat.pObject = NULL;
56 break;
57 }
21} 58}
22 59
23Bu::Json::Json( Bu::Stream &sInput ) : 60Bu::Json::Json( Bu::Stream &sInput ) :
@@ -132,6 +169,12 @@ void Bu::Json::parse( Bu::Stream &sInput )
132 parse( c, sInput ); 169 parse( c, sInput );
133} 170}
134 171
172void Bu::Json::parse( const Bu::String &sInput )
173{
174 Bu::StaticMemBuf mb( sInput.getStr(), sInput.getSize() );
175 parse( mb );
176}
177
135void Bu::Json::parse( char &c, Bu::Stream &sInput ) 178void Bu::Json::parse( char &c, Bu::Stream &sInput )
136{ 179{
137 while( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) 180 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
32 32
33 public: 33 public:
34 Json(); 34 Json();
35 Json( const Bu::String &sJson ); 35 Json( const Bu::String &sValue );
36 Json( double dValue );
37 Json( bool bValue );
38 Json( Type eType );
36 Json( Bu::Stream &sInput ); 39 Json( Bu::Stream &sInput );
37 virtual ~Json(); 40 virtual ~Json();
38 41
@@ -49,6 +52,7 @@ namespace Bu
49 const_iterator end() const; 52 const_iterator end() const;
50 53
51 void parse( Bu::Stream &sInput ); 54 void parse( Bu::Stream &sInput );
55 void parse( const Bu::String &sInput );
52 void reset(); 56 void reset();
53 57
54 void write( Bu::Stream &sOutput ) const; 58 void write( Bu::Stream &sOutput ) const;
@@ -75,6 +79,10 @@ namespace Bu
75 union DatUnion 79 union DatUnion
76 { 80 {
77 DatUnion() : pObject( NULL ) { } 81 DatUnion() : pObject( NULL ) { }
82 DatUnion( const Bu::String &sValue ) :
83 pString( new Bu::String( sValue ) ) { }
84 DatUnion( double dValue ) : dNumber( dValue ) { }
85 DatUnion( bool bValue ) : bBoolean( bValue ) { }
78 JsonHash *pObject; 86 JsonHash *pObject;
79 JsonList *pArray; 87 JsonList *pArray;
80 Bu::String *pString; 88 Bu::String *pString;