iFun Engine API  1.0.0-b2936
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
json.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_COMMON_JSON_H_
10 #define INCLUDE_FUNAPI_COMMON_JSON_H_
11 
12 #include <funapi/types.h>
13 #pragma GCC diagnostic ignored "-Wtype-limits"
14 #include <rapidjson/document.h>
15 #pragma GCC diagnostic warning "-Wtype-limits"
16 
17 
18 namespace fun {
19 
20 typedef rapidjson::GenericValue<
21  rapidjson::UTF8<char>, rapidjson::CrtAllocator> JsonValueImpl;
22 
23 
24 class Buffer;
25 
26 
27 class Json : private JsonValueImpl {
28  public:
29  DECLARE_CLASS_PTR(Json);
30 
31  class Attribute : private Member {
32  public:
33  string GetName() const;
34  const Json &GetValue() const;
35  Json &GetValue();
36  private:
37  Attribute();
38  };
39 
40  typedef Attribute * AttributeIterator;
41  typedef const Attribute * ConstAttributeIterator;
42 
43  typedef Json * ValueIterator;
44  typedef const Json * ConstValueIterator;
45 
46  typedef boost::function<void(const string & /*json_string*/,
47  const string & /*error_desc*/,
48  size_t /*error_offset*/)> ParseErrorCallback;
49 
50  enum Type {
51  kNull = 0,
52  kBoolean,
53  kInteger,
54  kDouble,
55  kString,
56  kArray,
57  kObject
58  };
59 
60  static const ParseErrorCallback kDefaultParseErrorHandler;
61 
62  // constructor
63  Json();
64  explicit Json(bool value);
65  explicit Json(int value);
66  explicit Json(int64_t value);
67  explicit Json(double value);
68  explicit Json(const char *value);
69  explicit Json(const string &value);
70  Json(const Json &other);
71 
72  ~Json();
73 
74  void Copy(Json *to) const;
75  Json Copy() const;
76  void Move(Json *to);
77 
78  // read/write from/to string, file, stream
79  bool FromString(const string &json_string,
80  const ParseErrorCallback &cb = NULL);
81  bool FromStringInsitu(string *json_string,
82  const ParseErrorCallback &cb = NULL);
83  bool FromStringWithComments(const string &json_string,
84  const ParseErrorCallback &cb = NULL);
85  bool FromString(const char *json_string,
86  const ParseErrorCallback &cb = NULL);
87  bool FromStringInsitu(char *json_string,
88  const ParseErrorCallback &cb = NULL);
89  bool FromString(const char *json_string,
90  size_t len,
91  const ParseErrorCallback &cb = NULL);
92  bool FromStringInsitu(char *json_string,
93  size_t len,
94  const ParseErrorCallback &cb = NULL);
95  bool FromFile(const string &file_path,
96  const ParseErrorCallback &cb = NULL);
97  bool FromFileWithComments(const string &file_path,
98  const ParseErrorCallback &cb = NULL);
99  // NOLINT(runtime/references)
100  bool FromStream(std::istream &input_stream,
101  const ParseErrorCallback &cb = NULL);
102  // NOLINT(runtime/references)
103  bool FromStreamWithComments(std::istream &input_stream,
104  const ParseErrorCallback &cb = NULL);
105 
106  string ToString(bool pretty = false) const;
107  void ToString(string *str, bool pretty = false) const;
108  void ToString(Buffer *buffer, bool pretty = false) const;
109  void ToString(std::ostream *stream, bool pretty = false) const;
110 
111  // type check
112  Type type() const;
113  bool IsNull() const;
114  bool IsBool() const;
115  bool IsInteger() const;
116  bool IsDouble() const;
117  bool IsString() const;
118  bool IsArray() const;
119  bool IsObject() const;
120 
121  // assignment operators
122  Json &operator=(bool value);
123  Json &operator=(int value);
124  Json &operator=(int64_t value);
125  Json &operator=(double value);
126  Json &operator=(const char *value);
127  Json &operator=(const string &value);
128  Json &operator=(const Json &value);
129 
130  // null type
131  void SetNull();
132 
133  // boolean type
134  void SetBool(bool value);
135  bool GetBool() const;
136 
137  // integer type
138  void SetInteger(int64_t value);
139  int64_t GetInteger() const;
140 
141  // double type
142  void SetDouble(double value);
143  double GetDouble() const;
144 
145  // string type
146  void SetString(const string &value);
147  string GetString() const;
148 
149  // array type
150  void SetArray();
151  size_t Size() const;
152  const Json &operator[](size_t index) const;
153  Json &operator[](size_t index);
154 
155  void PushBack();
156  void PushBack(bool value);
157  void PushBack(int value);
158  void PushBack(int64_t value);
159  void PushBack(double value);
160  void PushBack(const char *value);
161  void PushBack(const string &value);
162  void PushBack(const Json &value);
163 
164  void RemoveElementByIndex(size_t index);
165  ValueIterator RemoveElement(ValueIterator iterator);
166  void RemoveAllElements();
167 
168  ValueIterator Begin();
169  ValueIterator End();
170  ConstValueIterator Begin() const;
171  ConstValueIterator End() const;
172 
173  // for object type
174  void SetObject();
175  const Json &operator[](const string &attr_name) const;
176  Json &operator[](const string &attr_name);
177 
178  bool HasAttribute(const string &attr_name) const;
179  bool HasAttribute(const string &attr_name, Type type) const;
180 
181  void AddAttribute(const string &attr_name, bool value);
182  void AddAttribute(const string &attr_name, int value);
183  void AddAttribute(const string &attr_name, int64_t value);
184  void AddAttribute(const string &attr_name, double value);
185  void AddAttribute(const string &attr_name, const char *value);
186  void AddAttribute(const string &attr_name, const string &value);
187  void AddAttribute(const string &attr_name, const Json &value);
188 
189  const Json &GetAttribute(const string &attr_name) const;
190  Json &GetAttribute(const string &attr_name);
191 
192  AttributeIterator FindAttribute(const string &attr_name);
193  ConstAttributeIterator FindAttribute(const string &attr_name) const;
194 
195  bool RemoveAttribute(const string &attr_name);
196  AttributeIterator RemoveAttribute(AttributeIterator iterator);
197  void RemoveAllAttributes();
198 
199  size_t MemberCount() const;
200  AttributeIterator AttributeBegin();
201  AttributeIterator AttributeEnd();
202  ConstAttributeIterator AttributeBegin() const;
203  ConstAttributeIterator AttributeEnd() const;
204 };
205 
206 
207 bool IsEqual(const Json &value1, const Json &value2);
208 bool operator==(const Json &lhs, const Json &rhs);
209 bool operator!=(const Json &lhs, const Json &rhs);
210 bool operator<(const Json &lhs, const Json &rhs);
211 
212 } // namespace fun
213 
214 #endif // INCLUDE_FUNAPI_COMMON_JSON_H_
Definition: json.h:31
Definition: json.h:27