OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_STRING_VALUE_SERIALIZER_H_ | 5 #ifndef BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
6 #define BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ | 6 #define BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 | 14 |
15 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { | 15 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { |
16 public: | 16 public: |
17 // json_string is the string that will be source of the deserialization | 17 // |json_string| is the string that will be source of the deserialization |
18 // or the destination of the serialization. The caller of the constructor | 18 // or the destination of the serialization. The caller of the constructor |
19 // retains ownership of the string. | 19 // retains ownership of the string. |json_string| must not be null. |
20 explicit JSONStringValueSerializer(std::string* json_string) | 20 explicit JSONStringValueSerializer(std::string* json_string); |
21 : json_string_(json_string), | |
22 initialized_with_const_string_(false), | |
23 pretty_print_(false), | |
24 allow_trailing_comma_(false) { | |
25 } | |
26 | 21 |
27 // This version allows initialization with a const string reference for | 22 // This version allows initialization with a const string reference for |
28 // deserialization only. | 23 // deserialization only. Retains a reference to |json_string|, so the string |
29 explicit JSONStringValueSerializer(const std::string& json_string) | 24 // argument must outlive the JSONStringValueSerializer. |
30 : json_string_(&const_cast<std::string&>(json_string)), | 25 explicit JSONStringValueSerializer(const std::string& json_string); |
31 initialized_with_const_string_(true), | |
32 pretty_print_(false), | |
33 allow_trailing_comma_(false) { | |
34 } | |
35 | 26 |
36 ~JSONStringValueSerializer() override; | 27 ~JSONStringValueSerializer() override; |
37 | 28 |
38 // Attempt to serialize the data structure represented by Value into | 29 // Attempt to serialize the data structure represented by Value into |
39 // JSON. If the return value is true, the result will have been written | 30 // JSON. If the return value is true, the result will have been written |
40 // into the string passed into the constructor. | 31 // into the string passed into the constructor. |
41 bool Serialize(const base::Value& root) override; | 32 bool Serialize(const base::Value& root) override; |
42 | 33 |
43 // Equivalent to Serialize(root) except binary values are omitted from the | 34 // Equivalent to Serialize(root) except binary values are omitted from the |
44 // output. | 35 // output. |
45 bool SerializeAndOmitBinaryValues(const base::Value& root); | 36 bool SerializeAndOmitBinaryValues(const base::Value& root); |
46 | 37 |
47 // Attempt to deserialize the data structure encoded in the string passed | 38 // Attempt to deserialize the data structure encoded in the string passed |
48 // in to the constructor into a structure of Value objects. If the return | 39 // in to the constructor into a structure of Value objects. If the return |
49 // value is NULL, and if |error_code| is non-null, |error_code| will | 40 // value is null, and if |error_code| is non-null, |error_code| will |
50 // contain an integer error code (a JsonParseError in this case). | 41 // contain an integer error code (a JsonParseError in this case). |
51 // If |error_message| is non-null, it will be filled in with a formatted | 42 // If |error_message| is non-null, it will be filled in with a formatted |
52 // error message including the location of the error if appropriate. | 43 // error message including the location of the error if appropriate. |
53 // The caller takes ownership of the returned value. | 44 // The caller takes ownership of the returned value. |
54 base::Value* Deserialize(int* error_code, | 45 base::Value* Deserialize(int* error_code, |
55 std::string* error_message) override; | 46 std::string* error_message) override; |
56 | 47 |
57 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 48 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
58 bool pretty_print() { return pretty_print_; } | 49 bool pretty_print() { return pretty_print_; } |
59 | 50 |
60 void set_allow_trailing_comma(bool new_value) { | 51 void set_allow_trailing_comma(bool new_value) { |
61 allow_trailing_comma_ = new_value; | 52 allow_trailing_comma_ = new_value; |
62 } | 53 } |
63 | 54 |
64 private: | 55 private: |
65 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | 56 bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
66 | 57 |
67 std::string* json_string_; | 58 std::string* json_string_; // Not null. |
68 bool initialized_with_const_string_; | 59 bool initialized_with_const_string_; |
69 bool pretty_print_; // If true, serialization will span multiple lines. | 60 bool pretty_print_; // If true, serialization will span multiple lines. |
70 // If true, deserialization will allow trailing commas. | 61 // If true, deserialization will allow trailing commas. |
71 bool allow_trailing_comma_; | 62 bool allow_trailing_comma_; |
72 | 63 |
73 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 64 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
74 }; | 65 }; |
75 | 66 |
76 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ | 67 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
77 | 68 |
OLD | NEW |