Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: base/json/json_file_value_serializer.h

Issue 925783002: Split ValueSerializer into separate Serializer and Deserializer classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed cpplint warnings. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/json/json_file_value_serializer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_path_| 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 destination of the
18 // deserialization or the destination of the serialization. 18 // serialization. The serializer will attempt to create the file at the
19 // When deserializing, the file should exist, but when serializing, the 19 // specified location.
20 // serializer will attempt to create the file at the specified location.
21 explicit JSONFileValueSerializer(const base::FilePath& json_file_path); 20 explicit JSONFileValueSerializer(const base::FilePath& json_file_path);
22 21
23 ~JSONFileValueSerializer() override; 22 ~JSONFileValueSerializer() override;
24 23
25 // DO NOT USE except in unit tests to verify the file was written properly. 24 // DO NOT USE except in unit tests to verify the file was written properly.
26 // We should never serialize directly to a file since this will block the 25 // We should never serialize directly to a file since this will block the
27 // thread. Instead, serialize to a string and write to the file you want on 26 // thread. Instead, serialize to a string and write to the file you want on
28 // the file thread. 27 // the file thread.
29 // 28 //
30 // Attempt to serialize the data structure represented by Value into 29 // Attempt to serialize the data structure represented by Value into
31 // JSON. If the return value is true, the result will have been written 30 // JSON. If the return value is true, the result will have been written
32 // into the file whose name was passed into the constructor. 31 // into the file whose name was passed into the constructor.
33 bool Serialize(const base::Value& root) override; 32 bool Serialize(const base::Value& root) override;
34 33
35 // Equivalent to Serialize(root) except binary values are omitted from the 34 // Equivalent to Serialize(root) except binary values are omitted from the
36 // output. 35 // output.
37 bool SerializeAndOmitBinaryValues(const base::Value& root); 36 bool SerializeAndOmitBinaryValues(const base::Value& root);
38 37
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
39 // Attempt to deserialize the data structure encoded in the file passed 54 // 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 55 // 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 56 // value is NULL, and if |error_code| is non-null, |error_code| will
42 // contain an integer error code (either JsonFileError or JsonParseError). 57 // contain an integer error code (either JsonFileError or JsonParseError).
43 // If |error_message| is non-null, it will be filled in with a formatted 58 // 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. 59 // error message including the location of the error if appropriate.
45 // The caller takes ownership of the returned value. 60 // The caller takes ownership of the returned value.
46 base::Value* Deserialize(int* error_code, 61 base::Value* Deserialize(int* error_code,
47 std::string* error_message) override; 62 std::string* error_message) override;
48 63
(...skipping 13 matching lines...) Expand all
62 static const char kNoSuchFile[]; 77 static const char kNoSuchFile[];
63 78
64 // Convert an error code into an error message. |error_code| is assumed to 79 // Convert an error code into an error message. |error_code| is assumed to
65 // be a JsonFileError. 80 // be a JsonFileError.
66 static const char* GetErrorMessageForCode(int error_code); 81 static const char* GetErrorMessageForCode(int error_code);
67 82
68 void set_allow_trailing_comma(bool new_value) { 83 void set_allow_trailing_comma(bool new_value) {
69 allow_trailing_comma_ = new_value; 84 allow_trailing_comma_ = new_value;
70 } 85 }
71 86
72 // Returns the syze (in bytes) of JSON string read from disk in the last 87 // Returns the size (in bytes) of JSON string read from disk in the last
73 // successful |Deserialize()| call. 88 // successful |Deserialize()| call.
74 size_t get_last_read_size() const { return last_read_size_; } 89 size_t get_last_read_size() const { return last_read_size_; }
75 90
76 private: 91 private:
77 bool SerializeInternal(const base::Value& root, bool omit_binary_values); 92 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if
93 // there were file errors.
94 int ReadFileToString(std::string* json_string);
78 95
79 const base::FilePath json_file_path_; 96 const base::FilePath json_file_path_;
80 bool allow_trailing_comma_; 97 bool allow_trailing_comma_;
81 size_t last_read_size_; 98 size_t last_read_size_;
82 99
83 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if 100 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueDeserializer);
84 // there were file errors.
85 int ReadFileToString(std::string* json_string);
86
87 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
88 }; 101 };
89 102
90 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ 103 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_
91 104
OLDNEW
« no previous file with comments | « no previous file | base/json/json_file_value_serializer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698