ROOTANA
Loading...
Searching...
No Matches
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
31class MJsonNode;
32
33typedef std::vector<std::string> MJsonStringVector;
34typedef std::vector<MJsonNode*> MJsonNodeVector;
35
36class 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 std::vector<char>* arraybuffer_vec = NULL;
47
48 public:
49 ~MJsonNode(); // dtor
50
51 public: // parser
52 static MJsonNode* Parse(const char* jsonstring);
53
54 public: // encoder
55 std::string Stringify(int flags = 0) const;
56
57 public: // string encoder
58 static std::string Encode(const char* s);
59 static std::string EncodeLL(long long v);
60 static std::string EncodeDouble(double v);
61
62 public: // public factory constructors
65 static MJsonNode* MakeString(const char* value);
66 static MJsonNode* MakeInt(long long value);
67 static MJsonNode* MakeNumber(double value);
68 static MJsonNode* MakeBool(bool value);
70 static MJsonNode* MakeJSON(const char* json);
71 static MJsonNode* MakeArrayBuffer(char* ptr, size_t size); /// the node takes ownership of the buffer
72 static MJsonNode* MakeArrayBuffer(std::vector<char>* buf); /// the node takes ownership of the buffer
73 static MJsonNode* MakeError(MJsonNode* errornode, const char* errormessage, const char* sin, const char* serror);
74
75 public: // public "put" methods
76 void AddToArray(MJsonNode* node); /// add node to an array. the array takes ownership of this node
77 void AddToObject(const char* name, MJsonNode* node); /// add node to an object. the object takes ownership of this node
78
79 public: // public "delete" methods
80 void DeleteObjectNode(const char* name); /// delete a node from an object
81
82 public: // public "get" methods
83 int GetType() const; /// get node type: MJSON_xxx
84 const MJsonNodeVector* GetArray() const; /// get array value, NULL if not array, empty array if value is JSON "null"
85 const MJsonStringVector* GetObjectNames() const; /// get array of object names, NULL if not object, empty array if value is JSON "null"
86 const MJsonNodeVector* GetObjectNodes() const; /// get array of object subnodes, NULL if not object, empty array if value is JSON "null"
87 const MJsonNode* FindObjectNode(const char* name) const; /// find subnode with given name, NULL if not object, NULL is name not found
88 std::string GetString() const; /// get string value, "" if not string or value is JSON "null"
89 long long GetInt() const; /// get integer value, 0 if not an integer or value is JSON "null"
90 long long GetLL() const; /// get 64-bit long long value, 0 if not an integer or value is JSON "null"
91 double GetDouble() const; /// get number or integer value, 0 if not a number or value is JSON "null"
92 bool GetBool() const; /// get boolean value, false if not a boolean or value is JSON "null"
93 void GetArrayBuffer(const char** pptr, size_t* psize) const;
94 std::string GetError() const; /// get error message from MJSON_ERROR nodes
95
96 public: // public helper and debug methods
97 static const char* TypeToString(int type); /// return node type as string
98 void Dump(int nest = 0) const; /// dump the subtree to standard output
99 MJsonNode* Copy() const; /// make a copy of the json tree
100
101 protected:
102 MJsonNode(int type); // protected constructor for subclassing
103
104 private:
105 //MJsonNode(); // constructor not permitted
106 //MJsonNode(const MJsonNode&); // copy by copy-constructor not permitted, use Copy() method
107 MJsonNode& operator=(const MJsonNode&); // copy by assignement not permitted, use Copy() method
108};
109
110#endif
111
112/* emacs
113 * Local Variables:
114 * tab-width: 8
115 * c-basic-offset: 3
116 * indent-tabs-mode: nil
117 * End:
118 */
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"
static MJsonNode * MakeArrayBuffer(std::vector< char > *buf)
the node takes ownership of the buffer
MJsonNode & operator=(const MJsonNode &)
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)
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:33
int type
Definition mjson.h:38
size_t arraybuffer_size
Definition mjson.h:44
std::vector< char > * arraybuffer_vec
Definition mjson.h:46
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