iFun Engine API  1.0.0-b6053
Great Technology for Great Games
json.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 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 FUNAPI_DLL_VISIBILITY Json : private JsonValueImpl {
28  public:
30 
31  class FUNAPI_DLL_VISIBILITY 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  Json(Json &&other);
90 
91  ~Json();
92 
93  void Copy(Json *to) const;
94  Json Copy() const;
95  void Move(Json *to);
96 
97  // read/write from/to string, file, stream
98  bool FromString(const string &json_string,
99  const ParseErrorCallback &cb = NULL);
100  bool FromString(const string &json_string,
101  int parser_flag, // see ParserFlag
102  const ParseErrorCallback &cb = NULL);
103  bool FromStringInsitu(string *json_string,
104  const ParseErrorCallback &cb = NULL);
105  // Deprecated. Please use FromString(..., kAllowComment)
106  DEPRECATED(
107  bool FromStringWithComments(const string &json_string,
108  const ParseErrorCallback &cb = NULL));
109  bool FromString(const char *json_string,
110  const ParseErrorCallback &cb = NULL);
111  bool FromStringInsitu(char *json_string,
112  const ParseErrorCallback &cb = NULL);
113  bool FromString(const char *json_string,
114  size_t len,
115  const ParseErrorCallback &cb = NULL);
116  bool FromStringInsitu(char *json_string,
117  size_t len,
118  const ParseErrorCallback &cb = NULL);
119  bool FromFile(const string &file_path,
120  const ParseErrorCallback &cb = NULL);
121  bool FromFileWithComments(const string &file_path,
122  const ParseErrorCallback &cb = NULL);
123  // NOLINT(runtime/references)
124  bool FromStream(std::istream &input_stream,
125  const ParseErrorCallback &cb = NULL);
126  // Deprecated. Please use FromStream(..., kAllowComment)
127  // NOLINT(runtime/references)
128  DEPRECATED(
129  bool FromStreamWithComments(std::istream &input_stream,
130  const ParseErrorCallback &cb = NULL));
131  // NOLINT(runtime/references)
132  bool FromStream(std::istream &input_stream,
133  int parser_flag, // see ParserFlag
134  const ParseErrorCallback &cb = NULL);
135 
136  string ToString(bool pretty = false) const;
137  void ToString(string *str, bool pretty = false) const;
138  void ToString(Buffer *buffer, bool pretty = false) const;
139  void ToString(std::ostream *stream, bool pretty = false) const;
140 
141  // type check
142  Type type() const;
143  bool IsNull() const;
144  bool IsBool() const;
145  bool IsInteger() const;
146  bool IsDouble() const;
147  bool IsString() const;
148  bool IsArray() const;
149  bool IsObject() const;
150 
151  // assignment operators
152  Json &operator=(bool value);
153  Json &operator=(int value);
154  Json &operator=(int64_t value);
155  Json &operator=(double value);
156  Json &operator=(const char *value);
157  Json &operator=(const string &value);
158  Json &operator=(const Json &value);
159  Json &operator=(Json &&value);
160 
161  // null type
162  void SetNull();
163 
164  // boolean type
165  void SetBool(bool value);
166  bool GetBool() const;
167 
168  // integer type
169  void SetInteger(int64_t value);
170  int64_t GetInteger() const;
171 
172  // double type
173  void SetDouble(double value);
174  double GetDouble() const;
175 
176  // string type
177  void SetString(const string &value);
178  string GetString() const;
179 
180  // array type
181  void SetArray();
182  size_t Size() const;
183  const Json &operator[](size_t index) const;
184  Json &operator[](size_t index);
185 
186  void PushBack();
187  void PushBack(bool value);
188  void PushBack(int value);
189  void PushBack(int64_t value);
190  void PushBack(double value);
191  void PushBack(const char *value);
192  void PushBack(const string &value);
193  void PushBack(const Json &value);
194 
195  void RemoveElementByIndex(size_t index);
196  ValueIterator RemoveElement(ValueIterator iterator);
197  void RemoveAllElements();
198 
199  ValueIterator Begin();
200  ValueIterator End();
201  ConstValueIterator Begin() const;
202  ConstValueIterator End() const;
203 
204  // for object type
205  void SetObject();
206  const Json &operator[](const string &attr_name) const;
207  Json &operator[](const string &attr_name);
208 
209  bool HasAttribute(const string &attr_name) const;
210  bool HasAttribute(const string &attr_name, Type type) const;
211 
212  void AddAttribute(const string &attr_name, bool value);
213  void AddAttribute(const string &attr_name, int value);
214  void AddAttribute(const string &attr_name, int64_t value);
215  void AddAttribute(const string &attr_name, double value);
216  void AddAttribute(const string &attr_name, const char *value);
217  void AddAttribute(const string &attr_name, const string &value);
218  void AddAttribute(const string &attr_name, const Json &value);
219 
220  const Json &GetAttribute(const string &attr_name) const;
221  Json &GetAttribute(const string &attr_name);
222 
223  AttributeIterator FindAttribute(const string &attr_name);
224  ConstAttributeIterator FindAttribute(const string &attr_name) const;
225 
226  bool RemoveAttribute(const string &attr_name);
227  AttributeIterator RemoveAttribute(AttributeIterator iterator);
228  void RemoveAllAttributes();
229 
230  size_t MemberCount() const;
231  AttributeIterator AttributeBegin();
232  AttributeIterator AttributeEnd();
233  ConstAttributeIterator AttributeBegin() const;
234  ConstAttributeIterator AttributeEnd() const;
235 };
236 
237 
238 FUNAPI_DLL_VISIBILITY bool IsEqual(const Json &value1, const Json &value2);
239 FUNAPI_DLL_VISIBILITY bool operator==(const Json &lhs, const Json &rhs);
240 FUNAPI_DLL_VISIBILITY bool operator!=(const Json &lhs, const Json &rhs);
241 FUNAPI_DLL_VISIBILITY bool operator<(const Json &lhs, const Json &rhs);
242 
243 FUNAPI_DLL_VISIBILITY const string &ToString(Json::Type type);
244 FUNAPI_DLL_VISIBILITY
245 std::ostream &operator<<(std::ostream &out, Json::Type type);
246 
247 } // namespace fun
248 
249 #endif // INCLUDE_FUNAPI_COMMON_JSON_H_
Definition: json.h:31
Definition: json.h:18
Definition: json.h:27
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:89