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) OVERRIDE; | 42 virtual bool Serialize(const Value& root); |
43 | |
44 // Equivalent to Serialize(root) except binary values are omitted from the | |
45 // output. | |
46 bool SerializeAndOmitBinaryValues(const Value& root); | |
47 | 43 |
48 // Attempt to deserialize the data structure encoded in the string passed | 44 // Attempt to deserialize the data structure encoded in the string passed |
49 // 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 |
50 // 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 |
51 // contain an integer error code (either JsonFileError or JsonParseError). | 47 // contain an integer error code (either JsonFileError or JsonParseError). |
52 // 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 |
53 // error message including the location of the error if appropriate. | 49 // error message including the location of the error if appropriate. |
54 // The caller takes ownership of the returned value. | 50 // The caller takes ownership of the returned value. |
55 virtual Value* Deserialize(int* error_code, std::string* error_message) | 51 virtual Value* Deserialize(int* error_code, std::string* error_message); |
56 OVERRIDE; | |
57 | 52 |
58 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 53 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
59 bool pretty_print() { return pretty_print_; } | 54 bool pretty_print() { return pretty_print_; } |
60 | 55 |
61 void set_allow_trailing_comma(bool new_value) { | 56 void set_allow_trailing_comma(bool new_value) { |
62 allow_trailing_comma_ = new_value; | 57 allow_trailing_comma_ = new_value; |
63 } | 58 } |
64 | 59 |
65 private: | 60 private: |
66 bool SerializeInternal(const Value& root, bool omit_binary_values); | |
67 | |
68 std::string* json_string_; | 61 std::string* json_string_; |
69 bool initialized_with_const_string_; | 62 bool initialized_with_const_string_; |
70 bool pretty_print_; // If true, serialization will span multiple lines. | 63 bool pretty_print_; // If true, serialization will span multiple lines. |
71 // If true, deserialization will allow trailing commas. | 64 // If true, deserialization will allow trailing commas. |
72 bool allow_trailing_comma_; | 65 bool allow_trailing_comma_; |
73 | 66 |
74 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 67 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
75 }; | 68 }; |
76 | 69 |
77 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { | 70 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
78 public: | 71 public: |
79 // json_file_patch is the path of a file that will be source of the | 72 // json_file_patch is the path of a file that will be source of the |
80 // deserialization or the destination of the serialization. | 73 // deserialization or the destination of the serialization. |
81 // When deserializing, the file should exist, but when serializing, the | 74 // When deserializing, the file should exist, but when serializing, the |
82 // serializer will attempt to create the file at the specified location. | 75 // serializer will attempt to create the file at the specified location. |
83 explicit JSONFileValueSerializer(const FilePath& json_file_path) | 76 explicit JSONFileValueSerializer(const FilePath& json_file_path) |
84 : json_file_path_(json_file_path) {} | 77 : json_file_path_(json_file_path) {} |
85 | 78 |
86 virtual ~JSONFileValueSerializer() {} | 79 virtual ~JSONFileValueSerializer() {} |
87 | 80 |
88 // DO NOT USE except in unit tests to verify the file was written properly. | 81 // DO NOT USE except in unit tests to verify the file was written properly. |
89 // We should never serialize directly to a file since this will block the | 82 // We should never serialize directly to a file since this will block the |
90 // thread. Instead, serialize to a string and write to the file you want on | 83 // thread. Instead, serialize to a string and write to the file you want on |
91 // the file thread. | 84 // the file thread. |
92 // | 85 // |
93 // Attempt to serialize the data structure represented by Value into | 86 // Attempt to serialize the data structure represented by Value into |
94 // JSON. If the return value is true, the result will have been written | 87 // JSON. If the return value is true, the result will have been written |
95 // into the file whose name was passed into the constructor. | 88 // into the file whose name was passed into the constructor. |
96 virtual bool Serialize(const Value& root) OVERRIDE; | 89 virtual bool Serialize(const Value& root); |
97 | |
98 // Equivalent to Serialize(root) except binary values are omitted from the | |
99 // output. | |
100 bool SerializeAndOmitBinaryValues(const Value& root); | |
101 | 90 |
102 // Attempt to deserialize the data structure encoded in the file passed | 91 // Attempt to deserialize the data structure encoded in the file passed |
103 // in to the constructor into a structure of Value objects. If the return | 92 // in to the constructor into a structure of Value objects. If the return |
104 // value is NULL, and if |error_code| is non-null, |error_code| will | 93 // value is NULL, and if |error_code| is non-null, |error_code| will |
105 // contain an integer error code (either JsonFileError or JsonParseError). | 94 // contain an integer error code (either JsonFileError or JsonParseError). |
106 // If |error_message| is non-null, it will be filled in with a formatted | 95 // If |error_message| is non-null, it will be filled in with a formatted |
107 // error message including the location of the error if appropriate. | 96 // error message including the location of the error if appropriate. |
108 // The caller takes ownership of the returned value. | 97 // The caller takes ownership of the returned value. |
109 virtual Value* Deserialize(int* error_code, std::string* error_message) | 98 virtual Value* Deserialize(int* error_code, std::string* error_message); |
110 OVERRIDE; | |
111 | 99 |
112 // This enum is designed to safely overlap with JSONReader::JsonParseError. | 100 // This enum is designed to safely overlap with JSONReader::JsonParseError. |
113 enum JsonFileError { | 101 enum JsonFileError { |
114 JSON_NO_ERROR = 0, | 102 JSON_NO_ERROR = 0, |
115 JSON_ACCESS_DENIED = 1000, | 103 JSON_ACCESS_DENIED = 1000, |
116 JSON_CANNOT_READ_FILE, | 104 JSON_CANNOT_READ_FILE, |
117 JSON_FILE_LOCKED, | 105 JSON_FILE_LOCKED, |
118 JSON_NO_SUCH_FILE | 106 JSON_NO_SUCH_FILE |
119 }; | 107 }; |
120 | 108 |
121 // File-specific error messages that can be returned. | 109 // File-specific error messages that can be returned. |
122 static const char* kAccessDenied; | 110 static const char* kAccessDenied; |
123 static const char* kCannotReadFile; | 111 static const char* kCannotReadFile; |
124 static const char* kFileLocked; | 112 static const char* kFileLocked; |
125 static const char* kNoSuchFile; | 113 static const char* kNoSuchFile; |
126 | 114 |
127 // Convert an error code into an error message. |error_code| is assumed to | 115 // Convert an error code into an error message. |error_code| is assumed to |
128 // be a JsonFileError. | 116 // be a JsonFileError. |
129 static const char* GetErrorMessageForCode(int error_code); | 117 static const char* GetErrorMessageForCode(int error_code); |
130 | 118 |
131 private: | 119 private: |
132 bool SerializeInternal(const Value& root, bool omit_binary_values); | |
133 | |
134 FilePath json_file_path_; | 120 FilePath json_file_path_; |
135 | 121 |
136 // A wrapper for file_util::ReadFileToString which returns a non-zero | 122 // A wrapper for file_util::ReadFileToString which returns a non-zero |
137 // JsonFileError if there were file errors. | 123 // JsonFileError if there were file errors. |
138 int ReadFileToString(std::string* json_string); | 124 int ReadFileToString(std::string* json_string); |
139 | 125 |
140 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 126 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
141 }; | 127 }; |
142 | 128 |
143 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 129 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
OLD | NEW |