James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | |
| 5 | #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 6 | #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/basictypes.h" |
| 12 | #include "base/files/file_path.h" |
| 13 | #include "base/values.h" |
| 14 | |
| 15 | class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
| 16 | public: |
Dave Moore | cc0e4f9 | 2015-03-10 15:23:04 -0700 | [diff] [blame] | 17 | // |json_file_path_| is the path of a file that will be destination of the |
| 18 | // serialization. The serializer will attempt to create the file at the |
| 19 | // specified location. |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 20 | explicit JSONFileValueSerializer(const base::FilePath& json_file_path); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 21 | |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 22 | ~JSONFileValueSerializer() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 23 | |
| 24 | // DO NOT USE except in unit tests to verify the file was written properly. |
| 25 | // We should never serialize directly to a file since this will block the |
| 26 | // thread. Instead, serialize to a string and write to the file you want on |
| 27 | // the file thread. |
| 28 | // |
| 29 | // Attempt to serialize the data structure represented by Value into |
| 30 | // JSON. If the return value is true, the result will have been written |
| 31 | // into the file whose name was passed into the constructor. |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 32 | bool Serialize(const base::Value& root) override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 33 | |
| 34 | // Equivalent to Serialize(root) except binary values are omitted from the |
| 35 | // output. |
| 36 | bool SerializeAndOmitBinaryValues(const base::Value& root); |
| 37 | |
Dave Moore | cc0e4f9 | 2015-03-10 15:23:04 -0700 | [diff] [blame] | 38 | private: |
| 39 | bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
| 40 | |
| 41 | const base::FilePath json_file_path_; |
| 42 | |
| 43 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 44 | }; |
| 45 | |
| 46 | class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer { |
| 47 | public: |
| 48 | // |json_file_path_| is the path of a file that will be source of the |
| 49 | // deserialization. |
| 50 | explicit JSONFileValueDeserializer(const base::FilePath& json_file_path); |
| 51 | |
| 52 | ~JSONFileValueDeserializer() override; |
| 53 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 54 | // Attempt to deserialize the data structure encoded in the file passed |
| 55 | // in to the constructor into a structure of Value objects. If the return |
| 56 | // value is NULL, and if |error_code| is non-null, |error_code| will |
| 57 | // contain an integer error code (either JsonFileError or JsonParseError). |
| 58 | // If |error_message| is non-null, it will be filled in with a formatted |
| 59 | // error message including the location of the error if appropriate. |
| 60 | // The caller takes ownership of the returned value. |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 61 | base::Value* Deserialize(int* error_code, |
| 62 | std::string* error_message) override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 63 | |
| 64 | // This enum is designed to safely overlap with JSONReader::JsonParseError. |
| 65 | enum JsonFileError { |
| 66 | JSON_NO_ERROR = 0, |
| 67 | JSON_ACCESS_DENIED = 1000, |
| 68 | JSON_CANNOT_READ_FILE, |
| 69 | JSON_FILE_LOCKED, |
| 70 | JSON_NO_SUCH_FILE |
| 71 | }; |
| 72 | |
| 73 | // File-specific error messages that can be returned. |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 74 | static const char kAccessDenied[]; |
| 75 | static const char kCannotReadFile[]; |
| 76 | static const char kFileLocked[]; |
| 77 | static const char kNoSuchFile[]; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 78 | |
| 79 | // Convert an error code into an error message. |error_code| is assumed to |
| 80 | // be a JsonFileError. |
| 81 | static const char* GetErrorMessageForCode(int error_code); |
| 82 | |
| 83 | void set_allow_trailing_comma(bool new_value) { |
| 84 | allow_trailing_comma_ = new_value; |
| 85 | } |
| 86 | |
Dave Moore | cc0e4f9 | 2015-03-10 15:23:04 -0700 | [diff] [blame] | 87 | // Returns the size (in bytes) of JSON string read from disk in the last |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 88 | // successful |Deserialize()| call. |
| 89 | size_t get_last_read_size() const { return last_read_size_; } |
| 90 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 91 | private: |
Dave Moore | cc0e4f9 | 2015-03-10 15:23:04 -0700 | [diff] [blame] | 92 | // A wrapper for ReadFileToString which returns a non-zero JsonFileError if |
| 93 | // there were file errors. |
| 94 | int ReadFileToString(std::string* json_string); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 95 | |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 96 | const base::FilePath json_file_path_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 97 | bool allow_trailing_comma_; |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 98 | size_t last_read_size_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 99 | |
Dave Moore | cc0e4f9 | 2015-03-10 15:23:04 -0700 | [diff] [blame] | 100 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 104 | |