| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ | 5 #ifndef BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 6 #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ | 6 #define BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // | 29 // |
| 30 // Attempt to serialize the data structure represented by Value into | 30 // Attempt to serialize the data structure represented by Value into |
| 31 // JSON. If the return value is true, the result will have been written | 31 // JSON. If the return value is true, the result will have been written |
| 32 // into the file whose name was passed into the constructor. | 32 // into the file whose name was passed into the constructor. |
| 33 bool Serialize(const base::Value& root) override; | 33 bool Serialize(const base::Value& root) override; |
| 34 | 34 |
| 35 // Equivalent to Serialize(root) except binary values are omitted from the | 35 // Equivalent to Serialize(root) except binary values are omitted from the |
| 36 // output. | 36 // output. |
| 37 bool SerializeAndOmitBinaryValues(const base::Value& root); | 37 bool SerializeAndOmitBinaryValues(const base::Value& root); |
| 38 | 38 |
| 39 private: |
| 40 bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
| 41 |
| 42 const base::FilePath json_file_path_; |
| 43 |
| 44 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 45 }; |
| 46 |
| 47 class BASE_EXPORT JSONFileValueDeserializer : public base::ValueDeserializer { |
| 48 public: |
| 49 // |json_file_path_| is the path of a file that will be source of the |
| 50 // deserialization or the destination of the serialization. |
| 51 // When deserializing, the file should exist, but when serializing, the |
| 52 // serializer will attempt to create the file at the specified location. |
| 53 explicit JSONFileValueDeserializer(const base::FilePath& json_file_path); |
| 54 |
| 55 ~JSONFileValueDeserializer() override; |
| 56 |
| 39 // Attempt to deserialize the data structure encoded in the file passed | 57 // Attempt to deserialize the data structure encoded in the file passed |
| 40 // in to the constructor into a structure of Value objects. If the return | 58 // in to the constructor into a structure of Value objects. If the return |
| 41 // value is NULL, and if |error_code| is non-null, |error_code| will | 59 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 42 // contain an integer error code (either JsonFileError or JsonParseError). | 60 // contain an integer error code (either JsonFileError or JsonParseError). |
| 43 // If |error_message| is non-null, it will be filled in with a formatted | 61 // If |error_message| is non-null, it will be filled in with a formatted |
| 44 // error message including the location of the error if appropriate. | 62 // error message including the location of the error if appropriate. |
| 45 // The caller takes ownership of the returned value. | 63 // The caller takes ownership of the returned value. |
| 46 base::Value* Deserialize(int* error_code, | 64 base::Value* Deserialize(int* error_code, |
| 47 std::string* error_message) override; | 65 std::string* error_message) override; |
| 48 | 66 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 | 85 |
| 68 void set_allow_trailing_comma(bool new_value) { | 86 void set_allow_trailing_comma(bool new_value) { |
| 69 allow_trailing_comma_ = new_value; | 87 allow_trailing_comma_ = new_value; |
| 70 } | 88 } |
| 71 | 89 |
| 72 // Returns the syze (in bytes) of JSON string read from disk in the last | 90 // Returns the syze (in bytes) of JSON string read from disk in the last |
| 73 // successful |Deserialize()| call. | 91 // successful |Deserialize()| call. |
| 74 size_t get_last_read_size() const { return last_read_size_; } | 92 size_t get_last_read_size() const { return last_read_size_; } |
| 75 | 93 |
| 76 private: | 94 private: |
| 77 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | 95 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if |
| 96 // there were file errors. |
| 97 int ReadFileToString(std::string* json_string); |
| 78 | 98 |
| 79 const base::FilePath json_file_path_; | 99 const base::FilePath json_file_path_; |
| 80 bool allow_trailing_comma_; | 100 bool allow_trailing_comma_; |
| 81 size_t last_read_size_; | 101 size_t last_read_size_; |
| 82 | 102 |
| 83 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if | 103 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer); |
| 84 // there were file errors. | |
| 85 int ReadFileToString(std::string* json_string); | |
| 86 | |
| 87 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | |
| 88 }; | 104 }; |
| 89 | 105 |
| 90 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ | 106 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 91 | 107 |
| OLD | NEW |