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 explicit JSONFileValueSerializer(const base::FilePath& json_file_path) |
18 : JSONFileValueSerializer(json_file_path, std::string()) {} | |
Alexei Svitkine (slow)
2015/01/30 13:49:03
Nit: Indent 2 more. Maybe run git cl format?
gab
2015/01/30 17:33:37
Good point, git cl formatted.
| |
19 | |
20 // |json_file_path_| is the path of a file that will be source of the | |
18 // deserialization or the destination of the serialization. | 21 // deserialization or the destination of the serialization. |
19 // When deserializing, the file should exist, but when serializing, the | 22 // When deserializing, the file should exist, but when serializing, the |
20 // serializer will attempt to create the file at the specified location. | 23 // serializer will attempt to create the file at the specified location. |
21 explicit JSONFileValueSerializer(const base::FilePath& json_file_path) | 24 // If |histogram_suffix| is non-empty, will log a |
25 // "Settings.JsonDataSizeKilobytes.|histogram_suffix|" UMA histogram with the | |
26 // size of the content read from disk while deserializing. | |
27 JSONFileValueSerializer(const base::FilePath& json_file_path, | |
28 const std::string& histogram_suffix) | |
22 : json_file_path_(json_file_path), | 29 : json_file_path_(json_file_path), |
23 allow_trailing_comma_(false) {} | 30 allow_trailing_comma_(false), |
31 histogram_suffix_(histogram_suffix) {} | |
Alexei Svitkine (slow)
2015/01/30 13:49:03
Nit: The ctors and dtor should probably be defined
gab
2015/01/30 17:33:37
They were already inlined so I'm not sure this CL
Alexei Svitkine (slow)
2015/01/30 17:41:16
But they're not POD types... a std::string is back
gab
2015/01/30 18:22:34
Okay you convinced me, done.
| |
24 | 32 |
25 ~JSONFileValueSerializer() override {} | 33 ~JSONFileValueSerializer() override {} |
26 | 34 |
27 // DO NOT USE except in unit tests to verify the file was written properly. | 35 // 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 | 36 // 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 | 37 // thread. Instead, serialize to a string and write to the file you want on |
30 // the file thread. | 38 // the file thread. |
31 // | 39 // |
32 // Attempt to serialize the data structure represented by Value into | 40 // Attempt to serialize the data structure represented by Value into |
33 // JSON. If the return value is true, the result will have been written | 41 // JSON. If the return value is true, the result will have been written |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 // be a JsonFileError. | 75 // be a JsonFileError. |
68 static const char* GetErrorMessageForCode(int error_code); | 76 static const char* GetErrorMessageForCode(int error_code); |
69 | 77 |
70 void set_allow_trailing_comma(bool new_value) { | 78 void set_allow_trailing_comma(bool new_value) { |
71 allow_trailing_comma_ = new_value; | 79 allow_trailing_comma_ = new_value; |
72 } | 80 } |
73 | 81 |
74 private: | 82 private: |
75 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | 83 bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
76 | 84 |
77 base::FilePath json_file_path_; | 85 const base::FilePath json_file_path_; |
78 bool allow_trailing_comma_; | 86 bool allow_trailing_comma_; |
87 const std::string histogram_suffix_; | |
79 | 88 |
80 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if | 89 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if |
81 // there were file errors. | 90 // there were file errors. |
82 int ReadFileToString(std::string* json_string); | 91 int ReadFileToString(std::string* json_string); |
83 | 92 |
84 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 93 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
85 }; | 94 }; |
86 | 95 |
87 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ | 96 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
88 | 97 |
OLD | NEW |