ROOTANA
mjson.h
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: mjson.h
4  Created by: Konstantin Olchanski
5 
6  Contents: JSON encoder and decoder
7 
8 \********************************************************************/
9 
10 #ifndef _MJSON_H_
11 #define _MJSON_H_
12 
13 #include <string>
14 #include <vector>
15 
16 /** @addtogroup mjson JSON Functions
17  * @{ */
18 
19 #define MJSON_ERROR -1
20 #define MJSON_NONE 0
21 #define MJSON_ARRAY 1
22 #define MJSON_OBJECT 2
23 #define MJSON_STRING 3
24 #define MJSON_INT 4
25 #define MJSON_NUMBER 5
26 #define MJSON_BOOL 6
27 #define MJSON_NULL 7
28 #define MJSON_JSON 8
29 #define MJSON_ARRAYBUFFER 9
30 
31 class MJsonNode;
32 
33 typedef std::vector<std::string> MJsonStringVector;
34 typedef std::vector<MJsonNode*> MJsonNodeVector;
35 
36 class MJsonNode {
37  protected:
38  int type;
41  std::string string_value;
42  long long ll_value = 0;
43  double double_value = 0;
44  size_t arraybuffer_size = 0;
45  char* arraybuffer_ptr = NULL;
46 
47  public:
48  ~MJsonNode(); // dtor
49 
50  public: // parser
51  static MJsonNode* Parse(const char* jsonstring);
52 
53  public: // encoder
54  std::string Stringify(int flags = 0) const;
55 
56  public: // string encoder
57  static std::string Encode(const char* s);
58  static std::string EncodeLL(long long v);
59  static std::string EncodeDouble(double v);
60 
61  public: // public factory constructors
62  static MJsonNode* MakeArray();
63  static MJsonNode* MakeObject();
64  static MJsonNode* MakeString(const char* value);
65  static MJsonNode* MakeInt(long long value);
66  static MJsonNode* MakeNumber(double value);
67  static MJsonNode* MakeBool(bool value);
68  static MJsonNode* MakeNull();
69  static MJsonNode* MakeJSON(const char* json);
70  static MJsonNode* MakeArrayBuffer(char* ptr, size_t size); /// the node takes ownership of the buffer
71  static MJsonNode* MakeError(MJsonNode* errornode, const char* errormessage, const char* sin, const char* serror);
72 
73  public: // public "put" methods
74  void AddToArray(MJsonNode* node); /// add node to an array. the array takes ownership of this node
75  void AddToObject(const char* name, MJsonNode* node); /// add node to an object. the object takes ownership of this node
76 
77  public: // public "delete" methods
78  void DeleteObjectNode(const char* name); /// delete a node from an object
79 
80  public: // public "get" methods
81  int GetType() const; /// get node type: MJSON_xxx
82  const MJsonNodeVector* GetArray() const; /// get array value, NULL if not array, empty array if value is JSON "null"
83  const MJsonStringVector* GetObjectNames() const; /// get array of object names, NULL if not object, empty array if value is JSON "null"
84  const MJsonNodeVector* GetObjectNodes() const; /// get array of object subnodes, NULL if not object, empty array if value is JSON "null"
85  const MJsonNode* FindObjectNode(const char* name) const; /// find subnode with given name, NULL if not object, NULL is name not found
86  std::string GetString() const; /// get string value, "" if not string or value is JSON "null"
87  long long GetInt() const; /// get integer value, 0 if not an integer or value is JSON "null"
88  long long GetLL() const; /// get 64-bit long long value, 0 if not an integer or value is JSON "null"
89  double GetDouble() const; /// get number or integer value, 0 if not a number or value is JSON "null"
90  bool GetBool() const; /// get boolean value, false if not a boolean or value is JSON "null"
91  void GetArrayBuffer(const char** pptr, size_t* psize) const;
92  std::string GetError() const; /// get error message from MJSON_ERROR nodes
93 
94  public: // public helper and debug methods
95  static const char* TypeToString(int type); /// return node type as string
96  void Dump(int nest = 0) const; /// dump the subtree to standard output
97  MJsonNode* Copy() const; /// make a copy of the json tree
98 
99  protected:
100  MJsonNode(int type); // protected constructor for subclassing
101 
102  private:
103  //MJsonNode(); // constructor not permitted
104  //MJsonNode(const MJsonNode&); // copy by copy-constructor not permitted, use Copy() method
105  MJsonNode& operator=(const MJsonNode&); // copy by assignement not permitted, use Copy() method
106 };
107 
108 #endif
109 
110 /* emacs
111  * Local Variables:
112  * tab-width: 8
113  * c-basic-offset: 3
114  * indent-tabs-mode: nil
115  * End:
116  */
std::string GetString() const
find subnode with given name, NULL if not object, NULL is name not found
static MJsonNode * MakeJSON(const char *json)
void Dump(int nest=0) const
return node type as string
static MJsonNode * MakeArray()
MJsonNode * Copy() const
dump the subtree to standard output
const MJsonNodeVector * GetArray() const
get node type: MJSON_xxx
long long GetLL() const
get integer value, 0 if not an integer or value is JSON "null"
int GetType() const
delete a node from an object
void AddToArray(MJsonNode *node)
static MJsonNode * MakeNumber(double value)
static std::string Encode(const char *s)
long long GetInt() const
get string value, "" if not string or value is JSON "null"
MJsonNode(int type)
make a copy of the json tree
static MJsonNode * MakeObject()
void AddToObject(const char *name, MJsonNode *node)
add node to an array. the array takes ownership of this node
double GetDouble() const
get 64-bit long long value, 0 if not an integer or value is JSON "null"
const MJsonNodeVector * GetObjectNodes() const
get array of object names, NULL if not object, empty array if value is JSON "null"
static MJsonNode * MakeBool(bool value)
std::string Stringify(int flags=0) const
static MJsonNode * MakeString(const char *value)
void DeleteObjectNode(const char *name)
add node to an object. the object takes ownership of this node
bool GetBool() const
get number or integer value, 0 if not a number or value is JSON "null"
static MJsonNode * MakeInt(long long value)
static std::string EncodeDouble(double v)
void GetArrayBuffer(const char **pptr, size_t *psize) const
get boolean value, false if not a boolean or value is JSON "null"
const MJsonStringVector * GetObjectNames() const
get array value, NULL if not array, empty array if value is JSON "null"
static MJsonNode * MakeError(MJsonNode *errornode, const char *errormessage, const char *sin, const char *serror)
the node takes ownership of the buffer
const MJsonNode * FindObjectNode(const char *name) const
get array of object subnodes, NULL if not object, empty array if value is JSON "null"
static MJsonNode * MakeArrayBuffer(char *ptr, size_t size)
MJsonNode & operator=(const MJsonNode &)
static std::string EncodeLL(long long v)
static MJsonNode * MakeNull()
std::string GetError() const
static MJsonNode * Parse(const char *jsonstring)
static const char * TypeToString(int type)
get error message from MJSON_ERROR nodes
MJsonStringVector object_names
Definition: mjson.h:40
double double_value
Definition: mjson.h:43
std::vector< MJsonNode * > MJsonNodeVector
Definition: mjson.h:34
std::vector< std::string > MJsonStringVector
Definition: mjson.h:31
int type
Definition: mjson.h:38
size_t arraybuffer_size
Definition: mjson.h:44
long long ll_value
Definition: mjson.h:42
std::string string_value
Definition: mjson.h:41
char * arraybuffer_ptr
Definition: mjson.h:45
MJsonNodeVector subnodes
Definition: mjson.h:39