1 //=== JSON.cpp - JSON value, parsing and serialization - C++ -----------*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 #include "llvm/Support/JSON.h"
11 #include "llvm/Support/ConvertUTF.h"
12 #include "llvm/Support/Format.h"
18 Value &Object::operator[](const ObjectKey &K) {
19 return try_emplace(K, nullptr).first->getSecond();
21 Value &Object::operator[](ObjectKey &&K) {
22 return try_emplace(std::move(K), nullptr).first->getSecond();
24 Value *Object::get(StringRef K) {
30 const Value *Object::get(StringRef K) const {
36 llvm::Optional<std::nullptr_t> Object::getNull(StringRef K) const {
38 return V->getAsNull();
41 llvm::Optional<bool> Object::getBoolean(StringRef K) const {
43 return V->getAsBoolean();
46 llvm::Optional<double> Object::getNumber(StringRef K) const {
48 return V->getAsNumber();
51 llvm::Optional<int64_t> Object::getInteger(StringRef K) const {
53 return V->getAsInteger();
56 llvm::Optional<llvm::StringRef> Object::getString(StringRef K) const {
58 return V->getAsString();
61 const json::Object *Object::getObject(StringRef K) const {
63 return V->getAsObject();
66 json::Object *Object::getObject(StringRef K) {
68 return V->getAsObject();
71 const json::Array *Object::getArray(StringRef K) const {
73 return V->getAsArray();
76 json::Array *Object::getArray(StringRef K) {
78 return V->getAsArray();
81 bool operator==(const Object &LHS, const Object &RHS) {
82 if (LHS.size() != RHS.size())
84 for (const auto &L : LHS) {
85 auto R = RHS.find(L.first);
86 if (R == RHS.end() || L.second != R->second)
92 Array::Array(std::initializer_list<Value> Elements) {
93 V.reserve(Elements.size());
94 for (const Value &V : Elements) {
95 emplace_back(nullptr);
96 back().moveFrom(std::move(V));
100 Value::Value(std::initializer_list<Value> Elements)
101 : Value(json::Array(Elements)) {}
103 void Value::copyFrom(const Value &M) {
110 memcpy(Union.buffer, M.Union.buffer, sizeof(Union.buffer));
113 create<StringRef>(M.as<StringRef>());
116 create<std::string>(M.as<std::string>());
119 create<json::Object>(M.as<json::Object>());
122 create<json::Array>(M.as<json::Array>());
127 void Value::moveFrom(const Value &&M) {
134 memcpy(Union.buffer, M.Union.buffer, sizeof(Union.buffer));
137 create<StringRef>(M.as<StringRef>());
140 create<std::string>(std::move(M.as<std::string>()));
144 create<json::Object>(std::move(M.as<json::Object>()));
148 create<json::Array>(std::move(M.as<json::Array>()));
154 void Value::destroy() {
162 as<StringRef>().~StringRef();
165 as<std::string>().~basic_string();
168 as<json::Object>().~Object();
171 as<json::Array>().~Array();
176 bool operator==(const Value &L, const Value &R) {
177 if (L.kind() != R.kind())
181 return *L.getAsNull() == *R.getAsNull();
183 return *L.getAsBoolean() == *R.getAsBoolean();
185 // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
186 // The same integer must convert to the same double, per the standard.
187 // However we see 64-vs-80-bit precision comparisons with gcc-7 -O3 -m32.
188 // So we avoid floating point promotion for exact comparisons.
189 if (L.Type == Value::T_Integer || R.Type == Value::T_Integer)
190 return L.getAsInteger() == R.getAsInteger();
191 return *L.getAsNumber() == *R.getAsNumber();
193 return *L.getAsString() == *R.getAsString();
195 return *L.getAsArray() == *R.getAsArray();
197 return *L.getAsObject() == *R.getAsObject();
199 llvm_unreachable("Unknown value kind");
203 // Simple recursive-descent JSON parser.
206 Parser(StringRef JSON)
207 : Start(JSON.begin()), P(JSON.begin()), End(JSON.end()) {}
211 if (isUTF8(StringRef(Start, End - Start), &ErrOffset))
213 P = Start + ErrOffset; // For line/column calculation.
214 return parseError("Invalid UTF-8 sequence");
217 bool parseValue(Value &Out);
223 return parseError("Text after end of document");
228 return std::move(*Err);
232 void eatWhitespace() {
233 while (P != End && (*P == ' ' || *P == '\r' || *P == '\n' || *P == '\t'))
237 // On invalid syntax, parseX() functions return false and set Err.
238 bool parseNumber(char First, Value &Out);
239 bool parseString(std::string &Out);
240 bool parseUnicode(std::string &Out);
241 bool parseError(const char *Msg); // always returns false
243 char next() { return P == End ? 0 : *P++; }
244 char peek() { return P == End ? 0 : *P; }
245 static bool isNumber(char C) {
246 return C == '0' || C == '1' || C == '2' || C == '3' || C == '4' ||
247 C == '5' || C == '6' || C == '7' || C == '8' || C == '9' ||
248 C == 'e' || C == 'E' || C == '+' || C == '-' || C == '.';
252 const char *Start, *P, *End;
255 bool Parser::parseValue(Value &Out) {
258 return parseError("Unexpected EOF");
259 switch (char C = next()) {
260 // Bare null/true/false are easy - first char identifies them.
263 return (next() == 'u' && next() == 'l' && next() == 'l') ||
264 parseError("Invalid JSON value (null?)");
267 return (next() == 'r' && next() == 'u' && next() == 'e') ||
268 parseError("Invalid JSON value (true?)");
271 return (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') ||
272 parseError("Invalid JSON value (false?)");
275 if (parseString(S)) {
283 Array &A = *Out.getAsArray();
290 A.emplace_back(nullptr);
291 if (!parseValue(A.back()))
301 return parseError("Expected , or ] after array element");
307 Object &O = *Out.getAsObject();
315 return parseError("Expected object key");
321 return parseError("Expected : after object key");
323 if (!parseValue(O[std::move(K)]))
333 return parseError("Expected , or } after object property");
339 return parseNumber(C, Out);
340 return parseError("Invalid JSON value");
344 bool Parser::parseNumber(char First, Value &Out) {
345 // Read the number into a string. (Must be null-terminated for strto*).
348 while (isNumber(peek()))
351 // Try first to parse as integer, and if so preserve full 64 bits.
352 // strtoll returns long long >= 64 bits, so check it's in range too.
353 auto I = std::strtoll(S.c_str(), &End, 10);
354 if (End == S.end() && I >= std::numeric_limits<int64_t>::min() &&
355 I <= std::numeric_limits<int64_t>::max()) {
359 // If it's not an integer
360 Out = std::strtod(S.c_str(), &End);
361 return End == S.end() || parseError("Invalid JSON value (number?)");
364 bool Parser::parseString(std::string &Out) {
365 // leading quote was already consumed.
366 for (char C = next(); C != '"'; C = next()) {
367 if (LLVM_UNLIKELY(P == End))
368 return parseError("Unterminated string");
369 if (LLVM_UNLIKELY((C & 0x1f) == C))
370 return parseError("Control character in string");
371 if (LLVM_LIKELY(C != '\\')) {
375 // Handle escape sequence.
376 switch (C = next()) {
398 if (!parseUnicode(Out))
402 return parseError("Invalid escape sequence");
408 static void encodeUtf8(uint32_t Rune, std::string &Out) {
410 Out.push_back(Rune & 0x7F);
411 } else if (Rune < 0x800) {
412 uint8_t FirstByte = 0xC0 | ((Rune & 0x7C0) >> 6);
413 uint8_t SecondByte = 0x80 | (Rune & 0x3F);
414 Out.push_back(FirstByte);
415 Out.push_back(SecondByte);
416 } else if (Rune < 0x10000) {
417 uint8_t FirstByte = 0xE0 | ((Rune & 0xF000) >> 12);
418 uint8_t SecondByte = 0x80 | ((Rune & 0xFC0) >> 6);
419 uint8_t ThirdByte = 0x80 | (Rune & 0x3F);
420 Out.push_back(FirstByte);
421 Out.push_back(SecondByte);
422 Out.push_back(ThirdByte);
423 } else if (Rune < 0x110000) {
424 uint8_t FirstByte = 0xF0 | ((Rune & 0x1F0000) >> 18);
425 uint8_t SecondByte = 0x80 | ((Rune & 0x3F000) >> 12);
426 uint8_t ThirdByte = 0x80 | ((Rune & 0xFC0) >> 6);
427 uint8_t FourthByte = 0x80 | (Rune & 0x3F);
428 Out.push_back(FirstByte);
429 Out.push_back(SecondByte);
430 Out.push_back(ThirdByte);
431 Out.push_back(FourthByte);
433 llvm_unreachable("Invalid codepoint");
437 // Parse a UTF-16 \uNNNN escape sequence. "\u" has already been consumed.
438 // May parse several sequential escapes to ensure proper surrogate handling.
439 // We do not use ConvertUTF.h, it can't accept and replace unpaired surrogates.
440 // These are invalid Unicode but valid JSON (RFC 8259, section 8.2).
441 bool Parser::parseUnicode(std::string &Out) {
442 // Invalid UTF is not a JSON error (RFC 8529§8.2). It gets replaced by U+FFFD.
443 auto Invalid = [&] { Out.append(/* UTF-8 */ {'\xef', '\xbf', '\xbd'}); };
444 // Decodes 4 hex digits from the stream into Out, returns false on error.
445 auto Parse4Hex = [this](uint16_t &Out) -> bool {
447 char Bytes[] = {next(), next(), next(), next()};
448 for (unsigned char C : Bytes) {
449 if (!std::isxdigit(C))
450 return parseError("Invalid \\u escape sequence");
452 Out |= (C > '9') ? (C & ~0x20) - 'A' + 10 : (C - '0');
456 uint16_t First; // UTF-16 code unit from the first \u escape.
457 if (!Parse4Hex(First))
460 // We loop to allow proper surrogate-pair error handling.
462 // Case 1: the UTF-16 code unit is already a codepoint in the BMP.
463 if (LLVM_LIKELY(First < 0xD800 || First >= 0xE000)) {
464 encodeUtf8(First, Out);
468 // Case 2: it's an (unpaired) trailing surrogate.
469 if (LLVM_UNLIKELY(First >= 0xDC00)) {
474 // Case 3: it's a leading surrogate. We expect a trailing one next.
475 // Case 3a: there's no trailing \u escape. Don't advance in the stream.
476 if (LLVM_UNLIKELY(P + 2 > End || *P != '\\' || *(P + 1) != 'u')) {
477 Invalid(); // Leading surrogate was unpaired.
482 if (!Parse4Hex(Second))
484 // Case 3b: there was another \u escape, but it wasn't a trailing surrogate.
485 if (LLVM_UNLIKELY(Second < 0xDC00 || Second >= 0xE000)) {
486 Invalid(); // Leading surrogate was unpaired.
487 First = Second; // Second escape still needs to be processed.
490 // Case 3c: a valid surrogate pair encoding an astral codepoint.
491 encodeUtf8(0x10000 | ((First - 0xD800) << 10) | (Second - 0xDC00), Out);
496 bool Parser::parseError(const char *Msg) {
498 const char *StartOfLine = Start;
499 for (const char *X = Start; X < P; ++X) {
506 llvm::make_unique<ParseError>(Msg, Line, P - StartOfLine, P - Start));
511 Expected<Value> parse(StringRef JSON) {
518 return P.takeError();
520 char ParseError::ID = 0;
522 static std::vector<const Object::value_type *> sortedElements(const Object &O) {
523 std::vector<const Object::value_type *> Elements;
524 for (const auto &E : O)
525 Elements.push_back(&E);
527 [](const Object::value_type *L, const Object::value_type *R) {
528 return L->first < R->first;
533 bool isUTF8(llvm::StringRef S, size_t *ErrOffset) {
534 // Fast-path for ASCII, which is valid UTF-8.
535 if (LLVM_LIKELY(isASCII(S)))
538 const UTF8 *Data = reinterpret_cast<const UTF8 *>(S.data()), *Rest = Data;
539 if (LLVM_LIKELY(isLegalUTF8String(&Rest, Data + S.size())))
543 *ErrOffset = Rest - Data;
547 std::string fixUTF8(llvm::StringRef S) {
548 // This isn't particularly efficient, but is only for error-recovery.
549 std::vector<UTF32> Codepoints(S.size()); // 1 codepoint per byte suffices.
550 const UTF8 *In8 = reinterpret_cast<const UTF8 *>(S.data());
551 UTF32 *Out32 = Codepoints.data();
552 ConvertUTF8toUTF32(&In8, In8 + S.size(), &Out32, Out32 + Codepoints.size(),
554 Codepoints.resize(Out32 - Codepoints.data());
555 std::string Res(4 * Codepoints.size(), 0); // 4 bytes per codepoint suffice
556 const UTF32 *In32 = Codepoints.data();
557 UTF8 *Out8 = reinterpret_cast<UTF8 *>(&Res[0]);
558 ConvertUTF32toUTF8(&In32, In32 + Codepoints.size(), &Out8, Out8 + Res.size(),
560 Res.resize(reinterpret_cast<char *>(Out8) - Res.data());
567 static void quote(llvm::raw_ostream &OS, llvm::StringRef S) {
569 for (unsigned char C : S) {
570 if (C == 0x22 || C == 0x5C)
578 // A few characters are common enough to make short escapes worthwhile.
590 llvm::write_hex(OS, C, llvm::HexPrintStyle::Lower, 4);
597 enum IndenterAction {
604 // Prints JSON. The indenter can be used to control formatting.
605 template <typename Indenter>
606 void llvm::json::Value::print(raw_ostream &OS, const Indenter &I) const {
612 OS << (as<bool>() ? "true" : "false");
615 OS << format("%.*g", std::numeric_limits<double>::max_digits10,
622 quote(OS, as<StringRef>());
625 quote(OS, as<std::string>());
631 for (const auto *P : sortedElements(as<json::Object>())) {
639 P->second.print(OS, I);
651 for (const auto &E : as<json::Array>()) {
667 void llvm::format_provider<llvm::json::Value>::format(
668 const llvm::json::Value &E, raw_ostream &OS, StringRef Options) {
669 if (Options.empty()) {
673 unsigned IndentAmount = 0;
674 if (Options.getAsInteger(/*Radix=*/10, IndentAmount))
675 llvm_unreachable("json::Value format options should be an integer");
676 unsigned IndentLevel = 0;
677 E.print(OS, [&](IndenterAction A) {
681 OS.indent(IndentLevel);
687 IndentLevel += IndentAmount;
690 IndentLevel -= IndentAmount;
696 llvm::raw_ostream &llvm::json::operator<<(raw_ostream &OS, const Value &E) {
697 E.print(OS, [](IndenterAction A) { /*ignore*/ });