geopm 3.1.1.dev456+g3ba31824
GEOPM - Global Extensible Open Power Manager
Loading...
Searching...
No Matches
geopm/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#ifndef JSON11_HPP_INCLUDE
52#define JSON11_HPP_INCLUDE
53#pragma once
54
55#include <string>
56#include <vector>
57#include <map>
58#include <memory>
59#include <initializer_list>
60
61#ifdef _MSC_VER
62 #if _MSC_VER <= 1800 // VS 2013
63 #ifndef noexcept
64 #define noexcept throw()
65 #endif
66
67 #ifndef snprintf
68 #define snprintf _snprintf_s
69 #endif
70 #endif
71#endif
72
73namespace json11 {
74
77};
78
79class JsonValue;
80
81class __attribute__((visibility("default"))) Json final {
82public:
83 // Types
84 enum Type {
85 NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
86 };
87
88 // Array and object typedefs
89 typedef std::vector<Json> array;
90 typedef std::map<std::string, Json> object;
91
92 // Constructors for the various types of JSON value.
93 Json() noexcept; // NUL
94 Json(std::nullptr_t) noexcept; // NUL
95 Json(double value); // NUMBER
96 Json(int value); // NUMBER
97 Json(bool value); // BOOL
98 Json(const std::string &value); // STRING
99 Json(std::string &&value); // STRING
100 Json(const char * value); // STRING
101 Json(const array &values); // ARRAY
102 Json(array &&values); // ARRAY
103 Json(const object &values); // OBJECT
104 Json(object &&values); // OBJECT
105
106 // Implicit constructor: anything with a to_json() function.
107 template <class T, class = decltype(&T::to_json)>
108 Json(const T & t) : Json(t.to_json()) {}
109
110 // Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
111 template <class M, typename std::enable_if<
112 std::is_constructible<std::string, decltype(std::declval<M>().begin()->first)>::value
113 && std::is_constructible<Json, decltype(std::declval<M>().begin()->second)>::value,
114 int>::type = 0>
115 Json(const M & m) : Json(object(m.begin(), m.end())) {}
116
117 // Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
118 template <class V, typename std::enable_if<
119 std::is_constructible<Json, decltype(*std::declval<V>().begin())>::value,
120 int>::type = 0>
121 Json(const V & v) : Json(array(v.begin(), v.end())) {}
122
123 // This prevents Json(some_pointer) from accidentally producing a bool. Use
124 // Json(bool(some_pointer)) if that behavior is desired.
125 Json(void *) = delete;
126
127 // Accessors
128 Type type() const;
129
130 bool is_null() const { return type() == NUL; }
131 bool is_number() const { return type() == NUMBER; }
132 bool is_bool() const { return type() == BOOL; }
133 bool is_string() const { return type() == STRING; }
134 bool is_array() const { return type() == ARRAY; }
135 bool is_object() const { return type() == OBJECT; }
136
137 // Return the enclosed value if this is a number, 0 otherwise. Note that json11 does not
138 // distinguish between integer and non-integer numbers - number_value() and int_value()
139 // can both be applied to a NUMBER-typed object.
140 double number_value() const;
141 int int_value() const;
142
143 // Return the enclosed value if this is a boolean, false otherwise.
144 bool bool_value() const;
145 // Return the enclosed string if this is a string, "" otherwise.
146 const std::string &string_value() const;
147 // Return the enclosed std::vector if this is an array, or an empty vector otherwise.
148 const array &array_items() const;
149 // Return the enclosed std::map if this is an object, or an empty map otherwise.
150 const object &object_items() const;
151
152 // Return a reference to arr[i] if this is an array, Json() otherwise.
153 const Json & operator[](size_t i) const;
154 // Return a reference to obj[key] if this is an object, Json() otherwise.
155 const Json & operator[](const std::string &key) const;
156
157 // Serialize.
158 void dump(std::string &out) const;
159 std::string dump() const {
160 std::string out;
161 dump(out);
162 return out;
163 }
164
165 // Parse. If parse fails, return Json() and assign an error message to err.
166 static Json parse(const std::string & in,
167 std::string & err,
168 JsonParse strategy = JsonParse::STANDARD);
169 static Json parse(const char * in,
170 std::string & err,
171 JsonParse strategy = JsonParse::STANDARD) {
172 if (in) {
173 return parse(std::string(in), err, strategy);
174 } else {
175 err = "null input";
176 return nullptr;
177 }
178 }
179 // Parse multiple objects, concatenated or separated by whitespace
180 static std::vector<Json> parse_multi(
181 const std::string & in,
182 std::string::size_type & parser_stop_pos,
183 std::string & err,
184 JsonParse strategy = JsonParse::STANDARD);
185
186 static inline std::vector<Json> parse_multi(
187 const std::string & in,
188 std::string & err,
189 JsonParse strategy = JsonParse::STANDARD) {
190 std::string::size_type parser_stop_pos;
191 return parse_multi(in, parser_stop_pos, err, strategy);
192 }
193
194 bool operator== (const Json &rhs) const;
195 bool operator< (const Json &rhs) const;
196 bool operator!= (const Json &rhs) const { return !(*this == rhs); }
197 bool operator<= (const Json &rhs) const { return !(rhs < *this); }
198 bool operator> (const Json &rhs) const { return (rhs < *this); }
199 bool operator>= (const Json &rhs) const { return !(*this < rhs); }
200
201 /* has_shape(types, err)
202 *
203 * Return true if this is a JSON object and, for each item in types, has a field of
204 * the given type. If not, return false and set err to a descriptive message.
205 */
206 typedef std::initializer_list<std::pair<std::string, Type>> shape;
207 bool has_shape(const shape & types, std::string & err) const;
208
209private:
210 std::shared_ptr<JsonValue> m_ptr;
211};
212
213// Internal class hierarchy - JsonValue objects are not exposed to users of this API.
214class JsonValue {
215protected:
216 friend class Json;
217 friend class JsonInt;
218 friend class JsonDouble;
219 virtual Json::Type type() const = 0;
220 virtual bool equals(const JsonValue * other) const = 0;
221 virtual bool less(const JsonValue * other) const = 0;
222 virtual void dump(std::string &out) const = 0;
223 virtual double number_value() const;
224 virtual int int_value() const;
225 virtual bool bool_value() const;
226 virtual const std::string &string_value() const;
227 virtual const Json::array &array_items() const;
228 virtual const Json &operator[](size_t i) const;
229 virtual const Json::object &object_items() const;
230 virtual const Json &operator[](const std::string &key) const;
231 virtual ~JsonValue() {}
232};
233
234} // namespace json11
235#endif
Definition json11.hpp:214
virtual bool less(const JsonValue *other) const =0
virtual const Json & operator[](size_t i) const
virtual bool equals(const JsonValue *other) const =0
virtual bool bool_value() const
virtual void dump(std::string &out) const =0
virtual const Json::array & array_items() const
virtual ~JsonValue()
Definition geopm/json11.hpp:231
virtual int int_value() const
virtual const std::string & string_value() const
virtual Json::Type type() const =0
virtual const Json::object & object_items() const
virtual const Json & operator[](const std::string &key) const
virtual double number_value() const
Definition json11.hpp:73
JsonParse
Definition json11.hpp:75
@ STANDARD
Definition json11.hpp:76
@ COMMENTS
Definition json11.hpp:76
class __attribute__((visibility("default"))) Json final
Definition json11.hpp:81