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
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
Definition mjson.cxx:961
static MJsonNode * MakeJSON(const char *json)
Definition mjson.cxx:867
void Dump(int nest=0) const
return node type as string
Definition mjson.cxx:1071
static MJsonNode * MakeArray()
Definition mjson.cxx:821
MJsonNode * Copy() const
dump the subtree to standard output
Definition mjson.cxx:1110
const MJsonNodeVector * GetArray() const
get node type: MJSON_xxx
Definition mjson.cxx:909
long long GetLL() const
get integer value, 0 if not an integer or value is JSON "null"
Definition mjson.cxx:977
MJsonStringVector object_names
Definition mjson.h:40
int GetType() const
delete a node from an object
Definition mjson.cxx:904
void AddToArray(MJsonNode *node)
Definition mjson.cxx:882
static MJsonNode * MakeNumber(double value)
Definition mjson.cxx:845
static std::string Encode(const char *s)
Definition mjson.cxx:678
double double_value
Definition mjson.h:43
std::vector< MJsonNode * > MJsonNodeVector
Definition mjson.h:34
std::vector< std::string > MJsonStringVector
Definition mjson.h:33
long long GetInt() const
get string value, "" if not string or value is JSON "null"
Definition mjson.cxx:969
static MJsonNode * MakeObject()
Definition mjson.cxx:826
void AddToObject(const char *name, MJsonNode *node)
add node to an array. the array takes ownership of this node
Definition mjson.cxx:892
double GetDouble() const
get 64-bit long long value, 0 if not an integer or value is JSON "null"
Definition mjson.cxx:985
int type
Definition mjson.h:38
const MJsonNodeVector * GetObjectNodes() const
get array of object names, NULL if not object, empty array if value is JSON "null"
Definition mjson.cxx:925
static MJsonNode * MakeBool(bool value)
Definition mjson.cxx:852
size_t arraybuffer_size
Definition mjson.h:44
std::string Stringify(int flags=0) const
Definition mjson.cxx:736
static MJsonNode * MakeString(const char *value)
Definition mjson.cxx:831
void DeleteObjectNode(const char *name)
add node to an object. the object takes ownership of this node
Definition mjson.cxx:944
bool GetBool() const
get number or integer value, 0 if not a number or value is JSON "null"
Definition mjson.cxx:1011
static MJsonNode * MakeInt(long long value)
Definition mjson.cxx:838
static std::string EncodeDouble(double v)
Definition mjson.cxx:717
~MJsonNode()
Definition mjson.cxx:651
void GetArrayBuffer(const char **pptr, size_t *psize) const
get boolean value, false if not a boolean or value is JSON "null"
Definition mjson.cxx:1019
MJsonNode & operator=(const MJsonNode &)
long long ll_value
Definition mjson.h:42
std::string string_value
Definition mjson.h:41
const MJsonStringVector * GetObjectNames() const
get array value, NULL if not array, empty array if value is JSON "null"
Definition mjson.cxx:917
char * arraybuffer_ptr
Definition mjson.h:45
static MJsonNode * MakeError(MJsonNode *errornode, const char *errormessage, const char *sin, const char *serror)
the node takes ownership of the buffer
Definition mjson.cxx:791
const MJsonNode * FindObjectNode(const char *name) const
get array of object subnodes, NULL if not object, empty array if value is JSON "null"
Definition mjson.cxx:933
static MJsonNode * MakeArrayBuffer(char *ptr, size_t size)
Definition mjson.cxx:874
static std::string EncodeLL(long long v)
Definition mjson.cxx:710
static MJsonNode * MakeNull()
Definition mjson.cxx:862
std::string GetError() const
Definition mjson.cxx:1034
static MJsonNode * Parse(const char *jsonstring)
Definition mjson.cxx:645
MJsonNodeVector subnodes
Definition mjson.h:39
static const char * TypeToString(int type)
get error message from MJSON_ERROR nodes
Definition mjson.cxx:1047