aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/json.h
diff options
context:
space:
mode:
authorMike Buland <mbuland@penny-arcade.com>2017-06-06 12:30:52 -0700
committerMike Buland <mbuland@penny-arcade.com>2017-06-06 12:30:52 -0700
commitb7751f0136502592e9b1897b51859b1133339a93 (patch)
tree835b4a78c6b2322039d7b4b70652976bd539a06b /src/unstable/json.h
parent8e0f468e79263dc7f9a6364ca1a26036906642bc (diff)
downloadlibbu++-b7751f0136502592e9b1897b51859b1133339a93.tar.gz
libbu++-b7751f0136502592e9b1897b51859b1133339a93.tar.bz2
libbu++-b7751f0136502592e9b1897b51859b1133339a93.tar.xz
libbu++-b7751f0136502592e9b1897b51859b1133339a93.zip
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.
Diffstat (limited to '')
-rw-r--r--src/unstable/json.h113
1 files changed, 43 insertions, 70 deletions
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 @@
1#ifndef BU_JSON_H 1#ifndef BU_JSON_H
2#define BU_JSON_H 2#define BU_JSON_H
3 3
4#include "bu/hash.h"
5#include "bu/list.h"
6#include "bu/string.h"
7
4namespace Bu 8namespace Bu
5{ 9{
10 class Stream;
11
6 class Json 12 class Json
7 { 13 {
8 public: 14 public:
9 Json();
10 virtual ~Json();
11
12 public:
13 enum Type 15 enum Type
14 { 16 {
15 tObject, 17 Invalid,
16 tArray, 18 Object,
17 tString, 19 Array,
18 tNumber, 20 String,
19 tBoolean, 21 Number,
20 tNull 22 Boolean,
21 }; 23 Null
22
23 class Base
24 {
25 public:
26 Base();
27 virtual ~Base();
28
29 virtual Type getType()=0;
30 };
31
32 class Object : public Base
33 {
34 public:
35 Object();
36 virtual ~Object();
37
38 virtual Type getType();
39 };
40
41 class Array : public Base
42 {
43 public:
44 Array();
45 virtual ~Array();
46
47 virtual Type getType();
48 }; 24 };
49 25
50 class String : public Base 26 public:
51 { 27 Json();
52 public: 28 Json( const Bu::String &sJson );
53 String(); 29 Json( Bu::Stream &sInput );
54 virtual ~String(); 30 virtual ~Json();
55
56 virtual Type getType();
57 };
58
59 class Number : public Base
60 {
61 public:
62 Number();
63 virtual ~Number();
64
65 virtual Type getType();
66 };
67
68 class Boolean : public Base
69 {
70 public:
71 Boolean();
72 virtual ~Boolean();
73
74 virtual Type getType();
75 };
76 31
77 class Null : public Base 32 void parse( Bu::Stream &sInput );
78 { 33 void reset();
79 public:
80 Null();
81 virtual ~Null();
82 34
83 virtual Type getType(); 35 private:
84 }; 36 void parseString( char &c, Bu::Stream &sInput, Bu::String &sOut );
37 void parseString( char &c, Bu::Stream &sInput );
38 void parseObject( char &c, Bu::Stream &sInput );
39 void parseArray( char &c, Bu::Stream &sInput );
40 void parseNumber( char &c, Bu::Stream &sInput );
41 void parseLiteral( char &c, Bu::Stream &sInput );
42 bool readChar( char &c, Bu::Stream &sInput );
43 void readChar( char &c, Bu::Stream &sInput, const char *sSection );
44 bool isWs( char c );
85 45
86 private: 46 private:
47 typedef Bu::Hash<Bu::String, Json *> JsonHash;
48 typedef Bu::List<Json *> JsonList;
49
50 Type eType;
51 union DatUnion
52 {
53 DatUnion() : pObject( NULL ) { }
54 JsonHash *pObject;
55 JsonList *pArray;
56 Bu::String *pString;
57 double dNumber;
58 bool bBoolean;
59 } uDat;
87 }; 60 };
88}; 61};
89 62