| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 // This version allows initialization with a const string reference for | 27 // This version allows initialization with a const string reference for |
| 28 // deserialization only. | 28 // deserialization only. |
| 29 explicit JSONStringValueSerializer(const std::string& json_string) | 29 explicit JSONStringValueSerializer(const std::string& json_string) |
| 30 : json_string_(&const_cast<std::string&>(json_string)), | 30 : json_string_(&const_cast<std::string&>(json_string)), |
| 31 initialized_with_const_string_(true), | 31 initialized_with_const_string_(true), |
| 32 pretty_print_(false), | 32 pretty_print_(false), |
| 33 allow_trailing_comma_(false) { | 33 allow_trailing_comma_(false) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 ~JSONStringValueSerializer(); | 36 virtual ~JSONStringValueSerializer(); |
| 37 | 37 |
| 38 // Attempt to serialize the data structure represented by Value into | 38 // Attempt to serialize the data structure represented by Value into |
| 39 // 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 |
| 40 // into the string passed into the constructor. | 40 // into the string passed into the constructor. |
| 41 bool Serialize(const Value& root); | 41 virtual bool Serialize(const Value& root); |
| 42 | 42 |
| 43 // Attempt to deserialize the data structure encoded in the string passed | 43 // Attempt to deserialize the data structure encoded in the string passed |
| 44 // in to the constructor into a structure of Value objects. If the return | 44 // in to the constructor into a structure of Value objects. If the return |
| 45 // value is NULL, and if |error_code| is non-null, |error_code| will | 45 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 46 // contain an integer error code (either JsonFileError or JsonParseError). | 46 // contain an integer error code (either JsonFileError or JsonParseError). |
| 47 // If |error_message| is non-null, it will be filled in with a formatted | 47 // If |error_message| is non-null, it will be filled in with a formatted |
| 48 // error message including the location of the error if appropriate. | 48 // error message including the location of the error if appropriate. |
| 49 // The caller takes ownership of the returned value. | 49 // The caller takes ownership of the returned value. |
| 50 Value* Deserialize(int* error_code, std::string* error_message); | 50 virtual Value* Deserialize(int* error_code, std::string* error_message); |
| 51 | 51 |
| 52 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 52 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
| 53 bool pretty_print() { return pretty_print_; } | 53 bool pretty_print() { return pretty_print_; } |
| 54 | 54 |
| 55 void set_allow_trailing_comma(bool new_value) { | 55 void set_allow_trailing_comma(bool new_value) { |
| 56 allow_trailing_comma_ = new_value; | 56 allow_trailing_comma_ = new_value; |
| 57 } | 57 } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 std::string* json_string_; | 60 std::string* json_string_; |
| 61 bool initialized_with_const_string_; | 61 bool initialized_with_const_string_; |
| 62 bool pretty_print_; // If true, serialization will span multiple lines. | 62 bool pretty_print_; // If true, serialization will span multiple lines. |
| 63 // If true, deserialization will allow trailing commas. | 63 // If true, deserialization will allow trailing commas. |
| 64 bool allow_trailing_comma_; | 64 bool allow_trailing_comma_; |
| 65 | 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 66 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 class JSONFileValueSerializer : public ValueSerializer { | 69 class JSONFileValueSerializer : public ValueSerializer { |
| 70 public: | 70 public: |
| 71 // json_file_patch is the path of a file that will be source of the | 71 // json_file_patch is the path of a file that will be source of the |
| 72 // deserialization or the destination of the serialization. | 72 // deserialization or the destination of the serialization. |
| 73 // When deserializing, the file should exist, but when serializing, the | 73 // When deserializing, the file should exist, but when serializing, the |
| 74 // serializer will attempt to create the file at the specified location. | 74 // serializer will attempt to create the file at the specified location. |
| 75 explicit JSONFileValueSerializer(const FilePath& json_file_path) | 75 explicit JSONFileValueSerializer(const FilePath& json_file_path) |
| 76 : json_file_path_(json_file_path) {} | 76 : json_file_path_(json_file_path) {} |
| 77 | 77 |
| 78 ~JSONFileValueSerializer() {} | 78 virtual ~JSONFileValueSerializer() {} |
| 79 | 79 |
| 80 // DO NOT USE except in unit tests to verify the file was written properly. | 80 // DO NOT USE except in unit tests to verify the file was written properly. |
| 81 // We should never serialize directly to a file since this will block the | 81 // We should never serialize directly to a file since this will block the |
| 82 // thread. Instead, serialize to a string and write to the file you want on | 82 // thread. Instead, serialize to a string and write to the file you want on |
| 83 // the file thread. | 83 // the file thread. |
| 84 // | 84 // |
| 85 // Attempt to serialize the data structure represented by Value into | 85 // Attempt to serialize the data structure represented by Value into |
| 86 // JSON. If the return value is true, the result will have been written | 86 // JSON. If the return value is true, the result will have been written |
| 87 // into the file whose name was passed into the constructor. | 87 // into the file whose name was passed into the constructor. |
| 88 bool Serialize(const Value& root); | 88 virtual bool Serialize(const Value& root); |
| 89 | 89 |
| 90 // Attempt to deserialize the data structure encoded in the file passed | 90 // Attempt to deserialize the data structure encoded in the file passed |
| 91 // in to the constructor into a structure of Value objects. If the return | 91 // in to the constructor into a structure of Value objects. If the return |
| 92 // value is NULL, and if |error_code| is non-null, |error_code| will | 92 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 93 // contain an integer error code (either JsonFileError or JsonParseError). | 93 // contain an integer error code (either JsonFileError or JsonParseError). |
| 94 // If |error_message| is non-null, it will be filled in with a formatted | 94 // If |error_message| is non-null, it will be filled in with a formatted |
| 95 // error message including the location of the error if appropriate. | 95 // error message including the location of the error if appropriate. |
| 96 // The caller takes ownership of the returned value. | 96 // The caller takes ownership of the returned value. |
| 97 Value* Deserialize(int* error_code, std::string* error_message); | 97 virtual Value* Deserialize(int* error_code, std::string* error_message); |
| 98 | 98 |
| 99 // This enum is designed to safely overlap with JSONReader::JsonParseError. | 99 // This enum is designed to safely overlap with JSONReader::JsonParseError. |
| 100 enum JsonFileError { | 100 enum JsonFileError { |
| 101 JSON_NO_ERROR = 0, | 101 JSON_NO_ERROR = 0, |
| 102 JSON_ACCESS_DENIED = 1000, | 102 JSON_ACCESS_DENIED = 1000, |
| 103 JSON_CANNOT_READ_FILE, | 103 JSON_CANNOT_READ_FILE, |
| 104 JSON_FILE_LOCKED, | 104 JSON_FILE_LOCKED, |
| 105 JSON_NO_SUCH_FILE | 105 JSON_NO_SUCH_FILE |
| 106 }; | 106 }; |
| 107 | 107 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 119 FilePath json_file_path_; | 119 FilePath json_file_path_; |
| 120 | 120 |
| 121 // A wrapper for file_util::ReadFileToString which returns a non-zero | 121 // A wrapper for file_util::ReadFileToString which returns a non-zero |
| 122 // JsonFileError if there were file errors. | 122 // JsonFileError if there were file errors. |
| 123 int ReadFileToString(std::string* json_string); | 123 int ReadFileToString(std::string* json_string); |
| 124 | 124 |
| 125 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 125 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 #endif // CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ | 128 #endif // CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ |
| OLD | NEW |