OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_VALUE_SERIALIZER_H_ | 5 #ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 allow_trailing_comma_(false) { | 34 allow_trailing_comma_(false) { |
35 } | 35 } |
36 | 36 |
37 virtual ~JSONStringValueSerializer(); | 37 virtual ~JSONStringValueSerializer(); |
38 | 38 |
39 // Attempt to serialize the data structure represented by Value into | 39 // Attempt to serialize the data structure represented by Value into |
40 // JSON. If the return value is true, the result will have been written | 40 // JSON. If the return value is true, the result will have been written |
41 // into the string passed into the constructor. | 41 // into the string passed into the constructor. |
42 virtual bool Serialize(const Value& root) OVERRIDE; | 42 virtual bool Serialize(const Value& root) OVERRIDE; |
43 | 43 |
| 44 // Equivalent to Serialize(root) except binary values are omitted from the |
| 45 // output. |
| 46 bool SerializeAndOmitBinaryValues(const Value& root); |
| 47 |
44 // Attempt to deserialize the data structure encoded in the string passed | 48 // Attempt to deserialize the data structure encoded in the string passed |
45 // in to the constructor into a structure of Value objects. If the return | 49 // in to the constructor into a structure of Value objects. If the return |
46 // value is NULL, and if |error_code| is non-null, |error_code| will | 50 // value is NULL, and if |error_code| is non-null, |error_code| will |
47 // contain an integer error code (either JsonFileError or JsonParseError). | 51 // contain an integer error code (either JsonFileError or JsonParseError). |
48 // If |error_message| is non-null, it will be filled in with a formatted | 52 // If |error_message| is non-null, it will be filled in with a formatted |
49 // error message including the location of the error if appropriate. | 53 // error message including the location of the error if appropriate. |
50 // The caller takes ownership of the returned value. | 54 // The caller takes ownership of the returned value. |
51 virtual Value* Deserialize(int* error_code, | 55 virtual Value* Deserialize(int* error_code, |
52 std::string* error_message) OVERRIDE; | 56 std::string* error_message) OVERRIDE; |
53 | 57 |
54 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 58 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
55 bool pretty_print() { return pretty_print_; } | 59 bool pretty_print() { return pretty_print_; } |
56 | 60 |
57 void set_allow_trailing_comma(bool new_value) { | 61 void set_allow_trailing_comma(bool new_value) { |
58 allow_trailing_comma_ = new_value; | 62 allow_trailing_comma_ = new_value; |
59 } | 63 } |
60 | 64 |
61 private: | 65 private: |
| 66 bool SerializeInternal(const Value& root, bool omit_binary_values); |
| 67 |
62 std::string* json_string_; | 68 std::string* json_string_; |
63 bool initialized_with_const_string_; | 69 bool initialized_with_const_string_; |
64 bool pretty_print_; // If true, serialization will span multiple lines. | 70 bool pretty_print_; // If true, serialization will span multiple lines. |
65 // If true, deserialization will allow trailing commas. | 71 // If true, deserialization will allow trailing commas. |
66 bool allow_trailing_comma_; | 72 bool allow_trailing_comma_; |
67 | 73 |
68 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 74 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
69 }; | 75 }; |
70 | 76 |
71 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { | 77 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
(...skipping 10 matching lines...) Expand all Loading... |
82 // DO NOT USE except in unit tests to verify the file was written properly. | 88 // DO NOT USE except in unit tests to verify the file was written properly. |
83 // We should never serialize directly to a file since this will block the | 89 // We should never serialize directly to a file since this will block the |
84 // thread. Instead, serialize to a string and write to the file you want on | 90 // thread. Instead, serialize to a string and write to the file you want on |
85 // the file thread. | 91 // the file thread. |
86 // | 92 // |
87 // Attempt to serialize the data structure represented by Value into | 93 // Attempt to serialize the data structure represented by Value into |
88 // JSON. If the return value is true, the result will have been written | 94 // JSON. If the return value is true, the result will have been written |
89 // into the file whose name was passed into the constructor. | 95 // into the file whose name was passed into the constructor. |
90 virtual bool Serialize(const Value& root) OVERRIDE; | 96 virtual bool Serialize(const Value& root) OVERRIDE; |
91 | 97 |
| 98 // Equivalent to Serialize(root) except binary values are omitted from the |
| 99 // output. |
| 100 bool SerializeAndOmitBinaryValues(const Value& root); |
| 101 |
92 // Attempt to deserialize the data structure encoded in the file passed | 102 // Attempt to deserialize the data structure encoded in the file passed |
93 // in to the constructor into a structure of Value objects. If the return | 103 // in to the constructor into a structure of Value objects. If the return |
94 // value is NULL, and if |error_code| is non-null, |error_code| will | 104 // value is NULL, and if |error_code| is non-null, |error_code| will |
95 // contain an integer error code (either JsonFileError or JsonParseError). | 105 // contain an integer error code (either JsonFileError or JsonParseError). |
96 // If |error_message| is non-null, it will be filled in with a formatted | 106 // If |error_message| is non-null, it will be filled in with a formatted |
97 // error message including the location of the error if appropriate. | 107 // error message including the location of the error if appropriate. |
98 // The caller takes ownership of the returned value. | 108 // The caller takes ownership of the returned value. |
99 virtual Value* Deserialize(int* error_code, | 109 virtual Value* Deserialize(int* error_code, |
100 std::string* error_message) OVERRIDE; | 110 std::string* error_message) OVERRIDE; |
101 | 111 |
(...skipping 10 matching lines...) Expand all Loading... |
112 static const char* kAccessDenied; | 122 static const char* kAccessDenied; |
113 static const char* kCannotReadFile; | 123 static const char* kCannotReadFile; |
114 static const char* kFileLocked; | 124 static const char* kFileLocked; |
115 static const char* kNoSuchFile; | 125 static const char* kNoSuchFile; |
116 | 126 |
117 // Convert an error code into an error message. |error_code| is assumed to | 127 // Convert an error code into an error message. |error_code| is assumed to |
118 // be a JsonFileError. | 128 // be a JsonFileError. |
119 static const char* GetErrorMessageForCode(int error_code); | 129 static const char* GetErrorMessageForCode(int error_code); |
120 | 130 |
121 private: | 131 private: |
| 132 bool SerializeInternal(const Value& root, bool omit_binary_values); |
| 133 |
122 FilePath json_file_path_; | 134 FilePath json_file_path_; |
123 | 135 |
124 // A wrapper for file_util::ReadFileToString which returns a non-zero | 136 // A wrapper for file_util::ReadFileToString which returns a non-zero |
125 // JsonFileError if there were file errors. | 137 // JsonFileError if there were file errors. |
126 int ReadFileToString(std::string* json_string); | 138 int ReadFileToString(std::string* json_string); |
127 | 139 |
128 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 140 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
129 }; | 141 }; |
130 | 142 |
131 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 143 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
OLD | NEW |