json_string_value_serializer.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_
  5. #define BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/json/json_reader.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/values.h"
  13. class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer {
  14. public:
  15. // |json_string| is the string that will be the destination of the
  16. // serialization. The caller of the constructor retains ownership of the
  17. // string. |json_string| must not be null.
  18. explicit JSONStringValueSerializer(std::string* json_string);
  19. JSONStringValueSerializer(const JSONStringValueSerializer&) = delete;
  20. JSONStringValueSerializer& operator=(const JSONStringValueSerializer&) =
  21. delete;
  22. ~JSONStringValueSerializer() override;
  23. // Attempt to serialize the data structure represented by Value into
  24. // JSON. If the return value is true, the result will have been written
  25. // into the string passed into the constructor.
  26. bool Serialize(base::ValueView root) override;
  27. // Equivalent to Serialize(root) except binary values are omitted from the
  28. // output.
  29. bool SerializeAndOmitBinaryValues(base::ValueView root);
  30. void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
  31. bool pretty_print() { return pretty_print_; }
  32. private:
  33. bool SerializeInternal(base::ValueView root, bool omit_binary_values);
  34. // Owned by the caller of the constructor.
  35. raw_ptr<std::string> json_string_;
  36. bool pretty_print_; // If true, serialization will span multiple lines.
  37. };
  38. class BASE_EXPORT JSONStringValueDeserializer : public base::ValueDeserializer {
  39. public:
  40. // This retains a reference to the contents of |json_string|, so the data
  41. // must outlive the JSONStringValueDeserializer. |options| is a bitmask of
  42. // JSONParserOptions.
  43. explicit JSONStringValueDeserializer(
  44. const base::StringPiece& json_string,
  45. int options = base::JSON_PARSE_CHROMIUM_EXTENSIONS);
  46. JSONStringValueDeserializer(const JSONStringValueDeserializer&) = delete;
  47. JSONStringValueDeserializer& operator=(const JSONStringValueDeserializer&) =
  48. delete;
  49. ~JSONStringValueDeserializer() override;
  50. // Attempts to deserialize |json_string_| into a structure of Value objects.
  51. // If the return value is null, then
  52. // (1) |error_code| will be filled with an integer error code
  53. // (base::ValueDeserializer::kErrorCodeInvalidFormat) if a non-null
  54. // |error_code| was given.
  55. // (2) |error_message| will be filled with a formatted error message,
  56. // including the location of the error (if appropriate), if a non-null
  57. // |error_message| was given.
  58. // The caller takes ownership of the returned value.
  59. std::unique_ptr<base::Value> Deserialize(int* error_code,
  60. std::string* error_message) override;
  61. private:
  62. // Data is owned by the caller of the constructor.
  63. base::StringPiece json_string_;
  64. const int options_;
  65. };
  66. #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_