// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // A JSON parser, converting from a std::string_view to a base::Value. // // The JSON spec is: // https://tools.ietf.org/rfc/rfc8259.txt // which obsoletes the earlier RFCs 4627, 7158 and 7159. // // This RFC should be equivalent to the informal spec: // https://www.json.org/json-en.html // // Implementation choices permitted by the RFC: // - Nesting is limited (to a configurable depth, 200 by default). // - Numbers are limited to those representable by a finite double. The // conversion from a JSON number (in the std::string_view input) to a // double-flavored base::Value may also be lossy. // - The input (which must be UTF-8) may begin with a BOM (Byte Order Mark). // - Duplicate object keys (strings) are silently allowed. Last key-value pair // wins. Previous pairs are discarded. // // Configurable (see the JSONParserOptions type) deviations from the RFC: // - Allow trailing commas: "[1,2,]". // - Replace invalid Unicode with U+FFFD REPLACEMENT CHARACTER. // - Allow "// etc\n" and "/* etc */" C-style comments. // - Allow ASCII control characters, including literal (not escaped) NUL bytes // and new lines, within a JSON string. // - Allow "\\v" escapes within a JSON string, producing a vertical tab. // - Allow "\\x23" escapes within a JSON string. Subtly, the 2-digit hex value // is a Unicode code point, not a UTF-8 byte. For example, "\\xFF" in the // JSON source decodes to a base::Value whose string contains "\xC3\xBF", the // UTF-8 encoding of U+00FF LATIN SMALL LETTER Y WITH DIAERESIS. Converting // from UTF-8 to UTF-16, e.g. via UTF8ToWide, will recover a 16-bit 0x00FF. #ifndef BASE_JSON_JSON_READER_H_ #define BASE_JSON_JSON_READER_H_ #include <optional> #include <string> #include <string_view> #include "base/base_export.h" #include "base/json/json_common.h" #include "base/strings/string_number_conversions.h" #include "base/types/expected.h" #include "base/values.h" namespace base { enum JSONParserOptions { … }; class BASE_EXPORT JSONReader { … }; } // namespace base #endif // BASE_JSON_JSON_READER_H_