json_reader.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "base/json/json_reader.h"
  5. #include <utility>
  6. #include "base/logging.h"
  7. #include "base/parsing_buildflags.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #if BUILDFLAG(BUILD_RUST_JSON_PARSER)
  10. #include "base/json/json_parser.rs.h"
  11. #include "base/strings/string_piece_rust.h"
  12. #else
  13. #include "base/json/json_parser.h"
  14. #endif
  15. namespace base {
  16. #if BUILDFLAG(BUILD_RUST_JSON_PARSER)
  17. namespace {
  18. base::expected<Value, JSONReader::Error>
  19. DecodeJSONInRust(const base::StringPiece& json, int options, size_t max_depth) {
  20. int32_t error_line;
  21. int32_t error_column;
  22. base::ffi::json::json_parser::JsonOptions rust_options;
  23. rust_options.allow_trailing_commas =
  24. options & base::JSON_ALLOW_TRAILING_COMMAS;
  25. rust_options.replace_invalid_characters =
  26. options & base::JSON_REPLACE_INVALID_CHARACTERS;
  27. rust_options.allow_comments = options & base::JSON_ALLOW_COMMENTS;
  28. rust_options.allow_vert_tab = options & base::JSON_ALLOW_VERT_TAB;
  29. rust_options.allow_control_chars = options & base::JSON_ALLOW_CONTROL_CHARS;
  30. rust_options.allow_x_escapes = options & base::JSON_ALLOW_X_ESCAPES;
  31. rust_options.max_depth = max_depth;
  32. base::JSONReader::Error error;
  33. absl::optional<base::Value> value;
  34. bool ok = base::ffi::json::json_parser::decode_json_from_cpp(
  35. base::StringPieceToRustSlice(json), rust_options, value, error_line,
  36. error_column, error.message);
  37. if (!ok) {
  38. error.line = error_line;
  39. error.column = error_column;
  40. return base::unexpected(std::move(error));
  41. }
  42. return std::move(*value);
  43. }
  44. } // anonymous namespace
  45. #endif // BUILDFLAG(BUILD_RUST_JSON_PARSER)
  46. JSONReader::Error::Error() = default;
  47. JSONReader::Error::Error(Error&& other) = default;
  48. JSONReader::Error::~Error() = default;
  49. JSONReader::Error& JSONReader::Error::operator=(Error&& other) = default;
  50. // static
  51. absl::optional<Value> JSONReader::Read(StringPiece json,
  52. int options,
  53. size_t max_depth) {
  54. #if BUILDFLAG(BUILD_RUST_JSON_PARSER)
  55. auto result = DecodeJSONInRust(json, options, max_depth);
  56. if (!result.has_value()) {
  57. return absl::nullopt;
  58. }
  59. return std::move(*result);
  60. #else // BUILDFLAG(BUILD_RUST_JSON_PARSER)
  61. internal::JSONParser parser(options, max_depth);
  62. return parser.Parse(json);
  63. #endif // BUILDFLAG(BUILD_RUST_JSON_PARSER)
  64. }
  65. // static
  66. std::unique_ptr<Value> JSONReader::ReadDeprecated(StringPiece json,
  67. int options,
  68. size_t max_depth) {
  69. absl::optional<Value> value = Read(json, options, max_depth);
  70. return value ? Value::ToUniquePtrValue(std::move(*value)) : nullptr;
  71. }
  72. // static
  73. JSONReader::Result JSONReader::ReadAndReturnValueWithError(StringPiece json,
  74. int options) {
  75. #if BUILDFLAG(BUILD_RUST_JSON_PARSER)
  76. return DecodeJSONInRust(json, options, internal::kAbsoluteMaxDepth);
  77. #else // BUILDFLAG(BUILD_RUST_JSON_PARSER)
  78. internal::JSONParser parser(options);
  79. auto value = parser.Parse(json);
  80. if (!value) {
  81. Error error;
  82. error.message = parser.GetErrorMessage();
  83. error.line = parser.error_line();
  84. error.column = parser.error_column();
  85. return base::unexpected(std::move(error));
  86. }
  87. return std::move(*value);
  88. #endif // BUILDFLAG(BUILD_RUST_JSON_PARSER)
  89. }
  90. } // namespace base