OLD | NEW |
1 // Copyright (c) 2006-2008 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" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 | 13 |
14 class JSONStringValueSerializer : public ValueSerializer { | 14 class JSONStringValueSerializer : public ValueSerializer { |
15 public: | 15 public: |
16 // json_string is the string that will be source of the deserialization | 16 // json_string is the string that will be source of the deserialization |
17 // or the destination of the serialization. The caller of the constructor | 17 // or the destination of the serialization. The caller of the constructor |
18 // retains ownership of the string. | 18 // retains ownership of the string. |
19 JSONStringValueSerializer(std::string* json_string) | 19 JSONStringValueSerializer(std::string* json_string) |
20 : json_string_(json_string), | 20 : json_string_(json_string), |
21 initialized_with_const_string_(false), | 21 initialized_with_const_string_(false), |
22 pretty_print_(false), | 22 pretty_print_(false), |
23 allow_trailing_comma_(false) { | 23 allow_trailing_comma_(false) { |
24 } | 24 } |
25 | 25 |
26 // This version allows initialization with a const string reference for | 26 // This version allows initialization with a const string reference for |
27 // deserialization only. | 27 // deserialization only. |
28 JSONStringValueSerializer(const std::string& json_string) | 28 JSONStringValueSerializer(const std::string& json_string) |
29 : json_string_(&const_cast<std::string&>(json_string)), | 29 : json_string_(&const_cast<std::string&>(json_string)), |
30 initialized_with_const_string_(true), | 30 initialized_with_const_string_(true), |
| 31 pretty_print_(false), |
31 allow_trailing_comma_(false) { | 32 allow_trailing_comma_(false) { |
32 } | 33 } |
33 | 34 |
34 ~JSONStringValueSerializer(); | 35 ~JSONStringValueSerializer(); |
35 | 36 |
36 // Attempt to serialize the data structure represented by Value into | 37 // Attempt to serialize the data structure represented by Value into |
37 // 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 |
38 // into the string passed into the constructor. | 39 // into the string passed into the constructor. |
39 bool Serialize(const Value& root); | 40 bool Serialize(const Value& root); |
40 | 41 |
(...skipping 10 matching lines...) Expand all Loading... |
51 allow_trailing_comma_ = new_value; | 52 allow_trailing_comma_ = new_value; |
52 } | 53 } |
53 | 54 |
54 private: | 55 private: |
55 std::string* json_string_; | 56 std::string* json_string_; |
56 bool initialized_with_const_string_; | 57 bool initialized_with_const_string_; |
57 bool pretty_print_; // If true, serialization will span multiple lines. | 58 bool pretty_print_; // If true, serialization will span multiple lines. |
58 // If true, deserialization will allow trailing commas. | 59 // If true, deserialization will allow trailing commas. |
59 bool allow_trailing_comma_; | 60 bool allow_trailing_comma_; |
60 | 61 |
61 DISALLOW_EVIL_CONSTRUCTORS(JSONStringValueSerializer); | 62 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
62 }; | 63 }; |
63 | 64 |
64 class JSONFileValueSerializer : public ValueSerializer { | 65 class JSONFileValueSerializer : public ValueSerializer { |
65 public: | 66 public: |
66 // json_file_patch is the path of a file that will be source of the | 67 // json_file_patch is the path of a file that will be source of the |
67 // deserialization or the destination of the serialization. | 68 // deserialization or the destination of the serialization. |
68 // When deserializing, the file should exist, but when serializing, the | 69 // When deserializing, the file should exist, but when serializing, the |
69 // serializer will attempt to create the file at the specified location. | 70 // serializer will attempt to create the file at the specified location. |
70 explicit JSONFileValueSerializer(const FilePath& json_file_path) | 71 explicit JSONFileValueSerializer(const FilePath& json_file_path) |
71 : json_file_path_(json_file_path) {} | 72 : json_file_path_(json_file_path) {} |
(...skipping 17 matching lines...) Expand all Loading... |
89 // returned value. | 90 // returned value. |
90 Value* Deserialize(std::string* error_message); | 91 Value* Deserialize(std::string* error_message); |
91 | 92 |
92 private: | 93 private: |
93 FilePath json_file_path_; | 94 FilePath json_file_path_; |
94 | 95 |
95 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); | 96 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer); |
96 }; | 97 }; |
97 | 98 |
98 #endif // CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ | 99 #endif // CHROME_COMMON_JSON_VALUE_SERIALIZER_H_ |
OLD | NEW |