COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
json11.hpp
Go to the documentation of this file.
1 /* json11
2  *
3  * json11 is a tiny JSON library for C++11, providing JSON parsing and serialization.
4  *
5  * The core object provided by the library is json11::Json. A Json object represents any JSON
6  * value: null, bool, number (int or double), string (std::string), array (std::vector), or
7  * object (std::map).
8  *
9  * Json objects act like values: they can be assigned, copied, moved, compared for equality or
10  * order, etc. There are also helper methods Json::dump, to serialize a Json to a string, and
11  * Json::parse (static) to parse a std::string as a Json object.
12  *
13  * Internally, the various types of Json object are represented by the JsonValue class
14  * hierarchy.
15  *
16  * A note on numbers - JSON specifies the syntax of number formatting but not its semantics,
17  * so some JSON implementations distinguish between integers and floating-point numbers, while
18  * some don't. In json11, we choose the latter. Because some JSON implementations (namely
19  * Javascript itself) treat all numbers as the same type, distinguishing the two leads
20  * to JSON that will be *silently* changed by a round-trip through those implementations.
21  * Dangerous! To avoid that risk, json11 stores all numbers as double internally, but also
22  * provides integer helpers.
23  *
24  * Fortunately, double-precision IEEE754 ('double') can precisely store any integer in the
25  * range +/-2^53, which includes every 'int' on most systems. (Timestamps often use int64
26  * or long long to avoid the Y2038K problem; a double storing microseconds since some epoch
27  * will be exact for +/- 275 years.)
28  */
29 
30 /* Copyright (c) 2013 Dropbox, Inc.
31  *
32  * Permission is hereby granted, free of charge, to any person obtaining a copy
33  * of this software and associated documentation files (the "Software"), to deal
34  * in the Software without restriction, including without limitation the rights
35  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
36  * copies of the Software, and to permit persons to whom the Software is
37  * furnished to do so, subject to the following conditions:
38  *
39  * The above copyright notice and this permission notice shall be included in
40  * all copies or substantial portions of the Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
48  * THE SOFTWARE.
49  */
50 
51 #pragma once
52 
53 #include <string>
54 #include <vector>
55 #include <map>
56 #include <memory>
57 #include <initializer_list>
58 
59 #ifdef _MSC_VER
60  #if _MSC_VER <= 1800 // VS 2013
61  #ifndef noexcept
62  #define noexcept throw()
63  #endif
64 
65  #ifndef snprintf
66  #define snprintf _snprintf_s
67  #endif
68  #endif
69 #endif
70 
71 namespace json11 {
72 
73 enum JsonParse {
75 };
76 
77 class JsonValue;
78 
79 class Json final {
80 public:
81  // Types
82  enum Type {
84  };
85 
86  // Array and object typedefs
87  typedef std::vector<Json> array;
88  typedef std::map<std::string, Json> object;
89 
90  // Constructors for the various types of JSON value.
91  Json() noexcept; // NUL
92  Json(std::nullptr_t) noexcept; // NUL
93  Json(double value); // NUMBER
94  Json(int value); // NUMBER
95  Json(bool value); // BOOL
96  Json(const std::string &value); // STRING
97  Json(std::string &&value); // STRING
98  Json(const char * value); // STRING
99  Json(const array &values); // ARRAY
100  Json(array &&values); // ARRAY
101  Json(const object &values); // OBJECT
102  Json(object &&values); // OBJECT
103 
104  // Implicit constructor: anything with a to_json() function.
105  template <class T, class = decltype(&T::to_json)>
106  Json(const T & t) : Json(t.to_json()) {}
107 
108  // Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
109  template <class M, typename std::enable_if<
110  std::is_constructible<std::string, decltype(std::declval<M>().begin()->first)>::value
111  && std::is_constructible<Json, decltype(std::declval<M>().begin()->second)>::value,
112  int>::type = 0>
113  Json(const M & m) : Json(object(m.begin(), m.end())) {}
114 
115  // Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
116  template <class V, typename std::enable_if<
117  std::is_constructible<Json, decltype(*std::declval<V>().begin())>::value,
118  int>::type = 0>
119  Json(const V & v) : Json(array(v.begin(), v.end())) {}
120 
121  // This prevents Json(some_pointer) from accidentally producing a bool. Use
122  // Json(bool(some_pointer)) if that behavior is desired.
123  Json(void *) = delete;
124 
125  // Accessors
126  Type type() const;
127  std::string type_name() const;
128 
129  bool is_null() const { return type() == NUL; }
130  bool is_number() const { return type() == NUMBER; }
131  bool is_bool() const { return type() == BOOL; }
132  bool is_string() const { return type() == STRING; }
133  bool is_array() const { return type() == ARRAY; }
134  bool is_object() const { return type() == OBJECT; }
135 
136  // Return the enclosed value if this is a number, 0 otherwise. Note that json11 does not
137  // distinguish between integer and non-integer numbers - number_value() and int_value()
138  // can both be applied to a NUMBER-typed object.
139  double number_value() const;
140  int int_value() const;
141 
142  // Return the enclosed value if this is a boolean, false otherwise.
143  bool bool_value() const;
144  // Return the enclosed string if this is a string, "" otherwise.
145  const std::string &string_value() const;
146  // Return the enclosed std::vector if this is an array, or an empty vector otherwise.
147  const array &array_items() const;
148  // Return the enclosed std::map if this is an object, or an empty map otherwise.
149  const object &object_items() const;
150 
151  // Return a reference to arr[i] if this is an array, Json() otherwise.
152  const Json & operator[](size_t i) const;
153  // Return a reference to obj[key] if this is an object, Json() otherwise.
154  const Json & operator[](const std::string &key) const;
155 
156  // Serialize.
157  void dump(std::string &out) const;
158  std::string dump() const {
159  std::string out;
160  dump(out);
161  return out;
162  }
163 
164  // Parse. If parse fails, return Json() and assign an error message to err.
165  static Json parse(const std::string & in,
166  std::string & err,
168  static Json parse(const char * in,
169  std::string & err,
171  if (in) {
172  return parse(std::string(in), err, strategy);
173  } else {
174  err = "null input";
175  return nullptr;
176  }
177  }
178  // Parse multiple objects, concatenated or separated by whitespace
179  static std::vector<Json> parse_multi(
180  const std::string & in,
181  std::string::size_type & parser_stop_pos,
182  std::string & err,
184 
185  static inline std::vector<Json> parse_multi(
186  const std::string & in,
187  std::string & err,
189  std::string::size_type parser_stop_pos;
190  return parse_multi(in, parser_stop_pos, err, strategy);
191  }
192 
193  bool operator== (const Json &rhs) const;
194  bool operator< (const Json &rhs) const;
195  bool operator!= (const Json &rhs) const { return !(*this == rhs); }
196  bool operator<= (const Json &rhs) const { return !(rhs < *this); }
197  bool operator> (const Json &rhs) const { return (rhs < *this); }
198  bool operator>= (const Json &rhs) const { return !(*this < rhs); }
199 
200  /* has_shape(types, err)
201  *
202  * Return true if this is a JSON object and, for each item in types, has a field of
203  * the given type. If not, return false and set err to a descriptive message.
204  */
205  typedef std::initializer_list<std::pair<std::string, Type>> shape;
206  bool has_shape(const shape & types, std::string & err) const;
207 
208 private:
209  std::shared_ptr<JsonValue> m_ptr;
210 };
211 
212 // Internal class hierarchy - JsonValue objects are not exposed to users of this API.
213 class JsonValue {
214 protected:
215  friend class Json;
216  friend class JsonInt;
217  friend class JsonDouble;
218  virtual Json::Type type() const = 0;
219  virtual bool equals(const JsonValue * other) const = 0;
220  virtual bool less(const JsonValue * other) const = 0;
221  virtual void dump(std::string &out) const = 0;
222  virtual double number_value() const;
223  virtual int int_value() const;
224  virtual bool bool_value() const;
225  virtual const std::string &string_value() const;
226  virtual const Json::array &array_items() const;
227  virtual const Json &operator[](size_t i) const;
228  virtual const Json::object &object_items() const;
229  virtual const Json &operator[](const std::string &key) const;
230  virtual ~JsonValue() {}
231 };
232 
233 } // namespace json11
friend class JsonDouble
Definition: json11.hpp:217
std::initializer_list< std::pair< std::string, Type > > shape
Definition: json11.hpp:205
std::map< std::string, Json > object
Definition: json11.hpp:88
static std::vector< Json > parse_multi(const std::string &in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
Definition: json11.hpp:185
Definition: json11.hpp:79
bool operator!=(const Json &rhs) const
Definition: json11.hpp:195
Definition: json11.hpp:83
int i
Definition: rw_test.cpp:37
virtual ~JsonValue()
Definition: json11.hpp:230
bool operator>(const Json &rhs) const
Definition: json11.hpp:197
std::string type_name() const
Definition: json11.cpp:271
bool operator==(const Json &rhs) const
Definition: json11.cpp:311
Definition: json11.hpp:74
Type type() const
Definition: json11.cpp:270
static Json parse(const char *in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
Definition: json11.hpp:168
Type
Definition: json11.hpp:82
const Json & operator[](size_t i) const
Definition: json11.cpp:286
JsonParse
Definition: json11.hpp:73
Definition: json11.hpp:83
static Json parse(const std::string &in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
std::string dump() const
Definition: json11.hpp:158
Definition: json11.hpp:74
const std::string & string_value() const
Definition: json11.cpp:283
bool is_string() const
Definition: json11.hpp:132
bool bool_value() const
Definition: json11.cpp:282
const array & array_items() const
Definition: json11.cpp:284
friend class JsonInt
Definition: json11.hpp:216
bool operator<=(const Json &rhs) const
Definition: json11.hpp:196
bool is_array() const
Definition: json11.hpp:133
Json(const M &m)
Definition: json11.hpp:113
Definition: json11.cpp:29
string & err
Definition: json11.cpp:362
Definition: json11.hpp:83
std::vector< Json > array
Definition: json11.hpp:87
bool has_shape(const shape &types, std::string &err) const
Definition: json11.cpp:812
static std::vector< Json > parse_multi(const std::string &in, std::string::size_type &parser_stop_pos, std::string &err, JsonParse strategy=JsonParse::STANDARD)
Definition: json11.hpp:213
Definition: json11.hpp:83
bool operator>=(const Json &rhs) const
Definition: json11.hpp:198
bool is_number() const
Definition: json11.hpp:130
bool operator<(const Json &rhs) const
Definition: json11.cpp:320
Definition: json11.hpp:83
bool is_bool() const
Definition: json11.hpp:131
Json() noexcept
Definition: json11.cpp:253
const JsonParse strategy
Definition: json11.cpp:364
friend class Json
Definition: json11.hpp:215
#define const
Definition: zconf.h:217
Json(const V &v)
Definition: json11.hpp:119
bool is_object() const
Definition: json11.hpp:134
int int_value() const
Definition: json11.cpp:281
string to_json(string key, string value)
Definition: stringlib.cpp:334
std::shared_ptr< JsonValue > m_ptr
Definition: json11.hpp:209
double number_value() const
Definition: json11.cpp:280
Definition: json11.hpp:83
bool is_null() const
Definition: json11.hpp:129
const object & object_items() const
Definition: json11.cpp:285