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