2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/json.h>
23 #include <boost/next_prior.hpp>
24 #include <boost/algorithm/string.hpp>
26 #include <folly/Conv.h>
27 #include <folly/Portability.h>
28 #include <folly/Range.h>
29 #include <folly/String.h>
30 #include <folly/Unicode.h>
31 #include <folly/portability/Constexpr.h>
35 //////////////////////////////////////////////////////////////////////
43 unsigned* indentLevel,
44 serialization_opts const* opts)
45 : out_(out), indentLevel_(indentLevel), opts_(*opts) {}
47 void operator()(dynamic const& v) const {
50 if (!opts_.allow_nan_inf &&
51 (std::isnan(v.asDouble()) || std::isinf(v.asDouble()))) {
52 throw std::runtime_error("folly::toJson: JSON object value was a "
55 toAppend(v.asDouble(), &out_, opts_.double_mode, opts_.double_num_digits);
57 case dynamic::INT64: {
58 auto intval = v.asInt();
59 if (opts_.javascript_safe) {
60 // Use folly::to to check that this integer can be represented
61 // as a double without loss of precision.
62 intval = int64_t(to<double>(intval));
64 toAppend(intval, &out_);
68 out_ += v.asBool() ? "true" : "false";
74 escapeString(v.asString(), out_, opts_);
83 CHECK(0) << "Bad type " << v.type();
88 void printKV(const std::pair<const dynamic, dynamic>& p) const {
89 if (!opts_.allow_non_string_keys && !p.first.isString()) {
90 throw std::runtime_error("folly::toJson: JSON object key was not a "
98 template <typename Iterator>
99 void printKVPairs(Iterator begin, Iterator end) const {
101 for (++begin; begin != end; ++begin) {
108 void printObject(dynamic const& o) const {
117 if (opts_.sort_keys) {
118 using ref = std::reference_wrapper<decltype(o.items())::value_type const>;
119 std::vector<ref> refs(o.items().begin(), o.items().end());
120 std::sort(refs.begin(), refs.end(), [](ref a, ref b) {
121 // Only compare keys. No ordering among identical keys.
122 return a.get().first < b.get().first;
124 printKVPairs(refs.cbegin(), refs.cend());
126 printKVPairs(o.items().begin(), o.items().end());
133 void printArray(dynamic const& a) const {
143 for (auto& val : range(boost::next(a.begin()), a.end())) {
154 void outdent() const {
160 void indent() const {
166 void newline() const {
168 out_ += to<std::string>('\n', std::string(*indentLevel_ * 2, ' '));
172 void mapColon() const {
173 out_ += indentLevel_ ? " : " : ":";
178 unsigned* const indentLevel_;
179 serialization_opts const& opts_;
182 //////////////////////////////////////////////////////////////////////
184 struct ParseError : std::runtime_error {
185 explicit ParseError(int line)
186 : std::runtime_error(to<std::string>("json parse error on line ", line))
189 explicit ParseError(int line, std::string const& context,
190 std::string const& expected)
191 : std::runtime_error(to<std::string>("json parse error on line ", line,
192 !context.empty() ? to<std::string>(" near `", context, '\'')
197 explicit ParseError(std::string const& msg)
198 : std::runtime_error("json parse error: " + msg)
202 // Wraps our input buffer with some helper functions.
204 explicit Input(StringPiece range, json::serialization_opts const* opts)
212 Input(Input const&) = delete;
213 Input& operator=(Input const&) = delete;
215 char const* begin() const { return range_.begin(); }
217 // Parse ahead for as long as the supplied predicate is satisfied,
218 // returning a range of what was skipped.
219 template<class Predicate>
220 StringPiece skipWhile(const Predicate& p) {
221 std::size_t skipped = 0;
222 for (; skipped < range_.size(); ++skipped) {
223 if (!p(range_[skipped])) {
226 if (range_[skipped] == '\n') {
230 auto ret = range_.subpiece(0, skipped);
231 range_.advance(skipped);
236 StringPiece skipDigits() {
237 return skipWhile([] (char c) { return c >= '0' && c <= '9'; });
240 StringPiece skipMinusAndDigits() {
241 bool firstChar = true;
242 return skipWhile([&firstChar] (char c) {
243 bool result = (c >= '0' && c <= '9') || (firstChar && c == '-');
249 void skipWhitespace() {
250 range_ = folly::skipWhitespace(range_);
254 void expect(char c) {
256 throw ParseError(lineNum_, context(),
257 to<std::string>("expected '", c, '\''));
262 std::size_t size() const {
263 return range_.size();
266 int operator*() const {
278 return to<T>(&range_);
279 } catch (std::exception const& e) {
284 bool consume(StringPiece str) {
285 if (boost::starts_with(range_, str)) {
286 range_.advance(str.size());
293 std::string context() const {
294 return range_.subpiece(0, 16 /* arbitrary */).toString();
297 dynamic error(char const* what) const {
298 throw ParseError(lineNum_, context(), what);
301 json::serialization_opts const& getOpts() {
305 void incrementRecursionLevel() {
306 if (currentRecursionLevel_ > opts_.recursion_limit) {
307 error("recursion limit exceeded");
309 currentRecursionLevel_++;
312 void decrementRecursionLevel() {
313 currentRecursionLevel_--;
317 void storeCurrent() {
318 current_ = range_.empty() ? EOF : range_.front();
323 json::serialization_opts const& opts_;
326 unsigned int currentRecursionLevel_{0};
329 class RecursionGuard {
331 explicit RecursionGuard(Input& in) : in_(in) {
332 in_.incrementRecursionLevel();
336 in_.decrementRecursionLevel();
343 dynamic parseValue(Input& in);
344 std::string parseString(Input& in);
345 dynamic parseNumber(Input& in);
347 dynamic parseObject(Input& in) {
351 dynamic ret = dynamic::object;
360 if (in.getOpts().allow_trailing_comma && *in == '}') {
363 if (*in == '\"') { // string
364 auto key = parseString(in);
368 ret.insert(std::move(key), parseValue(in));
369 } else if (!in.getOpts().allow_non_string_keys) {
370 in.error("expected string for object key name");
372 auto key = parseValue(in);
376 ret.insert(std::move(key), parseValue(in));
391 dynamic parseArray(Input& in) {
395 dynamic ret = dynamic::array;
404 if (in.getOpts().allow_trailing_comma && *in == ']') {
407 ret.push_back(parseValue(in));
420 dynamic parseNumber(Input& in) {
421 bool const negative = (*in == '-');
422 if (negative && in.consume("-Infinity")) {
423 if (in.getOpts().parse_numbers_as_strings) {
426 return -std::numeric_limits<double>::infinity();
430 auto integral = in.skipMinusAndDigits();
431 if (negative && integral.size() < 2) {
432 in.error("expected digits after `-'");
435 auto const wasE = *in == 'e' || *in == 'E';
437 constexpr const char* maxInt = "9223372036854775807";
438 constexpr const char* minInt = "-9223372036854775808";
439 constexpr auto maxIntLen = constexpr_strlen(maxInt);
440 constexpr auto minIntLen = constexpr_strlen(minInt);
442 if (*in != '.' && !wasE && in.getOpts().parse_numbers_as_strings) {
446 if (*in != '.' && !wasE) {
447 if (LIKELY(!in.getOpts().double_fallback || integral.size() < maxIntLen) ||
448 (!negative && integral.size() == maxIntLen && integral <= maxInt) ||
449 (negative && integral.size() == minIntLen && integral <= minInt)) {
450 auto val = to<int64_t>(integral);
454 auto val = to<double>(integral);
460 auto end = !wasE ? (++in, in.skipDigits().end()) : in.begin();
461 if (*in == 'e' || *in == 'E') {
463 if (*in == '+' || *in == '-') {
466 auto expPart = in.skipDigits();
469 auto fullNum = range(integral.begin(), end);
470 if (in.getOpts().parse_numbers_as_strings) {
473 auto val = to<double>(fullNum);
477 std::string decodeUnicodeEscape(Input& in) {
478 auto hexVal = [&] (int c) -> uint16_t {
480 c >= '0' && c <= '9' ? c - '0' :
481 c >= 'a' && c <= 'f' ? c - 'a' + 10 :
482 c >= 'A' && c <= 'F' ? c - 'A' + 10 :
483 (in.error("invalid hex digit"), 0));
486 auto readHex = [&]() -> uint16_t {
488 in.error("expected 4 hex digits");
491 uint16_t ret = hexVal(*in) * 4096;
493 ret += hexVal(*in) * 256;
495 ret += hexVal(*in) * 16;
503 * If the value encoded is in the surrogate pair range, we need to
504 * make sure there is another escape that we can use also.
506 uint32_t codePoint = readHex();
507 if (codePoint >= 0xd800 && codePoint <= 0xdbff) {
508 if (!in.consume("\\u")) {
509 in.error("expected another unicode escape for second half of "
512 uint16_t second = readHex();
513 if (second >= 0xdc00 && second <= 0xdfff) {
514 codePoint = 0x10000 + ((codePoint & 0x3ff) << 10) +
517 in.error("second character in surrogate pair is invalid");
519 } else if (codePoint >= 0xdc00 && codePoint <= 0xdfff) {
520 in.error("invalid unicode code point (in range [0xdc00,0xdfff])");
523 return codePointToUtf8(codePoint);
526 std::string parseString(Input& in) {
532 auto range = in.skipWhile(
533 [] (char c) { return c != '\"' && c != '\\'; }
535 ret.append(range.begin(), range.end());
544 case '\"': ret.push_back('\"'); ++in; break;
545 case '\\': ret.push_back('\\'); ++in; break;
546 case '/': ret.push_back('/'); ++in; break;
547 case 'b': ret.push_back('\b'); ++in; break;
548 case 'f': ret.push_back('\f'); ++in; break;
549 case 'n': ret.push_back('\n'); ++in; break;
550 case 'r': ret.push_back('\r'); ++in; break;
551 case 't': ret.push_back('\t'); ++in; break;
552 case 'u': ++in; ret += decodeUnicodeEscape(in); break;
554 in.error(to<std::string>("unknown escape ", *in, " in string").c_str());
559 in.error("unterminated string");
563 * Apparently we're actually supposed to ban all control
564 * characters from strings. This seems unnecessarily
565 * restrictive, so we're only banning zero bytes. (Since the
566 * string is presumed to be UTF-8 encoded it's fine to just
569 in.error("null byte in string");
572 ret.push_back(char(*in));
579 dynamic parseValue(Input& in) {
580 RecursionGuard guard(in);
583 return *in == '[' ? parseArray(in) :
584 *in == '{' ? parseObject(in) :
585 *in == '\"' ? parseString(in) :
586 (*in == '-' || (*in >= '0' && *in <= '9')) ? parseNumber(in) :
587 in.consume("true") ? true :
588 in.consume("false") ? false :
589 in.consume("null") ? nullptr :
590 in.consume("Infinity") ?
591 (in.getOpts().parse_numbers_as_strings ? (dynamic)"Infinity" :
592 (dynamic)std::numeric_limits<double>::infinity()) :
594 (in.getOpts().parse_numbers_as_strings ? (dynamic)"NaN" :
595 (dynamic)std::numeric_limits<double>::quiet_NaN()) :
596 in.error("expected json value");
601 //////////////////////////////////////////////////////////////////////
603 std::string serialize(dynamic const& dyn, serialization_opts const& opts) {
605 unsigned indentLevel = 0;
606 Printer p(ret, opts.pretty_formatting ? &indentLevel : nullptr, &opts);
611 // Escape a string so that it is legal to print it in JSON text.
615 const serialization_opts& opts) {
616 auto hexDigit = [] (uint8_t c) -> char {
617 return c < 10 ? c + '0' : c - 10 + 'a';
622 auto* p = reinterpret_cast<const unsigned char*>(input.begin());
623 auto* q = reinterpret_cast<const unsigned char*>(input.begin());
624 auto* e = reinterpret_cast<const unsigned char*>(input.end());
627 // Since non-ascii encoding inherently does utf8 validation
628 // we explicitly validate utf8 only if non-ascii encoding is disabled.
629 if ((opts.validate_utf8 || opts.skip_invalid_utf8)
630 && !opts.encode_non_ascii) {
631 // to achieve better spatial and temporal coherence
632 // we do utf8 validation progressively along with the
633 // string-escaping instead of two separate passes
635 // as the encoding progresses, q will stay at or ahead of p
638 // as p catches up with q, move q forward
640 // calling utf8_decode has the side effect of
641 // checking that utf8 encodings are valid
642 char32_t v = utf8ToCodePoint(q, e, opts.skip_invalid_utf8);
643 if (opts.skip_invalid_utf8 && v == U'\ufffd') {
644 out.append(u8"\ufffd");
650 if (opts.encode_non_ascii && (*p & 0x80)) {
651 // note that this if condition captures utf8 chars
652 // with value > 127, so size > 1 byte
653 char32_t v = utf8ToCodePoint(p, e, opts.skip_invalid_utf8);
655 out.push_back(hexDigit(uint8_t(v >> 12)));
656 out.push_back(hexDigit((v >> 8) & 0x0f));
657 out.push_back(hexDigit((v >> 4) & 0x0f));
658 out.push_back(hexDigit(v & 0x0f));
659 } else if (*p == '\\' || *p == '\"') {
661 out.push_back(char(*p++));
662 } else if (*p <= 0x1f) {
664 case '\b': out.append("\\b"); p++; break;
665 case '\f': out.append("\\f"); p++; break;
666 case '\n': out.append("\\n"); p++; break;
667 case '\r': out.append("\\r"); p++; break;
668 case '\t': out.append("\\t"); p++; break;
670 // note that this if condition captures non readable chars
671 // with value < 32, so size = 1 byte (e.g control chars).
673 out.push_back(hexDigit((*p & 0xf0) >> 4));
674 out.push_back(hexDigit(*p & 0xf));
685 std::string stripComments(StringPiece jsonC) {
692 } state = State::None;
694 for (size_t i = 0; i < jsonC.size(); ++i) {
695 auto s = jsonC.subpiece(i);
698 if (s.startsWith("/*")) {
699 state = State::InlineComment;
702 } else if (s.startsWith("//")) {
703 state = State::LineComment;
706 } else if (s[0] == '\"') {
707 state = State::InString;
709 result.push_back(s[0]);
711 case State::InString:
713 if (UNLIKELY(s.size() == 1)) {
714 throw std::logic_error("Invalid JSONC: string is not terminated");
716 result.push_back(s[0]);
717 result.push_back(s[1]);
720 } else if (s[0] == '\"') {
723 result.push_back(s[0]);
725 case State::InlineComment:
726 if (s.startsWith("*/")) {
731 case State::LineComment:
733 // skip the line break. It doesn't matter.
738 throw std::logic_error("Unknown comment state");
746 //////////////////////////////////////////////////////////////////////
748 dynamic parseJson(StringPiece range) {
749 return parseJson(range, json::serialization_opts());
754 json::serialization_opts const& opts) {
756 json::Input in(range, &opts);
758 auto ret = parseValue(in);
760 if (in.size() && *in != '\0') {
761 in.error("parsing didn't consume all input");
766 std::string toJson(dynamic const& dyn) {
767 return json::serialize(dyn, json::serialization_opts());
770 std::string toPrettyJson(dynamic const& dyn) {
771 json::serialization_opts opts;
772 opts.pretty_formatting = true;
773 return json::serialize(dyn, opts);
776 //////////////////////////////////////////////////////////////////////
777 // dynamic::print_as_pseudo_json() is implemented here for header
778 // ordering reasons (most of the dynamic implementation is in
779 // dynamic-inl.h, which we don't want to include json.h).
781 void dynamic::print_as_pseudo_json(std::ostream& out) const {
782 json::serialization_opts opts;
783 opts.allow_non_string_keys = true;
784 opts.allow_nan_inf = true;
785 out << json::serialize(*this, opts);
788 void PrintTo(const dynamic& dyn, std::ostream* os) {
789 json::serialization_opts opts;
790 opts.allow_nan_inf = true;
791 opts.allow_non_string_keys = true;
792 opts.pretty_formatting = true;
793 opts.sort_keys = true;
794 *os << json::serialize(dyn, opts);
797 //////////////////////////////////////////////////////////////////////