1 /* Copyright (c) 2013 Dropbox, Inc.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22 #include "json11.hpp"
23 #include <cassert>
24 #include <cmath>
25 #include <cstdlib>
26 #include <cstdio>
27 #include <limits>
28 #include <string>
29
30 namespace json11 {
31
32 static const int max_depth = 200;
33
34 using std::string;
35 using std::vector;
36 using std::map;
37 using std::make_shared;
38 using std::initializer_list;
39 using std::move;
40
41 /* Helper for representing null - just a do-nothing struct, plus comparison
42 * operators so the helpers in JsonValue work. We can't use nullptr_t because
43 * it may not be orderable.
44 */
45 struct NullStruct {
operator ==json11::NullStruct46 bool operator==(NullStruct) const { return true; }
operator <json11::NullStruct47 bool operator<(NullStruct) const { return false; }
48 };
49
50 /* * * * * * * * * * * * * * * * * * * *
51 * Serialization
52 */
53
dump(NullStruct,string & out)54 static void dump(NullStruct, string &out) {
55 out += "null";
56 }
57
dump(double value,string & out)58 static void dump(double value, string &out) {
59 if (std::isfinite(value)) {
60 char buf[32];
61 snprintf(buf, sizeof buf, "%.17g", value);
62 out += buf;
63 } else {
64 out += "null";
65 }
66 }
67
dump(int value,string & out)68 static void dump(int value, string &out) {
69 char buf[32];
70 snprintf(buf, sizeof buf, "%d", value);
71 out += buf;
72 }
73
dump(bool value,string & out)74 static void dump(bool value, string &out) {
75 out += value ? "true" : "false";
76 }
77
dump(const string & value,string & out)78 static void dump(const string &value, string &out) {
79 out += '"';
80 for (size_t i = 0; i < value.length(); i++) {
81 const char ch = value[i];
82 if (ch == '\\') {
83 out += "\\\\";
84 } else if (ch == '"') {
85 out += "\\\"";
86 } else if (ch == '\b') {
87 out += "\\b";
88 } else if (ch == '\f') {
89 out += "\\f";
90 } else if (ch == '\n') {
91 out += "\\n";
92 } else if (ch == '\r') {
93 out += "\\r";
94 } else if (ch == '\t') {
95 out += "\\t";
96 } else if (static_cast<uint8_t>(ch) <= 0x1f) {
97 char buf[8];
98 snprintf(buf, sizeof buf, "\\u%04x", ch);
99 out += buf;
100 } else if (static_cast<uint8_t>(ch) == 0xe2 && static_cast<uint8_t>(value[i+1]) == 0x80
101 && static_cast<uint8_t>(value[i+2]) == 0xa8) {
102 out += "\\u2028";
103 i += 2;
104 } else if (static_cast<uint8_t>(ch) == 0xe2 && static_cast<uint8_t>(value[i+1]) == 0x80
105 && static_cast<uint8_t>(value[i+2]) == 0xa9) {
106 out += "\\u2029";
107 i += 2;
108 } else {
109 out += ch;
110 }
111 }
112 out += '"';
113 }
114
dump(const Json::array & values,string & out)115 static void dump(const Json::array &values, string &out) {
116 bool first = true;
117 out += "[";
118 for (const auto &value : values) {
119 if (!first)
120 out += ", ";
121 value.dump(out);
122 first = false;
123 }
124 out += "]";
125 }
126
dump(const Json::object & values,string & out)127 static void dump(const Json::object &values, string &out) {
128 bool first = true;
129 out += "{";
130 for (const auto &kv : values) {
131 if (!first)
132 out += ", ";
133 dump(kv.first, out);
134 out += ": ";
135 kv.second.dump(out);
136 first = false;
137 }
138 out += "}";
139 }
140
dump(string & out) const141 void Json::dump(string &out) const {
142 m_ptr->dump(out);
143 }
144
145 /* * * * * * * * * * * * * * * * * * * *
146 * Value wrappers
147 */
148
149 template <Json::Type tag, typename T>
150 class Value : public JsonValue {
151 protected:
152
153 // Constructors
Value(const T & value)154 explicit Value(const T &value) : m_value(value) {}
Value(T && value)155 explicit Value(T &&value) : m_value(move(value)) {}
156
157 // Get type tag
type() const158 Json::Type type() const override {
159 return tag;
160 }
161
162 // Comparisons
equals(const JsonValue * other) const163 bool equals(const JsonValue * other) const override {
164 return m_value == static_cast<const Value<tag, T> *>(other)->m_value;
165 }
less(const JsonValue * other) const166 bool less(const JsonValue * other) const override {
167 return m_value < static_cast<const Value<tag, T> *>(other)->m_value;
168 }
169
170 const T m_value;
dump(string & out) const171 void dump(string &out) const override { json11::dump(m_value, out); }
172 };
173
174 class JsonDouble final : public Value<Json::NUMBER, double> {
number_value() const175 double number_value() const override { return m_value; }
int_value() const176 int int_value() const override { return static_cast<int>(m_value); }
equals(const JsonValue * other) const177 bool equals(const JsonValue * other) const override { return m_value == other->number_value(); }
less(const JsonValue * other) const178 bool less(const JsonValue * other) const override { return m_value < other->number_value(); }
179 public:
JsonDouble(double value)180 explicit JsonDouble(double value) : Value(value) {}
181 };
182
183 class JsonInt final : public Value<Json::NUMBER, int> {
number_value() const184 double number_value() const override { return m_value; }
int_value() const185 int int_value() const override { return m_value; }
equals(const JsonValue * other) const186 bool equals(const JsonValue * other) const override { return m_value == other->number_value(); }
less(const JsonValue * other) const187 bool less(const JsonValue * other) const override { return m_value < other->number_value(); }
188 public:
JsonInt(int value)189 explicit JsonInt(int value) : Value(value) {}
190 };
191
192 class JsonBoolean final : public Value<Json::BOOL, bool> {
bool_value() const193 bool bool_value() const override { return m_value; }
194 public:
JsonBoolean(bool value)195 explicit JsonBoolean(bool value) : Value(value) {}
196 };
197
198 class JsonString final : public Value<Json::STRING, string> {
string_value() const199 const string &string_value() const override { return m_value; }
200 public:
JsonString(const string & value)201 explicit JsonString(const string &value) : Value(value) {}
JsonString(string && value)202 explicit JsonString(string &&value) : Value(move(value)) {}
203 };
204
205 class JsonArray final : public Value<Json::ARRAY, Json::array> {
array_items() const206 const Json::array &array_items() const override { return m_value; }
207 const Json & operator[](size_t i) const override;
208 public:
JsonArray(const Json::array & value)209 explicit JsonArray(const Json::array &value) : Value(value) {}
JsonArray(Json::array && value)210 explicit JsonArray(Json::array &&value) : Value(move(value)) {}
211 };
212
213 class JsonObject final : public Value<Json::OBJECT, Json::object> {
object_items() const214 const Json::object &object_items() const override { return m_value; }
215 const Json & operator[](const string &key) const override;
216 public:
JsonObject(const Json::object & value)217 explicit JsonObject(const Json::object &value) : Value(value) {}
JsonObject(Json::object && value)218 explicit JsonObject(Json::object &&value) : Value(move(value)) {}
219 };
220
221 class JsonNull final : public Value<Json::NUL, NullStruct> {
222 public:
JsonNull()223 JsonNull() : Value({}) {}
224 };
225
226 /* * * * * * * * * * * * * * * * * * * *
227 * Static globals - static-init-safe
228 */
229 struct Statics {
230 const std::shared_ptr<JsonValue> null = make_shared<JsonNull>();
231 const std::shared_ptr<JsonValue> t = make_shared<JsonBoolean>(true);
232 const std::shared_ptr<JsonValue> f = make_shared<JsonBoolean>(false);
233 const string empty_string;
234 const vector<Json> empty_vector;
235 const map<string, Json> empty_map;
Staticsjson11::Statics236 Statics() {}
237 };
238
statics()239 static const Statics & statics() {
240 static const Statics s {};
241 return s;
242 }
243
static_null()244 static const Json & static_null() {
245 // This has to be separate, not in Statics, because Json() accesses statics().null.
246 static const Json json_null;
247 return json_null;
248 }
249
250 /* * * * * * * * * * * * * * * * * * * *
251 * Constructors
252 */
253
Json()254 Json::Json() noexcept : m_ptr(statics().null) {}
Json(std::nullptr_t)255 Json::Json(std::nullptr_t) noexcept : m_ptr(statics().null) {}
Json(double value)256 Json::Json(double value) : m_ptr(make_shared<JsonDouble>(value)) {}
Json(int value)257 Json::Json(int value) : m_ptr(make_shared<JsonInt>(value)) {}
Json(bool value)258 Json::Json(bool value) : m_ptr(value ? statics().t : statics().f) {}
Json(const string & value)259 Json::Json(const string &value) : m_ptr(make_shared<JsonString>(value)) {}
Json(string && value)260 Json::Json(string &&value) : m_ptr(make_shared<JsonString>(move(value))) {}
Json(const char * value)261 Json::Json(const char * value) : m_ptr(make_shared<JsonString>(value)) {}
Json(const Json::array & values)262 Json::Json(const Json::array &values) : m_ptr(make_shared<JsonArray>(values)) {}
Json(Json::array && values)263 Json::Json(Json::array &&values) : m_ptr(make_shared<JsonArray>(move(values))) {}
Json(const Json::object & values)264 Json::Json(const Json::object &values) : m_ptr(make_shared<JsonObject>(values)) {}
Json(Json::object && values)265 Json::Json(Json::object &&values) : m_ptr(make_shared<JsonObject>(move(values))) {}
266
267 /* * * * * * * * * * * * * * * * * * * *
268 * Accessors
269 */
270
type() const271 Json::Type Json::type() const { return m_ptr->type(); }
number_value() const272 double Json::number_value() const { return m_ptr->number_value(); }
int_value() const273 int Json::int_value() const { return m_ptr->int_value(); }
bool_value() const274 bool Json::bool_value() const { return m_ptr->bool_value(); }
string_value() const275 const string & Json::string_value() const { return m_ptr->string_value(); }
array_items() const276 const vector<Json> & Json::array_items() const { return m_ptr->array_items(); }
object_items() const277 const map<string, Json> & Json::object_items() const { return m_ptr->object_items(); }
operator [](size_t i) const278 const Json & Json::operator[] (size_t i) const { return (*m_ptr)[i]; }
operator [](const string & key) const279 const Json & Json::operator[] (const string &key) const { return (*m_ptr)[key]; }
280
number_value() const281 double JsonValue::number_value() const { return 0; }
int_value() const282 int JsonValue::int_value() const { return 0; }
bool_value() const283 bool JsonValue::bool_value() const { return false; }
string_value() const284 const string & JsonValue::string_value() const { return statics().empty_string; }
array_items() const285 const vector<Json> & JsonValue::array_items() const { return statics().empty_vector; }
object_items() const286 const map<string, Json> & JsonValue::object_items() const { return statics().empty_map; }
operator [](size_t) const287 const Json & JsonValue::operator[] (size_t) const { return static_null(); }
operator [](const string &) const288 const Json & JsonValue::operator[] (const string &) const { return static_null(); }
289
operator [](const string & key) const290 const Json & JsonObject::operator[] (const string &key) const {
291 auto iter = m_value.find(key);
292 return (iter == m_value.end()) ? static_null() : iter->second;
293 }
operator [](size_t i) const294 const Json & JsonArray::operator[] (size_t i) const {
295 if (i >= m_value.size()) return static_null();
296 else return m_value[i];
297 }
298
299 /* * * * * * * * * * * * * * * * * * * *
300 * Comparison
301 */
302
operator ==(const Json & other) const303 bool Json::operator== (const Json &other) const {
304 if (m_ptr->type() != other.m_ptr->type())
305 return false;
306
307 return m_ptr->equals(other.m_ptr.get());
308 }
309
operator <(const Json & other) const310 bool Json::operator< (const Json &other) const {
311 if (m_ptr->type() != other.m_ptr->type())
312 return m_ptr->type() < other.m_ptr->type();
313
314 return m_ptr->less(other.m_ptr.get());
315 }
316
317 /* * * * * * * * * * * * * * * * * * * *
318 * Parsing
319 */
320
321 /* esc(c)
322 *
323 * Format char c suitable for printing in an error message.
324 */
esc(char c)325 static inline string esc(char c) {
326 char buf[12];
327 if (static_cast<uint8_t>(c) >= 0x20 && static_cast<uint8_t>(c) <= 0x7f) {
328 snprintf(buf, sizeof buf, "'%c' (%d)", c, c);
329 } else {
330 snprintf(buf, sizeof buf, "(%d)", c);
331 }
332 return string(buf);
333 }
334
in_range(long x,long lower,long upper)335 static inline bool in_range(long x, long lower, long upper) {
336 return (x >= lower && x <= upper);
337 }
338
339 namespace {
340 /* JsonParser
341 *
342 * Object that tracks all state of an in-progress parse.
343 */
344 struct JsonParser final {
345
346 /* State
347 */
348 const string &str;
349 size_t i;
350 string &err;
351 bool failed;
352 const JsonParse strategy;
353
354 /* fail(msg, err_ret = Json())
355 *
356 * Mark this parse as failed.
357 */
failjson11::__anonfc6a1be70111::JsonParser358 Json fail(string &&msg) {
359 return fail(move(msg), Json());
360 }
361
362 template <typename T>
failjson11::__anonfc6a1be70111::JsonParser363 T fail(string &&msg, const T err_ret) {
364 if (!failed)
365 err = std::move(msg);
366 failed = true;
367 return err_ret;
368 }
369
370 /* consume_whitespace()
371 *
372 * Advance until the current character is non-whitespace.
373 */
consume_whitespacejson11::__anonfc6a1be70111::JsonParser374 void consume_whitespace() {
375 while (str[i] == ' ' || str[i] == '\r' || str[i] == '\n' || str[i] == '\t')
376 i++;
377 }
378
379 /* consume_comment()
380 *
381 * Advance comments (c-style inline and multiline).
382 */
consume_commentjson11::__anonfc6a1be70111::JsonParser383 bool consume_comment() {
384 bool comment_found = false;
385 if (str[i] == '/') {
386 i++;
387 if (i == str.size())
388 return fail("unexpected end of input inside comment", false);
389 if (str[i] == '/') { // inline comment
390 i++;
391 if (i == str.size())
392 return fail("unexpected end of input inside inline comment", false);
393 // advance until next line
394 while (str[i] != '\n') {
395 i++;
396 if (i == str.size())
397 return fail("unexpected end of input inside inline comment", false);
398 }
399 comment_found = true;
400 }
401 else if (str[i] == '*') { // multiline comment
402 i++;
403 if (i > str.size()-2)
404 return fail("unexpected end of input inside multi-line comment", false);
405 // advance until closing tokens
406 while (!(str[i] == '*' && str[i+1] == '/')) {
407 i++;
408 if (i > str.size()-2)
409 return fail(
410 "unexpected end of input inside multi-line comment", false);
411 }
412 i += 2;
413 if (i == str.size())
414 return fail(
415 "unexpected end of input inside multi-line comment", false);
416 comment_found = true;
417 }
418 else
419 return fail("malformed comment", false);
420 }
421 return comment_found;
422 }
423
424 /* consume_garbage()
425 *
426 * Advance until the current character is non-whitespace and non-comment.
427 */
consume_garbagejson11::__anonfc6a1be70111::JsonParser428 void consume_garbage() {
429 consume_whitespace();
430 if(strategy == JsonParse::COMMENTS) {
431 bool comment_found = false;
432 do {
433 comment_found = consume_comment();
434 consume_whitespace();
435 }
436 while(comment_found);
437 }
438 }
439
440 /* get_next_token()
441 *
442 * Return the next non-whitespace character. If the end of the input is reached,
443 * flag an error and return 0.
444 */
get_next_tokenjson11::__anonfc6a1be70111::JsonParser445 char get_next_token() {
446 consume_garbage();
447 if (i == str.size())
448 return fail("unexpected end of input", (char)0);
449
450 return str[i++];
451 }
452
453 /* encode_utf8(pt, out)
454 *
455 * Encode pt as UTF-8 and add it to out.
456 */
encode_utf8json11::__anonfc6a1be70111::JsonParser457 void encode_utf8(long pt, string & out) {
458 if (pt < 0)
459 return;
460
461 if (pt < 0x80) {
462 out += static_cast<char>(pt);
463 } else if (pt < 0x800) {
464 out += static_cast<char>((pt >> 6) | 0xC0);
465 out += static_cast<char>((pt & 0x3F) | 0x80);
466 } else if (pt < 0x10000) {
467 out += static_cast<char>((pt >> 12) | 0xE0);
468 out += static_cast<char>(((pt >> 6) & 0x3F) | 0x80);
469 out += static_cast<char>((pt & 0x3F) | 0x80);
470 } else {
471 out += static_cast<char>((pt >> 18) | 0xF0);
472 out += static_cast<char>(((pt >> 12) & 0x3F) | 0x80);
473 out += static_cast<char>(((pt >> 6) & 0x3F) | 0x80);
474 out += static_cast<char>((pt & 0x3F) | 0x80);
475 }
476 }
477
478 /* parse_string()
479 *
480 * Parse a string, starting at the current position.
481 */
parse_stringjson11::__anonfc6a1be70111::JsonParser482 string parse_string() {
483 string out;
484 long last_escaped_codepoint = -1;
485 while (true) {
486 if (i == str.size())
487 return fail("unexpected end of input in string", "");
488
489 char ch = str[i++];
490
491 if (ch == '"') {
492 encode_utf8(last_escaped_codepoint, out);
493 return out;
494 }
495
496 if (in_range(ch, 0, 0x1f))
497 return fail("unescaped " + esc(ch) + " in string", "");
498
499 // The usual case: non-escaped characters
500 if (ch != '\\') {
501 encode_utf8(last_escaped_codepoint, out);
502 last_escaped_codepoint = -1;
503 out += ch;
504 continue;
505 }
506
507 // Handle escapes
508 if (i == str.size())
509 return fail("unexpected end of input in string", "");
510
511 ch = str[i++];
512
513 if (ch == 'u') {
514 // Extract 4-byte escape sequence
515 string esc = str.substr(i, 4);
516 // Explicitly check length of the substring. The following loop
517 // relies on std::string returning the terminating NUL when
518 // accessing str[length]. Checking here reduces brittleness.
519 if (esc.length() < 4) {
520 return fail("bad \\u escape: " + esc, "");
521 }
522 for (size_t j = 0; j < 4; j++) {
523 if (!in_range(esc[j], 'a', 'f') && !in_range(esc[j], 'A', 'F')
524 && !in_range(esc[j], '0', '9'))
525 return fail("bad \\u escape: " + esc, "");
526 }
527
528 long codepoint = strtol(esc.data(), nullptr, 16);
529
530 // JSON specifies that characters outside the BMP shall be encoded as a pair
531 // of 4-hex-digit \u escapes encoding their surrogate pair components. Check
532 // whether we're in the middle of such a beast: the previous codepoint was an
533 // escaped lead (high) surrogate, and this is a trail (low) surrogate.
534 if (in_range(last_escaped_codepoint, 0xD800, 0xDBFF)
535 && in_range(codepoint, 0xDC00, 0xDFFF)) {
536 // Reassemble the two surrogate pairs into one astral-plane character, per
537 // the UTF-16 algorithm.
538 encode_utf8((((last_escaped_codepoint - 0xD800) << 10)
539 | (codepoint - 0xDC00)) + 0x10000, out);
540 last_escaped_codepoint = -1;
541 } else {
542 encode_utf8(last_escaped_codepoint, out);
543 last_escaped_codepoint = codepoint;
544 }
545
546 i += 4;
547 continue;
548 }
549
550 encode_utf8(last_escaped_codepoint, out);
551 last_escaped_codepoint = -1;
552
553 if (ch == 'b') {
554 out += '\b';
555 } else if (ch == 'f') {
556 out += '\f';
557 } else if (ch == 'n') {
558 out += '\n';
559 } else if (ch == 'r') {
560 out += '\r';
561 } else if (ch == 't') {
562 out += '\t';
563 } else if (ch == '"' || ch == '\\' || ch == '/') {
564 out += ch;
565 } else {
566 return fail("invalid escape character " + esc(ch), "");
567 }
568 }
569 }
570
571 /* parse_number()
572 *
573 * Parse a double.
574 */
parse_numberjson11::__anonfc6a1be70111::JsonParser575 Json parse_number() {
576 size_t start_pos = i;
577
578 if (str[i] == '-')
579 i++;
580
581 // Integer part
582 if (str[i] == '0') {
583 i++;
584 if (in_range(str[i], '0', '9'))
585 return fail("leading 0s not permitted in numbers");
586 } else if (in_range(str[i], '1', '9')) {
587 i++;
588 while (in_range(str[i], '0', '9'))
589 i++;
590 } else {
591 return fail("invalid " + esc(str[i]) + " in number");
592 }
593
594 if (str[i] != '.' && str[i] != 'e' && str[i] != 'E'
595 && (i - start_pos) <= static_cast<size_t>(std::numeric_limits<int>::digits10)) {
596 return std::atoi(str.c_str() + start_pos);
597 }
598
599 // Decimal part
600 if (str[i] == '.') {
601 i++;
602 if (!in_range(str[i], '0', '9'))
603 return fail("at least one digit required in fractional part");
604
605 while (in_range(str[i], '0', '9'))
606 i++;
607 }
608
609 // Exponent part
610 if (str[i] == 'e' || str[i] == 'E') {
611 i++;
612
613 if (str[i] == '+' || str[i] == '-')
614 i++;
615
616 if (!in_range(str[i], '0', '9'))
617 return fail("at least one digit required in exponent");
618
619 while (in_range(str[i], '0', '9'))
620 i++;
621 }
622
623 return std::strtod(str.c_str() + start_pos, nullptr);
624 }
625
626 /* expect(str, res)
627 *
628 * Expect that 'str' starts at the character that was just read. If it does, advance
629 * the input and return res. If not, flag an error.
630 */
expectjson11::__anonfc6a1be70111::JsonParser631 Json expect(const string &expected, Json res) {
632 assert(i != 0);
633 i--;
634 if (str.compare(i, expected.length(), expected) == 0) {
635 i += expected.length();
636 return res;
637 } else {
638 return fail("parse error: expected " + expected + ", got " + str.substr(i, expected.length()));
639 }
640 }
641
642 /* parse_json()
643 *
644 * Parse a JSON object.
645 */
parse_jsonjson11::__anonfc6a1be70111::JsonParser646 Json parse_json(int depth) {
647 if (depth > max_depth) {
648 return fail("exceeded maximum nesting depth");
649 }
650
651 char ch = get_next_token();
652 if (failed)
653 return Json();
654
655 if (ch == '-' || (ch >= '0' && ch <= '9')) {
656 i--;
657 return parse_number();
658 }
659
660 if (ch == 't')
661 return expect("true", true);
662
663 if (ch == 'f')
664 return expect("false", false);
665
666 if (ch == 'n')
667 return expect("null", Json());
668
669 if (ch == '"')
670 return parse_string();
671
672 if (ch == '{') {
673 map<string, Json> data;
674 ch = get_next_token();
675 if (ch == '}')
676 return data;
677
678 while (1) {
679 if (ch != '"')
680 return fail("expected '\"' in object, got " + esc(ch));
681
682 string key = parse_string();
683 if (failed)
684 return Json();
685
686 ch = get_next_token();
687 if (ch != ':')
688 return fail("expected ':' in object, got " + esc(ch));
689
690 data[std::move(key)] = parse_json(depth + 1);
691 if (failed)
692 return Json();
693
694 ch = get_next_token();
695 if (ch == '}')
696 break;
697 if (ch != ',')
698 return fail("expected ',' in object, got " + esc(ch));
699
700 ch = get_next_token();
701 }
702 return data;
703 }
704
705 if (ch == '[') {
706 vector<Json> data;
707 ch = get_next_token();
708 if (ch == ']')
709 return data;
710
711 while (1) {
712 i--;
713 data.push_back(parse_json(depth + 1));
714 if (failed)
715 return Json();
716
717 ch = get_next_token();
718 if (ch == ']')
719 break;
720 if (ch != ',')
721 return fail("expected ',' in list, got " + esc(ch));
722
723 ch = get_next_token();
724 (void)ch;
725 }
726 return data;
727 }
728
729 return fail("expected value, got " + esc(ch));
730 }
731 };
732 }//namespace {
733
parse(const string & in,string & err,JsonParse strategy)734 Json Json::parse(const string &in, string &err, JsonParse strategy) {
735 JsonParser parser { in, 0, err, false, strategy };
736 Json result = parser.parse_json(0);
737
738 // Check for any trailing garbage
739 parser.consume_garbage();
740 if (parser.i != in.size())
741 return parser.fail("unexpected trailing " + esc(in[parser.i]));
742
743 return result;
744 }
745
746 // Documented in json11.hpp
parse_multi(const string & in,std::string::size_type & parser_stop_pos,string & err,JsonParse strategy)747 vector<Json> Json::parse_multi(const string &in,
748 std::string::size_type &parser_stop_pos,
749 string &err,
750 JsonParse strategy) {
751 JsonParser parser { in, 0, err, false, strategy };
752 parser_stop_pos = 0;
753 vector<Json> json_vec;
754 while (parser.i != in.size() && !parser.failed) {
755 json_vec.push_back(parser.parse_json(0));
756 // Check for another object
757 parser.consume_garbage();
758 if (!parser.failed)
759 parser_stop_pos = parser.i;
760 }
761 return json_vec;
762 }
763
764 /* * * * * * * * * * * * * * * * * * * *
765 * Shape-checking
766 */
767
has_shape(const shape & types,string & err) const768 bool Json::has_shape(const shape & types, string & err) const {
769 if (!is_object()) {
770 err = "expected JSON object, got " + dump();
771 return false;
772 }
773
774 for (auto & item : types) {
775 if ((*this)[item.first].type() != item.second) {
776 err = "bad type for " + item.first + " in " + dump();
777 return false;
778 }
779 }
780
781 return true;
782 }
783
784 } // namespace json11
785