Argo  1.0
A C++ library for handling JSON.
json.hpp
Go to the documentation of this file.
1 #ifndef _json_json_hpp_
2 #define _json_json_hpp_
3 
4 /*
5  * Copyright (c) 2017 Andrew Haisley
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
27 
28 #include <memory>
29 #include <map>
30 #include <vector>
31 
32 #include "common.hpp"
33 #include "pointer.hpp"
34 
35 namespace NAMESPACE
36 {
63  class json
64  {
65  public:
66 
67  // Convenience definition for creating null instances
68  typedef decltype(nullptr) null_t;
69 
73  typedef enum
74  {
75  object_e,
76  array_e,
77  boolean_e,
78  null_e,
79  number_int_e,
80  number_double_e,
81  string_e
82  }
83  type;
84 
88  json() noexcept;
89 
93  ~json();
94 
99  json(const json &other);
100 
104  json(json &&other) noexcept;
105 
117  json(type t);
118 
132  json(type t, const std::string &raw_value);
133 
137  json(int i);
138 
142  json(double d);
143 
147  json(bool b);
148 
152  json(const std::string &s);
153 
157  json(const char *s);
158 
162  json(null_t) noexcept;
163 
170  json(std::unique_ptr<std::string> s);
171 
175  json &operator=(const json &other);
176 
181  json &operator=(json &&other) noexcept;
182 
187  json &operator=(int i);
188 
193  json &operator=(double d);
194 
199  json &operator=(bool b);
200 
205  json &operator=(const char *s);
206 
211  json &operator=(const std::string &s);
212 
219  json &operator=(std::unique_ptr<std::string> s);
220 
226  json &operator=(const std::map<std::string, std::unique_ptr<json>> &o);
227 
233  json &operator=(const std::vector<std::unique_ptr<json>> &a);
234 
240  json &operator=(null_t);
241 
245  type get_instance_type() const;
246 
250  const char *get_instance_type_name() const;
251 
256  std::vector<std::unique_ptr<json>> &get_array();
257 
262  const std::vector<std::unique_ptr<json>> &get_array() const;
263 
268  std::map<std::string, std::unique_ptr<json>> &get_object();
269 
274  const std::map<std::string, std::unique_ptr<json>> &get_object() const;
275 
280  operator int() const;
281 
286  operator double() const;
287 
292  operator const std::string&() const;
293 
298  operator bool() const;
299 
307  json &operator[](const std::string &name);
308 
316  json &operator[](const char *name);
317 
324  const json &operator[](const std::string &name) const;
325 
332  const json &operator[](const char *name) const;
333 
340  json &operator[](size_t index);
341 
350  json &operator[](int index);
351 
358  const json &operator[](size_t index) const;
359 
367  const json &operator[](int index) const;
368 
375  const json &append(const json &j);
376 
387  const json &append(std::unique_ptr<json> j);
388 
396  const json &insert(const std::string &name, const json &j);
397 
408  const json &insert(const std::string &name, std::unique_ptr<json> j);
409 
414  const std::string &get_raw_value() const;
415 
420  bool operator==(const json &other) const;
421 
426  bool operator!=(const json &other) const;
427 
435  bool operator<(const json &other) const;
436 
444  bool operator<=(const json &other) const;
445 
453  bool operator>(const json &other) const;
454 
462  bool operator>=(const json &other) const;
463 
468  bool operator==(int i) const;
471  bool operator==(double d) const;
473  bool operator==(const std::string &s) const;
475  bool operator==(const char *s) const;
477  bool operator!=(int i) const;
479  bool operator!=(double d) const;
481  bool operator!=(const std::string &s) const;
483  bool operator!=(const char *s) const;
485  bool operator<(int i) const;
487  bool operator<(double d) const;
489  bool operator<(const std::string &s) const;
491  bool operator<(const char *s) const;
493  bool operator<=(int i) const;
495  bool operator<=(double d) const;
497  bool operator<=(const std::string &s) const;
499  bool operator<=(const char *s) const;
501  bool operator>(int i) const;
503  bool operator>(double d) const;
505  bool operator>(const std::string &s) const;
507  bool operator>(const char *s) const;
509  bool operator>=(int i) const;
511  bool operator>=(double d) const;
513  bool operator>=(const std::string &s) const;
515  bool operator>=(const char *s) const;
516 
521  const json &find(const pointer &p) const;
522 
523  private:
524 
530  typedef union
531  {
533  std::map<std::string, std::unique_ptr<json>> *u_object;
535  std::vector<std::unique_ptr<json>> *u_array;
537  bool u_boolean;
539  int u_number_int;
541  double u_number_double;
543  std::string *u_string;
544  }
545  json_value;
546 
552  type m_type;
553 
555  json_value m_value;
556 
566  std::string m_raw_value;
567 
572  void reset();
573 
575  void copy_json(const json &other);
576 
578  void move_json(json &other);
579 
581  bool number_equal(const json &other) const;
582 
584  bool string_equal(const json &other) const;
585 
587  bool object_equal(const json &other) const;
588 
590  bool array_equal(const json &other) const;
591 
592  };
593 }
594 
595 #endif
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
Common defs needed everywhere and, as far as is possible, platform specific changes.
JSON pointers as per the RFT.
Definition: pointer.hpp:41
All json things are represented by instances of this class.
Definition: json.hpp:63
The pointer class.