json_string_value_serializer.h 2.9 KB

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