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 stringvalue;
42  int intvalue;
43  double numbervalue;
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 EncodeInt(int 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(int 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  int GetInt() const; /// get integer value, 0 if not an integer or value is JSON "null"
88  double GetDouble() const; /// get number or integer value, 0 if not a number or value is JSON "null"
89  bool GetBool() const; /// get boolean value, false if not a boolean or value is JSON "null"
90  void GetArrayBuffer(const char** pptr, size_t* psize) const;
91  std::string GetError() const; /// get error message from MJSON_ERROR nodes
92 
93  public: // public helper and debug methods
94  static const char* TypeToString(int type); /// return node type as string
95  void Dump(int nest = 0) const; /// dump the subtree to standard output
96  MJsonNode* Copy() const; /// make a copy of the json tree
97 
98  protected:
99  MJsonNode(int type); // protected constructor for subclassing
100 
101  private:
102  //MJsonNode(); // constructor not permitted
103  //MJsonNode(const MJsonNode&); // copy by copy-constructor not permitted, use Copy() method
104  MJsonNode& operator=(const MJsonNode&); // copy by assignement not permitted, use Copy() method
105 };
106 
107 #endif
108 
109 /* emacs
110  * Local Variables:
111  * tab-width: 8
112  * c-basic-offset: 3
113  * indent-tabs-mode: nil
114  * End:
115  */
std::string GetString() const
find subnode with given name, NULL if not object, NULL is name not found
Definition: mjson.cxx:960
static MJsonNode * MakeJSON(const char *json)
Definition: mjson.cxx:868
void Dump(int nest=0) const
return node type as string
Definition: mjson.cxx:1066
static MJsonNode * MakeArray()
Definition: mjson.cxx:821
MJsonNode * Copy() const
dump the subtree to standard output
Definition: mjson.cxx:1105
const MJsonNodeVector * GetArray() const
get node type: MJSON_xxx
Definition: mjson.cxx:910
int GetType() const
delete a node from an object
Definition: mjson.cxx:905
int GetInt() const
get string value, "" if not string or value is JSON "null"
Definition: mjson.cxx:968
void AddToArray(MJsonNode *node)
Definition: mjson.cxx:883
static MJsonNode * MakeNumber(double value)
Definition: mjson.cxx:846
int intvalue
Definition: mjson.h:42
static std::string Encode(const char *s)
Definition: mjson.cxx:678
std::vector< MJsonNode * > MJsonNodeVector
Definition: mjson.h:34
std::vector< std::string > MJsonStringVector
Definition: mjson.h:31
MJsonNode(int type)
make a copy of the json tree
Definition: mjson.cxx:1032
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:893
double GetDouble() const
get integer value, 0 if not an integer or value is JSON "null"
Definition: mjson.cxx:976
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:926
static MJsonNode * MakeBool(bool value)
Definition: mjson.cxx:853
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:1001
double numbervalue
Definition: mjson.h:43
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:1009
MJsonStringVector objectnames
Definition: mjson.h:40
const MJsonStringVector * GetObjectNames() const
get array value, NULL if not array, empty array if value is JSON "null"
Definition: mjson.cxx:918
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
std::string stringvalue
Definition: mjson.h:41
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:934
static MJsonNode * MakeArrayBuffer(char *ptr, size_t size)
Definition: mjson.cxx:875
MJsonNode & operator=(const MJsonNode &)
static MJsonNode * MakeNull()
Definition: mjson.cxx:863
std::string GetError() const
Definition: mjson.cxx:1024
static MJsonNode * Parse(const char *jsonstring)
Definition: mjson.cxx:645
static std::string EncodeInt(int v)
Definition: mjson.cxx:710
MJsonNodeVector subnodes
Definition: mjson.h:39
static MJsonNode * MakeInt(int value)
Definition: mjson.cxx:838
static const char * TypeToString(int type)
get error message from MJSON_ERROR nodes
Definition: mjson.cxx:1042