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 21 matching lines...) Expand all Loading... |
32 initialized_with_const_string_(true), | 32 initialized_with_const_string_(true), |
33 pretty_print_(false), | 33 pretty_print_(false), |
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); | 42 virtual bool Serialize(const Value& root) OVERRIDE; |
43 | 43 |
44 // Attempt to deserialize the data structure encoded in the string passed | 44 // 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 | 45 // 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 | 46 // value is NULL, and if |error_code| is non-null, |error_code| will |
47 // contain an integer error code (either JsonFileError or JsonParseError). | 47 // contain an integer error code (either JsonFileError or JsonParseError). |
48 // If |error_message| is non-null, it will be filled in with a formatted | 48 // 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. | 49 // error message including the location of the error if appropriate. |
50 // The caller takes ownership of the returned value. | 50 // The caller takes ownership of the returned value. |
51 virtual Value* Deserialize(int* error_code, std::string* error_message); | 51 virtual Value* Deserialize(int* error_code, |
| 52 std::string* error_message) OVERRIDE; |
52 | 53 |
53 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 54 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
54 bool pretty_print() { return pretty_print_; } | 55 bool pretty_print() { return pretty_print_; } |
55 | 56 |
56 void set_allow_trailing_comma(bool new_value) { | 57 void set_allow_trailing_comma(bool new_value) { |
57 allow_trailing_comma_ = new_value; | 58 allow_trailing_comma_ = new_value; |
58 } | 59 } |
59 | 60 |
60 private: | 61 private: |
61 std::string* json_string_; | 62 std::string* json_string_; |
(...skipping 17 matching lines...) Expand all Loading... |
79 virtual ~JSONFileValueSerializer() {} | 80 virtual ~JSONFileValueSerializer() {} |
80 | 81 |
81 // DO NOT USE except in unit tests to verify the file was written properly. | 82 // DO NOT USE except in unit tests to verify the file was written properly. |
82 // We should never serialize directly to a file since this will block the | 83 // We should never serialize directly to a file since this will block the |
83 // thread. Instead, serialize to a string and write to the file you want on | 84 // thread. Instead, serialize to a string and write to the file you want on |
84 // the file thread. | 85 // the file thread. |
85 // | 86 // |
86 // Attempt to serialize the data structure represented by Value into | 87 // Attempt to serialize the data structure represented by Value into |
87 // JSON. If the return value is true, the result will have been written | 88 // JSON. If the return value is true, the result will have been written |
88 // into the file whose name was passed into the constructor. | 89 // into the file whose name was passed into the constructor. |
89 virtual bool Serialize(const Value& root); | 90 virtual bool Serialize(const Value& root) OVERRIDE; |
90 | 91 |
91 // Attempt to deserialize the data structure encoded in the file passed | 92 // Attempt to deserialize the data structure encoded in the file passed |
92 // in to the constructor into a structure of Value objects. If the return | 93 // in to the constructor into a structure of Value objects. If the return |
93 // value is NULL, and if |error_code| is non-null, |error_code| will | 94 // value is NULL, and if |error_code| is non-null, |error_code| will |
94 // contain an integer error code (either JsonFileError or JsonParseError). | 95 // contain an integer error code (either JsonFileError or JsonParseError). |
95 // If |error_message| is non-null, it will be filled in with a formatted | 96 // If |error_message| is non-null, it will be filled in with a formatted |
96 // error message including the location of the error if appropriate. | 97 // error message including the location of the error if appropriate. |
97 // The caller takes ownership of the returned value. | 98 // The caller takes ownership of the returned value. |
98 virtual Value* Deserialize(int* error_code, std::string* error_message); | 99 virtual Value* Deserialize(int* error_code, |
| 100 std::string* error_message) OVERRIDE; |
99 | 101 |
100 // This enum is designed to safely overlap with JSONReader::JsonParseError. | 102 // This enum is designed to safely overlap with JSONReader::JsonParseError. |
101 enum JsonFileError { | 103 enum JsonFileError { |
102 JSON_NO_ERROR = 0, | 104 JSON_NO_ERROR = 0, |
103 JSON_ACCESS_DENIED = 1000, | 105 JSON_ACCESS_DENIED = 1000, |
104 JSON_CANNOT_READ_FILE, | 106 JSON_CANNOT_READ_FILE, |
105 JSON_FILE_LOCKED, | 107 JSON_FILE_LOCKED, |
106 JSON_NO_SUCH_FILE | 108 JSON_NO_SUCH_FILE |
107 }; | 109 }; |
108 | 110 |
(...skipping 11 matching lines...) Expand all Loading... |
120 FilePath json_file_path_; | 122 FilePath json_file_path_; |
121 | 123 |
122 // A wrapper for file_util::ReadFileToString which returns a non-zero | 124 // A wrapper for file_util::ReadFileToString which returns a non-zero |
123 // JsonFileError if there were file errors. | 125 // JsonFileError if there were file errors. |
124 int ReadFileToString(std::string* json_string); | 126 int ReadFileToString(std::string* json_string); |
125 | 127 |
126 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 128 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
127 }; | 129 }; |
128 | 130 |
129 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 131 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
OLD | NEW |