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 // If |histogram_suffix| is non-empty, will log a |
22 : json_file_path_(json_file_path), | 22 // "Settings.JsonDataSizeKilobytes.|histogram_suffix|" UMA histogram with the |
23 allow_trailing_comma_(false) {} | 23 // size of the content read from disk while deserializing. |
24 JSONFileValueSerializer(const base::FilePath& json_file_path, | |
25 const std::string& histogram_suffix); | |
Nico
2015/01/30 18:46:28
Why does a json library need to expose histograms
| |
24 | 26 |
25 ~JSONFileValueSerializer() override {} | 27 // Constructs a JSONFileValueSerializer as above but with an empty |
28 // |histogram_suffix|. | |
29 explicit JSONFileValueSerializer(const base::FilePath& json_file_path); | |
30 | |
31 ~JSONFileValueSerializer() override; | |
26 | 32 |
27 // DO NOT USE except in unit tests to verify the file was written properly. | 33 // 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 | 34 // 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 | 35 // thread. Instead, serialize to a string and write to the file you want on |
30 // the file thread. | 36 // the file thread. |
31 // | 37 // |
32 // Attempt to serialize the data structure represented by Value into | 38 // Attempt to serialize the data structure represented by Value into |
33 // JSON. If the return value is true, the result will have been written | 39 // JSON. If the return value is true, the result will have been written |
34 // into the file whose name was passed into the constructor. | 40 // into the file whose name was passed into the constructor. |
35 bool Serialize(const base::Value& root) override; | 41 bool Serialize(const base::Value& root) override; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 // be a JsonFileError. | 73 // be a JsonFileError. |
68 static const char* GetErrorMessageForCode(int error_code); | 74 static const char* GetErrorMessageForCode(int error_code); |
69 | 75 |
70 void set_allow_trailing_comma(bool new_value) { | 76 void set_allow_trailing_comma(bool new_value) { |
71 allow_trailing_comma_ = new_value; | 77 allow_trailing_comma_ = new_value; |
72 } | 78 } |
73 | 79 |
74 private: | 80 private: |
75 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | 81 bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
76 | 82 |
77 base::FilePath json_file_path_; | 83 const base::FilePath json_file_path_; |
78 bool allow_trailing_comma_; | 84 bool allow_trailing_comma_; |
85 const std::string histogram_suffix_; | |
79 | 86 |
80 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if | 87 // A wrapper for ReadFileToString which returns a non-zero JsonFileError if |
81 // there were file errors. | 88 // there were file errors. |
82 int ReadFileToString(std::string* json_string); | 89 int ReadFileToString(std::string* json_string); |
83 | 90 |
84 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 91 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
85 }; | 92 }; |
86 | 93 |
87 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ | 94 #endif // BASE_JSON_JSON_FILE_VALUE_SERIALIZER_H_ |
88 | 95 |
OLD | NEW |