json_file_value_serializer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_FILE_VALUE_SERIALIZER_H_
  5. #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/files/file_path.h"
  10. #include "base/macros.h"
  11. #include "base/values.h"
  12. class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
  13. public:
  14. // |json_file_path_| is the path of a file that will be destination of the
  15. // serialization. The serializer will attempt to create the file at the
  16. // specified location.
  17. explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
  18. ~JSONFileValueSerializer() override;
  19. // DO NOT USE except in unit tests to verify the file was written properly.
  20. // We should never serialize directly to a file since this will block the
  21. // thread. Instead, serialize to a string and write to the file you want on
  22. // the file thread.
  23. //
  24. // Attempt to serialize the data structure represented by Value into
  25. // JSON. If the return value is true, the result will have been written
  26. // into the file whose name was passed into the constructor.
  27. bool Serialize(const base::Value& root) override;
  28. // Equivalent to Serialize(root) except binary values are omitted from the
  29. // output.
  30. bool SerializeAndOmitBinaryValues(const base::Value& root);
  31. private:
  32. bool SerializeInternal(const base::Value& root, bool omit_binary_values);
  33. const base::FilePath json_file_path_;
  34. DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
  35. };
  36. class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer {
  37. public:
  38. // |json_file_path_| is the path of a file that will be source of the
  39. // deserialization. |options| is a bitmask of JSONParserOptions.
  40. explicit JSONFileValueDeserializer(const base::FilePath& json_file_path,
  41. int options = 0);
  42. ~JSONFileValueDeserializer() override;
  43. // Attempt to deserialize the data structure encoded in the file 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 (either JsonFileError or JsonParseError).
  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. // This enum is designed to safely overlap with JSONReader::JsonParseError.
  53. enum JsonFileError {
  54. JSON_NO_ERROR = 0,
  55. JSON_ACCESS_DENIED = kErrorCodeFirstMetadataError,
  56. JSON_CANNOT_READ_FILE,
  57. JSON_FILE_LOCKED,
  58. JSON_NO_SUCH_FILE
  59. };
  60. // File-specific error messages that can be returned.
  61. static const char kAccessDenied[];
  62. static const char kCannotReadFile[];
  63. static const char kFileLocked[];
  64. static const char kNoSuchFile[];
  65. // Convert an error code into an error message. |error_code| is assumed to
  66. // be a JsonFileError.
  67. static const char* GetErrorMessageForCode(int error_code);
  68. // Returns the size (in bytes) of JSON string read from disk in the last
  69. // successful |Deserialize()| call.
  70. size_t get_last_read_size() const { return last_read_size_; }
  71. private:
  72. // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
  73. // there were file errors.
  74. int ReadFileToString(std::string* json_string);
  75. const base::FilePath json_file_path_;
  76. const int options_;
  77. size_t last_read_size_;
  78. DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
  79. };
  80. #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_