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;
44  size_t arraybuffer_size;
45  char* arraybuffer_ptr;
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
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
int GetType() const
delete a node from an object
int GetInt() const
get string value, "" if not string or value is JSON "null"
void AddToArray(MJsonNode *node)
static MJsonNode * MakeNumber(double value)
static std::string Encode(const char *s)
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 integer 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 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 MJsonNode * MakeNull()
std::string GetError() const
static MJsonNode * Parse(const char *jsonstring)
static std::string EncodeInt(int v)
static MJsonNode * MakeInt(int value)
static const char * TypeToString(int type)
get error message from MJSON_ERROR nodes
int intvalue
Definition: mjson.h:42
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
double numbervalue
Definition: mjson.h:43
MJsonStringVector objectnames
Definition: mjson.h:40
char * arraybuffer_ptr
Definition: mjson.h:45
std::string stringvalue
Definition: mjson.h:41
MJsonNodeVector subnodes
Definition: mjson.h:39