| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ | 5 #ifndef CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ |
| 6 #define CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ | 6 #define CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 ~JSONStringValueSerializer(); | 35 ~JSONStringValueSerializer(); |
| 36 | 36 |
| 37 // Attempt to serialize the data structure represented by Value into | 37 // Attempt to serialize the data structure represented by Value into |
| 38 // JSON. If the return value is true, the result will have been written | 38 // JSON. If the return value is true, the result will have been written |
| 39 // into the string passed into the constructor. | 39 // into the string passed into the constructor. |
| 40 bool Serialize(const Value& root); | 40 bool Serialize(const Value& root); |
| 41 | 41 |
| 42 // Attempt to deserialize the data structure encoded in the string passed | 42 // Attempt to deserialize the data structure encoded in the string passed |
| 43 // in to the constructor into a structure of Value objects. If the return | 43 // in to the constructor into a structure of Value objects. If the return |
| 44 // value is NULL and |error_message| is non-null, |error_message| will contain | 44 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 45 // a string describing the error. | 45 // contain an integer error code (either JsonFileError or JsonParseError). |
| 46 Value* Deserialize(std::string* error_message); | 46 // If |error_message| is non-null, it will be filled in with a formatted |
| 47 // error message including the location of the error if appropriate. |
| 48 // The caller takes ownership of the returned value. |
| 49 Value* Deserialize(int* error_code, std::string* error_message); |
| 47 | 50 |
| 48 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 51 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
| 49 bool pretty_print() { return pretty_print_; } | 52 bool pretty_print() { return pretty_print_; } |
| 50 | 53 |
| 51 void set_allow_trailing_comma(bool new_value) { | 54 void set_allow_trailing_comma(bool new_value) { |
| 52 allow_trailing_comma_ = new_value; | 55 allow_trailing_comma_ = new_value; |
| 53 } | 56 } |
| 54 | 57 |
| 55 private: | 58 private: |
| 56 std::string* json_string_; | 59 std::string* json_string_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 78 // thread. Instead, serialize to a string and write to the file you want on | 81 // thread. Instead, serialize to a string and write to the file you want on |
| 79 // the file thread. | 82 // the file thread. |
| 80 // | 83 // |
| 81 // Attempt to serialize the data structure represented by Value into | 84 // Attempt to serialize the data structure represented by Value into |
| 82 // JSON. If the return value is true, the result will have been written | 85 // JSON. If the return value is true, the result will have been written |
| 83 // into the file whose name was passed into the constructor. | 86 // into the file whose name was passed into the constructor. |
| 84 bool Serialize(const Value& root); | 87 bool Serialize(const Value& root); |
| 85 | 88 |
| 86 // Attempt to deserialize the data structure encoded in the file passed | 89 // Attempt to deserialize the data structure encoded in the file passed |
| 87 // in to the constructor into a structure of Value objects. If the return | 90 // in to the constructor into a structure of Value objects. If the return |
| 88 // value is NULL, and if |error_message| is non-null, |error_message| will | 91 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 89 // contain a string describing the error. The caller takes ownership of the | 92 // contain an integer error code (either JsonFileError or JsonParseError). |
| 90 // returned value. | 93 // If |error_message| is non-null, it will be filled in with a formatted |
| 91 Value* Deserialize(std::string* error_message); | 94 // error message including the location of the error if appropriate. |
| 95 // The caller takes ownership of the returned value. |
| 96 Value* Deserialize(int* error_code, std::string* error_message); |
| 97 |
| 98 // This enum is designed to safely overlap with JSONReader::JsonParseError. |
| 99 enum JsonFileError { |
| 100 JSON_NO_ERROR = 0, |
| 101 JSON_ACCESS_DENIED = 1000, |
| 102 JSON_CANNOT_READ_FILE, |
| 103 JSON_FILE_LOCKED, |
| 104 JSON_NO_SUCH_FILE |
| 105 }; |
| 106 |
| 107 // File-specific error messages that can be returned. |
| 108 static const char* kAccessDenied; |
| 109 static const char* kCannotReadFile; |
| 110 static const char* kFileLocked; |
| 111 static const char* kNoSuchFile; |
| 112 |
| 113 // Convert an error code into an error message. |error_code| is assumed to |
| 114 // be a JsonFileError. |
| 115 static const char* GetErrorMessageForCode(int error_code); |
| 92 | 116 |
| 93 private: | 117 private: |
| 94 FilePath json_file_path_; | 118 FilePath json_file_path_; |
| 95 | 119 |
| 120 // A wrapper for file_util::ReadFileToString which returns a non-zero |
| 121 // JsonFileError if there were file errors. |
| 122 int ReadFileToString(std::string* json_string); |
| 123 |
| 96 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 124 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 97 }; | 125 }; |
| 98 | 126 |
| 99 #endif // CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ | 127 #endif // CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ |
| OLD | NEW |