json_reader.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // A JSON parser, converting from a base::StringPiece to a base::Value.
  5. //
  6. // The JSON spec is:
  7. // https://tools.ietf.org/rfc/rfc8259.txt
  8. // which obsoletes the earlier RFCs 4627, 7158 and 7159.
  9. //
  10. // This RFC should be equivalent to the informal spec:
  11. // https://www.json.org/json-en.html
  12. //
  13. // Implementation choices permitted by the RFC:
  14. // - Nesting is limited (to a configurable depth, 200 by default).
  15. // - Numbers are limited to those representable by a finite double. The
  16. // conversion from a JSON number (in the base::StringPiece input) to a
  17. // double-flavored base::Value may also be lossy.
  18. // - The input (which must be UTF-8) may begin with a BOM (Byte Order Mark).
  19. // - Duplicate object keys (strings) are silently allowed. Last key-value pair
  20. // wins. Previous pairs are discarded.
  21. //
  22. // Configurable (see the JSONParserOptions type) deviations from the RFC:
  23. // - Allow trailing commas: "[1,2,]".
  24. // - Replace invalid Unicode with U+FFFD REPLACEMENT CHARACTER.
  25. //
  26. // Non-configurable deviations from the RFC:
  27. // - Allow "// etc\n" and "/* etc */" C-style comments.
  28. // - Allow ASCII control characters, including literal (not escaped) NUL bytes
  29. // and new lines, within a JSON string.
  30. // - Allow "\\v" escapes within a JSON string, producing a vertical tab.
  31. // - Allow "\\x23" escapes within a JSON string. Subtly, the 2-digit hex value
  32. // is a Unicode code point, not a UTF-8 byte. For example, "\\xFF" in the
  33. // JSON source decodes to a base::Value whose string contains "\xC3\xBF", the
  34. // UTF-8 encoding of U+00FF LATIN SMALL LETTER Y WITH DIAERESIS. Converting
  35. // from UTF-8 to UTF-16, e.g. via UTF8ToWide, will recover a 16-bit 0x00FF.
  36. #ifndef BASE_JSON_JSON_READER_H_
  37. #define BASE_JSON_JSON_READER_H_
  38. #include <memory>
  39. #include <string>
  40. #include "base/base_export.h"
  41. #include "base/json/json_common.h"
  42. #include "base/optional.h"
  43. #include "base/strings/string_piece.h"
  44. #include "base/values.h"
  45. namespace base {
  46. enum JSONParserOptions {
  47. // Parses the input strictly according to RFC 8259, except for where noted
  48. // above.
  49. JSON_PARSE_RFC = 0,
  50. // Allows commas to exist after the last element in structures.
  51. JSON_ALLOW_TRAILING_COMMAS = 1 << 0,
  52. // If set the parser replaces invalid code points (i.e. lone
  53. // surrogates) with the Unicode replacement character (U+FFFD). If
  54. // not set, invalid code points trigger a hard error and parsing
  55. // fails.
  56. JSON_REPLACE_INVALID_CHARACTERS = 1 << 1,
  57. };
  58. class BASE_EXPORT JSONReader {
  59. public:
  60. struct BASE_EXPORT ValueWithError {
  61. ValueWithError();
  62. ValueWithError(ValueWithError&& other);
  63. ValueWithError& operator=(ValueWithError&& other);
  64. ~ValueWithError();
  65. Optional<Value> value;
  66. // Contains default values if |value| exists, or the error status if |value|
  67. // is base::nullopt.
  68. std::string error_message;
  69. int error_line = 0;
  70. int error_column = 0;
  71. DISALLOW_COPY_AND_ASSIGN(ValueWithError);
  72. };
  73. // Reads and parses |json|, returning a Value.
  74. // If |json| is not a properly formed JSON string, returns base::nullopt.
  75. static Optional<Value> Read(StringPiece json,
  76. int options = JSON_PARSE_RFC,
  77. size_t max_depth = internal::kAbsoluteMaxDepth);
  78. // Deprecated. Use the Read() method above.
  79. // Reads and parses |json|, returning a Value.
  80. // If |json| is not a properly formed JSON string, returns nullptr.
  81. // Wrap this in base::FooValue::From() to check the Value is of type Foo and
  82. // convert to a FooValue at the same time.
  83. static std::unique_ptr<Value> ReadDeprecated(
  84. StringPiece json,
  85. int options = JSON_PARSE_RFC,
  86. size_t max_depth = internal::kAbsoluteMaxDepth);
  87. // Reads and parses |json| like Read(). Returns a ValueWithError, which on
  88. // error, will be populated with a formatted error message, an error code, and
  89. // the error location if appropriate.
  90. static ValueWithError ReadAndReturnValueWithError(
  91. StringPiece json,
  92. int options = JSON_PARSE_RFC);
  93. // This class contains only static methods.
  94. JSONReader() = delete;
  95. DISALLOW_COPY_AND_ASSIGN(JSONReader);
  96. };
  97. } // namespace base
  98. #endif // BASE_JSON_JSON_READER_H_