iFun Engine API  1.0.0-b3630
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-2018 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  // Parse option for JSON parser. Can be bit-wise ORed.
61  // JSON 파서를 위한 옵션. bit 단위 OR 연산자로 처리 가능
62  // For example,
63  // // Discard comments; 주석을 무시하면서 읽기
64  // fun::Json document;
65  // document.FromString(some_json_str, fun::Json::kAllowComment);
66  // // Discard UTF-8 BOM; UTF-8 BOM을 무시하고 읽기
67  // document.FromString(some_json_str, fun::Json::kAllowUtf8Bom);
68  // // Discard comments and UTF-8 BOM
69  // // 주석과 UTF-8 BOM을 무시하면서 읽기
70  // document.FromString(some_json_str,
71  // fun::Json::kAllowUtf8Bom | fun::json::kAllowComment);
72  enum ParserFlag {
73  kNone = 0,
74  kAllowComment = 1, // allow single line or multi-line comments in JSON
75  kAllowUtf8Bom = 2, // allow byte order mark in JSON
76  };
77 
78  static const ParseErrorCallback kDefaultParseErrorHandler;
79 
80  // constructor
81  Json();
82  explicit Json(bool value);
83  explicit Json(int value);
84  explicit Json(int64_t value);
85  explicit Json(double value);
86  explicit Json(const char *value);
87  explicit Json(const string &value);
88  Json(const Json &other);
89 
90  ~Json();
91 
92  void Copy(Json *to) const;
93  Json Copy() const;
94  void Move(Json *to);
95 
96  // read/write from/to string, file, stream
97  bool FromString(const string &json_string,
98  const ParseErrorCallback &cb = NULL);
99  bool FromString(const string &json_string,
100  int parser_flag, // see ParserFlag
101  const ParseErrorCallback &cb = NULL);
102  bool FromStringInsitu(string *json_string,
103  const ParseErrorCallback &cb = NULL);
104  // Deprecated. Please use FromString(..., kAllowComment)
105  DEPRECATED(
106  bool FromStringWithComments(const string &json_string,
107  const ParseErrorCallback &cb = NULL));
108  bool FromString(const char *json_string,
109  const ParseErrorCallback &cb = NULL);
110  bool FromStringInsitu(char *json_string,
111  const ParseErrorCallback &cb = NULL);
112  bool FromString(const char *json_string,
113  size_t len,
114  const ParseErrorCallback &cb = NULL);
115  bool FromStringInsitu(char *json_string,
116  size_t len,
117  const ParseErrorCallback &cb = NULL);
118  bool FromFile(const string &file_path,
119  const ParseErrorCallback &cb = NULL);
120  bool FromFileWithComments(const string &file_path,
121  const ParseErrorCallback &cb = NULL);
122  // NOLINT(runtime/references)
123  bool FromStream(std::istream &input_stream,
124  const ParseErrorCallback &cb = NULL);
125  // Deprecated. Please use FromStream(..., kAllowComment)
126  // NOLINT(runtime/references)
127  DEPRECATED(
128  bool FromStreamWithComments(std::istream &input_stream,
129  const ParseErrorCallback &cb = NULL));
130  // NOLINT(runtime/references)
131  bool FromStream(std::istream &input_stream,
132  int parser_flag, // see ParserFlag
133  const ParseErrorCallback &cb = NULL);
134 
135  string ToString(bool pretty = false) const;
136  void ToString(string *str, bool pretty = false) const;
137  void ToString(Buffer *buffer, bool pretty = false) const;
138  void ToString(std::ostream *stream, bool pretty = false) const;
139 
140  // type check
141  Type type() const;
142  bool IsNull() const;
143  bool IsBool() const;
144  bool IsInteger() const;
145  bool IsDouble() const;
146  bool IsString() const;
147  bool IsArray() const;
148  bool IsObject() const;
149 
150  // assignment operators
151  Json &operator=(bool value);
152  Json &operator=(int value);
153  Json &operator=(int64_t value);
154  Json &operator=(double value);
155  Json &operator=(const char *value);
156  Json &operator=(const string &value);
157  Json &operator=(const Json &value);
158 
159  // null type
160  void SetNull();
161 
162  // boolean type
163  void SetBool(bool value);
164  bool GetBool() const;
165 
166  // integer type
167  void SetInteger(int64_t value);
168  int64_t GetInteger() const;
169 
170  // double type
171  void SetDouble(double value);
172  double GetDouble() const;
173 
174  // string type
175  void SetString(const string &value);
176  string GetString() const;
177 
178  // array type
179  void SetArray();
180  size_t Size() const;
181  const Json &operator[](size_t index) const;
182  Json &operator[](size_t index);
183 
184  void PushBack();
185  void PushBack(bool value);
186  void PushBack(int value);
187  void PushBack(int64_t value);
188  void PushBack(double value);
189  void PushBack(const char *value);
190  void PushBack(const string &value);
191  void PushBack(const Json &value);
192 
193  void RemoveElementByIndex(size_t index);
194  ValueIterator RemoveElement(ValueIterator iterator);
195  void RemoveAllElements();
196 
197  ValueIterator Begin();
198  ValueIterator End();
199  ConstValueIterator Begin() const;
200  ConstValueIterator End() const;
201 
202  // for object type
203  void SetObject();
204  const Json &operator[](const string &attr_name) const;
205  Json &operator[](const string &attr_name);
206 
207  bool HasAttribute(const string &attr_name) const;
208  bool HasAttribute(const string &attr_name, Type type) const;
209 
210  void AddAttribute(const string &attr_name, bool value);
211  void AddAttribute(const string &attr_name, int value);
212  void AddAttribute(const string &attr_name, int64_t value);
213  void AddAttribute(const string &attr_name, double value);
214  void AddAttribute(const string &attr_name, const char *value);
215  void AddAttribute(const string &attr_name, const string &value);
216  void AddAttribute(const string &attr_name, const Json &value);
217 
218  const Json &GetAttribute(const string &attr_name) const;
219  Json &GetAttribute(const string &attr_name);
220 
221  AttributeIterator FindAttribute(const string &attr_name);
222  ConstAttributeIterator FindAttribute(const string &attr_name) const;
223 
224  bool RemoveAttribute(const string &attr_name);
225  AttributeIterator RemoveAttribute(AttributeIterator iterator);
226  void RemoveAllAttributes();
227 
228  size_t MemberCount() const;
229  AttributeIterator AttributeBegin();
230  AttributeIterator AttributeEnd();
231  ConstAttributeIterator AttributeBegin() const;
232  ConstAttributeIterator AttributeEnd() const;
233 };
234 
235 
236 bool IsEqual(const Json &value1, const Json &value2);
237 bool operator==(const Json &lhs, const Json &rhs);
238 bool operator!=(const Json &lhs, const Json &rhs);
239 bool operator<(const Json &lhs, const Json &rhs);
240 
241 } // namespace fun
242 
243 #endif // INCLUDE_FUNAPI_COMMON_JSON_H_
Definition: json.h:31
Definition: json.h:27