Chromium Code Reviews| 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 18 matching lines...) Expand all Loading... | |
| 29 // deserialization only. | 29 // deserialization only. |
| 30 explicit JSONStringValueSerializer(const std::string& json_string) | 30 explicit JSONStringValueSerializer(const std::string& json_string) |
| 31 : json_string_(&const_cast<std::string&>(json_string)), | 31 : json_string_(&const_cast<std::string&>(json_string)), |
| 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 // Equivalent to calling Serialize(root, false). | |
|
willchan no longer on Chromium
2011/11/10 23:46:29
Comments are off.
Eric Dingle
2011/11/14 19:03:56
Done.
| |
| 40 virtual bool Serialize(const Value& root) OVERRIDE; | |
| 41 | |
| 39 // Attempt to serialize the data structure represented by Value into | 42 // Attempt to serialize the data structure represented by Value into |
| 40 // JSON. If the return value is true, the result will have been written | 43 // JSON. If the return value is true, the result will have been written |
| 41 // into the string passed into the constructor. | 44 // into the string passed into the constructor. |omit_binary_values| is |
| 42 virtual bool Serialize(const Value& root); | 45 // used to indicate how the serializer should behave when encountering a |
| 46 // binary value. | |
| 47 bool SerializeWithOptionalBinaryValues(const Value& root, | |
|
willchan no longer on Chromium
2011/11/10 23:46:29
There's no reason to use this except when we want
Eric Dingle
2011/11/14 19:03:56
Done.
| |
| 48 bool omit_binary_values); | |
| 43 | 49 |
| 44 // Attempt to deserialize the data structure encoded in the string passed | 50 // 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 | 51 // 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 | 52 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 47 // contain an integer error code (either JsonFileError or JsonParseError). | 53 // contain an integer error code (either JsonFileError or JsonParseError). |
| 48 // If |error_message| is non-null, it will be filled in with a formatted | 54 // 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. | 55 // error message including the location of the error if appropriate. |
| 50 // The caller takes ownership of the returned value. | 56 // The caller takes ownership of the returned value. |
| 51 virtual Value* Deserialize(int* error_code, std::string* error_message); | 57 virtual Value* Deserialize(int* error_code, std::string* error_message) |
| 58 OVERRIDE; | |
| 52 | 59 |
| 53 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 60 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
| 54 bool pretty_print() { return pretty_print_; } | 61 bool pretty_print() { return pretty_print_; } |
| 55 | 62 |
| 56 void set_allow_trailing_comma(bool new_value) { | 63 void set_allow_trailing_comma(bool new_value) { |
| 57 allow_trailing_comma_ = new_value; | 64 allow_trailing_comma_ = new_value; |
| 58 } | 65 } |
| 59 | 66 |
| 60 private: | 67 private: |
| 61 std::string* json_string_; | 68 std::string* json_string_; |
| 62 bool initialized_with_const_string_; | 69 bool initialized_with_const_string_; |
| 63 bool pretty_print_; // If true, serialization will span multiple lines. | 70 bool pretty_print_; // If true, serialization will span multiple lines. |
| 64 // If true, deserialization will allow trailing commas. | 71 // If true, deserialization will allow trailing commas. |
| 65 bool allow_trailing_comma_; | 72 bool allow_trailing_comma_; |
| 66 | 73 |
| 67 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 74 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
| 68 }; | 75 }; |
| 69 | 76 |
| 70 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { | 77 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer { |
| 71 public: | 78 public: |
| 72 // json_file_patch is the path of a file that will be source of the | 79 // json_file_patch is the path of a file that will be source of the |
| 73 // deserialization or the destination of the serialization. | 80 // deserialization or the destination of the serialization. |
| 74 // When deserializing, the file should exist, but when serializing, the | 81 // When deserializing, the file should exist, but when serializing, the |
| 75 // serializer will attempt to create the file at the specified location. | 82 // serializer will attempt to create the file at the specified location. |
| 76 explicit JSONFileValueSerializer(const FilePath& json_file_path) | 83 explicit JSONFileValueSerializer(const FilePath& json_file_path) |
| 77 : json_file_path_(json_file_path) {} | 84 : json_file_path_(json_file_path) {} |
| 78 | 85 |
| 79 virtual ~JSONFileValueSerializer() {} | 86 virtual ~JSONFileValueSerializer() {} |
| 80 | 87 |
| 88 // Equivalent to calling Serialize(root, false). | |
|
willchan no longer on Chromium
2011/11/10 23:46:29
Comments are off.
Eric Dingle
2011/11/14 19:03:56
Done.
| |
| 89 virtual bool Serialize(const Value& root) OVERRIDE; | |
| 90 | |
| 81 // DO NOT USE except in unit tests to verify the file was written properly. | 91 // 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 | 92 // 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 | 93 // thread. Instead, serialize to a string and write to the file you want on |
| 84 // the file thread. | 94 // the file thread. |
| 85 // | 95 // |
| 86 // Attempt to serialize the data structure represented by Value into | 96 // Attempt to serialize the data structure represented by Value into |
| 87 // JSON. If the return value is true, the result will have been written | 97 // JSON. If the return value is true, the result will have been written |
| 88 // into the file whose name was passed into the constructor. | 98 // into the file whose name was passed into the constructor. |
| 89 virtual bool Serialize(const Value& root); | 99 bool SerializeWithOptionalBinaryValues(const Value& root, |
| 100 bool omit_binary_values); | |
| 90 | 101 |
| 91 // Attempt to deserialize the data structure encoded in the file passed | 102 // 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 | 103 // 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 | 104 // value is NULL, and if |error_code| is non-null, |error_code| will |
| 94 // contain an integer error code (either JsonFileError or JsonParseError). | 105 // contain an integer error code (either JsonFileError or JsonParseError). |
| 95 // 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 |
| 96 // error message including the location of the error if appropriate. | 107 // error message including the location of the error if appropriate. |
| 97 // The caller takes ownership of the returned value. | 108 // The caller takes ownership of the returned value. |
| 98 virtual Value* Deserialize(int* error_code, std::string* error_message); | 109 virtual Value* Deserialize(int* error_code, std::string* error_message) |
| 110 OVERRIDE; | |
| 99 | 111 |
| 100 // This enum is designed to safely overlap with JSONReader::JsonParseError. | 112 // This enum is designed to safely overlap with JSONReader::JsonParseError. |
| 101 enum JsonFileError { | 113 enum JsonFileError { |
| 102 JSON_NO_ERROR = 0, | 114 JSON_NO_ERROR = 0, |
| 103 JSON_ACCESS_DENIED = 1000, | 115 JSON_ACCESS_DENIED = 1000, |
| 104 JSON_CANNOT_READ_FILE, | 116 JSON_CANNOT_READ_FILE, |
| 105 JSON_FILE_LOCKED, | 117 JSON_FILE_LOCKED, |
| 106 JSON_NO_SUCH_FILE | 118 JSON_NO_SUCH_FILE |
| 107 }; | 119 }; |
| 108 | 120 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 120 FilePath json_file_path_; | 132 FilePath json_file_path_; |
| 121 | 133 |
| 122 // A wrapper for file_util::ReadFileToString which returns a non-zero | 134 // A wrapper for file_util::ReadFileToString which returns a non-zero |
| 123 // JsonFileError if there were file errors. | 135 // JsonFileError if there were file errors. |
| 124 int ReadFileToString(std::string* json_string); | 136 int ReadFileToString(std::string* json_string); |
| 125 | 137 |
| 126 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 138 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
| 127 }; | 139 }; |
| 128 | 140 |
| 129 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ | 141 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_ |
| OLD | NEW |