Chromium Code Reviews| 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" |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 | 14 |
| 15 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { | 15 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
| 16 public: | 16 public: |
| 17 // json_file_patch is the path of a file that will be source of the | 17 // |json_file_path_| is the path of a file that will be source of the |
| 18 // deserialization or the destination of the serialization. | 18 // deserialization or the destination of the serialization. |
| 19 // When deserializing, the file should exist, but when serializing, the | 19 // When deserializing, the file should exist, but when serializing, the |
| 20 // serializer will attempt to create the file at the specified location. | 20 // serializer will attempt to create the file at the specified location. |
| 21 explicit JSONFileValueSerializer(const base::FilePath& json_file_path) | 21 explicit JSONFileValueSerializer(const base::FilePath& json_file_path); |
| 22 : json_file_path_(json_file_path), | |
| 23 allow_trailing_comma_(false) {} | |
| 24 | 22 |
| 25 ~JSONFileValueSerializer() override {} | 23 ~JSONFileValueSerializer() override; |
| 26 | 24 |
| 27 // DO NOT USE except in unit tests to verify the file was written properly. | 25 // DO NOT USE except in unit tests to verify the file was written properly. |
| 28 // We should never serialize directly to a file since this will block the | 26 // We should never serialize directly to a file since this will block the |
| 29 // thread. Instead, serialize to a string and write to the file you want on | 27 // thread. Instead, serialize to a string and write to the file you want on |
| 30 // the file thread. | 28 // the file thread. |
| 31 // | 29 // |
| 32 // Attempt to serialize the data structure represented by Value into | 30 // Attempt to serialize the data structure represented by Value into |
| 33 // 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 |
| 34 // into the file whose name was passed into the constructor. | 32 // into the file whose name was passed into the constructor. |
| 35 bool Serialize(const base::Value& root) override; | 33 bool Serialize(const base::Value& root) override; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 64 static const char kNoSuchFile[]; | 62 static const char kNoSuchFile[]; |
| 65 | 63 |
| 66 // Convert an error code into an error message. |error_code| is assumed to | 64 // Convert an error code into an error message. |error_code| is assumed to |
| 67 // be a JsonFileError. | 65 // be a JsonFileError. |
| 68 static const char* GetErrorMessageForCode(int error_code); | 66 static const char* GetErrorMessageForCode(int error_code); |
| 69 | 67 |
| 70 void set_allow_trailing_comma(bool new_value) { | 68 void set_allow_trailing_comma(bool new_value) { |
| 71 allow_trailing_comma_ = new_value; | 69 allow_trailing_comma_ = new_value; |
| 72 } | 70 } |
| 73 | 71 |
| 72 // Returns the syze (in bytes) of JSON string read from disk in the last | |
| 73 // |Deserialize()| call. | |
|
Alexei Svitkine (slow)
2015/01/30 20:46:32
Looks like you're only updating the field if ReadF
gab
2015/01/31 16:46:56
Done.
| |
| 74 size_t get_last_read_size() { return last_read_size_; } | |
|
Alexei Svitkine (slow)
2015/01/30 20:46:32
Nit: const
gab
2015/01/31 16:46:56
Done.
| |
| 75 | |
| 74 private: | 76 private: |
| 75 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | 77 bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
| 76 | 78 |
| 77 base::FilePath json_file_path_; | 79 const base::FilePath json_file_path_; |
| 78 bool allow_trailing_comma_; | 80 bool allow_trailing_comma_; |
| 81 size_t last_read_size_; | |
| 79 | 82 |
| 80 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if | 83 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if |
| 81 // there were file errors. | 84 // there were file errors. |
| 82 int ReadFileToString(std::string* json_string); | 85 int ReadFileToString(std::string* json_string); |
| 83 | 86 |
| 84 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 87 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 85 }; | 88 }; |
| 86 | 89 |
| 87 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ | 90 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
| 88 | 91 |
| OLD | NEW |