comparison C++/Json.h @ 244:777bc3cb665a

Add Json, a jansson wrapper
author David Demelier <markand@malikania.fr>
date Sat, 13 Sep 2014 19:42:15 +0200
parents
children ed3cc10761e4
comparison
equal deleted inserted replaced
243:73e5381d7baf 244:777bc3cb665a
1 /*
2 * Json.h -- jansson C++11 wrapper
3 *
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef _JSON_H_
20 #define _JSON_H_
21
22 #include <initializer_list>
23 #include <memory>
24 #include <stdexcept>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 #include <unordered_map>
29
30 #include <jansson.h>
31
32 /**
33 * @file Json.h
34 * @brief A jansson C++ modern wrapper
35 */
36
37 /**
38 * @class Json
39 * @brief Json value
40 *
41 * This class contains one or more json values.
42 */
43 class Json final {
44 public:
45 using Handle = std::unique_ptr<json_t, void (*)(json_t *)>;
46 using Array = std::vector<Json>;
47 using Map = std::unordered_map<std::string, Json>;
48
49 /**
50 * @class Error
51 * @brief Json error
52 */
53 class Error : public std::exception {
54 private:
55 friend class Json;
56
57 std::string m_text;
58 std::string m_source;
59 int m_line;
60 int m_column;
61 unsigned m_position;
62
63 Error(const json_error_t error)
64 : m_text(error.text)
65 , m_source(error.source)
66 , m_line(error.line)
67 , m_column(error.column)
68 , m_position(error.position)
69 {
70 }
71
72 public:
73 const char *what() const noexcept override
74 {
75 return m_text.c_str();
76 }
77 };
78
79 private:
80 Handle m_handle;
81 Array m_list;
82 Map m_map;
83
84 Json(Handle &&handle)
85 : m_handle(std::move(handle))
86 {
87 }
88
89 template <typename Func, typename... Args>
90 static Json from(Func func, Args&&... args)
91 {
92 json_error_t error;
93 json_t *value = func(std::forward<Args>(args)..., &error);
94
95 if (!value)
96 throw Error(error);
97
98 return Json(Handle(value, json_decref));
99 }
100
101 public:
102 /**
103 * Load data from a string.
104 *
105 * @param data the data
106 * @param flags the optional flags
107 * @return the json value
108 * @throw Error on failures
109 */
110 static Json fromString(const std::string &data, int flags = 0);
111
112 /**
113 * Load data from a file.
114 *
115 * @param path the path
116 * @param flags the optional flags
117 * @return the json value
118 * @throw Error on failures
119 */
120 static Json fromFile(const std::string &path, int flags = 0);
121
122 /**
123 * Create a json value of type JSON_OBJECT.
124 */
125 explicit Json();
126
127 /**
128 * Create a JSON_NULL object.
129 *
130 * @param n the null value (nullptr)
131 */
132 explicit Json(std::nullptr_t n);
133
134 /**
135 * Create a boolean object.
136 *
137 * @param value the boolean value
138 */
139 explicit Json(bool value);
140
141 /**
142 * Create a JSON_INTEGER object.
143 *
144 * @param value the value
145 */
146 explicit Json(int value);
147
148 /**
149 * Create a JSON_REAL object.
150 *
151 * @param value the value
152 */
153 explicit Json(double value);
154
155 /**
156 * Create a JSON_STRING object.
157 *
158 * @param value the value
159 */
160 explicit Json(const std::string &value);
161
162 /**
163 * Create a JSON_ARRAY object.
164 *
165 * @param list the list of children
166 */
167 explicit Json(std::initializer_list<Json> list);
168
169 /**
170 * Create a JSON_STRING object.
171 *
172 * @param str the string
173 */
174 template <size_t N>
175 explicit Json(const char str[N])
176 : m_handle(json_string(str), json_decref)
177 {
178 }
179
180 /**
181 * Copy the object, this does a deep copy.
182 *
183 * @param json the other value
184 */
185 Json(const Json &json);
186
187 /**
188 * Copy the object, this does a deep copy.
189 *
190 * @param json the other value
191 * @return *this
192 */
193 Json &operator=(const Json &json);
194
195 /**
196 * Move the object.
197 *
198 * @param json the other value
199 */
200 Json(Json &&);
201
202 /**
203 * Move the object.
204 *
205 * @param json the other value
206 * @return *this
207 */
208 Json &operator=(Json &&);
209
210 /**
211 * Get the type of value.
212 *
213 * @return the type
214 */
215 int typeOf() const;
216
217 /**
218 * Tells if the json value is an JSON_OBJECT.
219 *
220 * @return true or false
221 */
222 bool isObject() const;
223
224 /**
225 * Tells if the json value is an JSON_ARRAY.
226 *
227 * @return true or false
228 */
229 bool isArray() const;
230
231 /**
232 * Tells if the json value is an JSON_STRING.
233 *
234 * @return true or false
235 */
236 bool isString() const;
237
238 /**
239 * Tells if the json value is an JSON_REAL.
240 *
241 * @return true or false
242 */
243 bool isReal() const;
244
245 /**
246 * Tells if the json value is an JSON_TRUE.
247 *
248 * @return true or false
249 */
250 bool isTrue() const;
251
252 /**
253 * Tells if the json value is an JSON_FALSE.
254 *
255 * @return true or false
256 */
257 bool isFalse() const;
258
259 /**
260 * Tells if the json value is an JSON_NULL.
261 *
262 * @return true or false
263 */
264 bool isNull() const;
265
266 /**
267 * Tells if the json value is an JSON_INTEGER or JSON_REAL.
268 *
269 * @return true or false
270 */
271 bool isNumber() const;
272
273 /**
274 * Tells if the json value is an JSON_INTEGER.
275 *
276 * @return true or false
277 */
278 bool isInteger() const;
279
280 /**
281 * Tells if the json value is an JSON_TRUE or JSON_FALSE.
282 *
283 * @return true or false
284 */
285 bool isBoolean() const;
286
287 /**
288 * Get the number of values in the array
289 *
290 * @return the number or 0
291 */
292 unsigned size() const noexcept;
293
294 /**
295 * Insert a copy of the value at the end.
296 *
297 * @param value the value to insert
298 */
299 void append(const Json &value);
300
301 /**
302 * Move the value at the end.
303 *
304 * @param value the value to insert
305 */
306 void append(Json &&value);
307
308 /**
309 * Insert a copy of the value at the specified index.
310 *
311 * @param value the value to insert
312 * @param index the position
313 */
314 void insert(const Json &value, int index);
315
316 /**
317 * Move the value at the specified index.
318 *
319 * @param value the value to insert
320 * @param index the position
321 */
322 void insert(Json &&value, int index);
323
324 /**
325 * Set a copy of value to the key 'name'.
326 *
327 * @param value the value
328 * @param name the key name.
329 */
330 void set(const Json &value, const std::string &name);
331
332 /**
333 * Move value to the key 'name'.
334 *
335 * @param value the value
336 * @param name the key name.
337 */
338 void set(Json &&value, const std::string &name);
339
340 /**
341 * Get the string value.
342 *
343 * @return the string
344 */
345 std::string toString() const;
346
347 /**
348 * Get the integer value.
349 *
350 * @return the value or 0
351 */
352 int toInteger() const noexcept;
353
354 /**
355 * Get the real value.
356 *
357 * @return the value or 0
358 */
359 double toReal() const noexcept;
360
361 /**
362 * Dump the value as a string.
363 *
364 * @param flags the optional flags
365 * @return the string
366 */
367 std::string dump(int flags = 0);
368
369 /**
370 * Dump the value to a path.
371 *
372 * @param path the path
373 * @param flags the optional flags
374 */
375 void dump(const std::string &path, int flags = 0);
376
377 /**
378 * Get the value at the specified index.
379 *
380 * @param index the position
381 * @return the reference to the value
382 * @throw std::invalid_argument on error
383 * @throw std::out_of_range on bad arguments
384 */
385 Json &operator[](int index);
386
387 /**
388 * Get the value at the specified index.
389 *
390 * @param index the position
391 * @return the reference to the value
392 * @throw std::invalid_argument on error
393 * @throw std::out_of_range on bad arguments
394 */
395 const Json &operator[](int index) const;
396
397 /**
398 * Get the value at the specified key.
399 *
400 * @param name the key
401 * @return the reference to the value
402 * @throw std::invalid_argument on error
403 * @throw std::out_of_range on bad arguments
404 */
405 Json &operator[](const std::string &name);
406
407 /**
408 * Get the value at the specified key.
409 *
410 * @param name the key
411 * @return the reference to the value
412 * @throw std::invalid_argument on error
413 * @throw std::out_of_range on bad arguments
414 */
415 const Json &operator[](const std::string &name) const;
416
417 /**
418 * Compare the values.
419 *
420 * @param j1 the first value
421 * @param j2 the second value
422 */
423 friend bool operator==(const Json &j1, const Json &j2);
424 };
425
426 #endif // !_JSON_H_