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/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 | 15 |
16 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { | 16 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { |
17 public: | 17 public: |
18 // |json_string| is the string that will be source of the deserialization | 18 // |json_string| is the string that will be source of the deserialization |
19 // or the destination of the serialization. The caller of the constructor | 19 // or the destination of the serialization. The caller of the constructor |
20 // retains ownership of the string. |json_string| must not be null. | 20 // retains ownership of the string. |json_string| must not be null. |
21 explicit JSONStringValueSerializer(std::string* json_string); | 21 explicit JSONStringValueSerializer(std::string* json_string); |
22 | 22 |
23 // This version allows initialization with a StringPiece for deserialization | |
24 // only. Retains a reference to the contents of |json_string|, so the data | |
25 // must outlive the JSONStringValueSerializer. | |
26 explicit JSONStringValueSerializer(const base::StringPiece& json_string); | |
27 | |
28 ~JSONStringValueSerializer() override; | 23 ~JSONStringValueSerializer() override; |
29 | 24 |
30 // Attempt to serialize the data structure represented by Value into | 25 // Attempt to serialize the data structure represented by Value into |
31 // JSON. If the return value is true, the result will have been written | 26 // JSON. If the return value is true, the result will have been written |
32 // into the string passed into the constructor. | 27 // into the string passed into the constructor. |
33 bool Serialize(const base::Value& root) override; | 28 bool Serialize(const base::Value& root) override; |
34 | 29 |
35 // Equivalent to Serialize(root) except binary values are omitted from the | 30 // Equivalent to Serialize(root) except binary values are omitted from the |
36 // output. | 31 // output. |
37 bool SerializeAndOmitBinaryValues(const base::Value& root); | 32 bool SerializeAndOmitBinaryValues(const base::Value& root); |
38 | 33 |
34 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | |
35 bool pretty_print() { return pretty_print_; } | |
36 | |
37 private: | |
38 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | |
39 | |
40 // String for writing. Owned by the caller of the constructor. Will be null if | |
Matt Giuca
2015/02/16 00:18:27
Rewrite this comment. "String for writing" no long
| |
41 // the serializer was initialized with a const string. | |
Matt Giuca
2015/02/16 00:18:27
Delete the last sentence; it is no longer possible
| |
42 std::string* json_string_; | |
43 bool pretty_print_; // If true, serialization will span multiple lines. | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | |
46 }; | |
47 | |
48 class BASE_EXPORT JSONStringValueDeserializer : public base::ValueDeserializer { | |
49 public: | |
50 // |json_string| is the string that will be source of the deserialization. | |
51 // The caller of the constructor retains ownership of the string. | |
52 // |json_string| must not be null. | |
53 explicit JSONStringValueDeserializer(std::string* json_string); | |
Matt Giuca
2015/02/16 00:18:27
This constructor should be deleted (the whole poin
| |
54 | |
55 // This version allows initialization with a StringPiece for deserialization | |
Matt Giuca
2015/02/16 00:18:27
You will need to update this comment ("This versio
| |
56 // only. Retains a reference to the contents of |json_string|, so the data | |
57 // must outlive the JSONStringValueSerializer. | |
58 explicit JSONStringValueDeserializer(const base::StringPiece& json_string); | |
59 | |
60 ~JSONStringValueDeserializer() override; | |
61 | |
39 // Attempt to deserialize the data structure encoded in the string passed | 62 // Attempt to deserialize the data structure encoded in the string passed |
40 // in to the constructor into a structure of Value objects. If the return | 63 // in to the constructor into a structure of Value objects. If the return |
41 // value is null, and if |error_code| is non-null, |error_code| will | 64 // value is null, and if |error_code| is non-null, |error_code| will |
42 // contain an integer error code (a JsonParseError in this case). | 65 // contain an integer error code (a JsonParseError in this case). |
43 // If |error_message| is non-null, it will be filled in with a formatted | 66 // If |error_message| is non-null, it will be filled in with a formatted |
44 // error message including the location of the error if appropriate. | 67 // error message including the location of the error if appropriate. |
45 // The caller takes ownership of the returned value. | 68 // The caller takes ownership of the returned value. |
46 base::Value* Deserialize(int* error_code, | 69 base::Value* Deserialize(int* error_code, |
47 std::string* error_message) override; | 70 std::string* error_message) override; |
48 | 71 |
49 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | |
50 bool pretty_print() { return pretty_print_; } | |
51 | |
52 void set_allow_trailing_comma(bool new_value) { | 72 void set_allow_trailing_comma(bool new_value) { |
53 allow_trailing_comma_ = new_value; | 73 allow_trailing_comma_ = new_value; |
54 } | 74 } |
55 | 75 |
56 private: | 76 private: |
57 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | |
58 | |
59 // String for writing. Owned by the caller of the constructor. Will be null if | |
60 // the serializer was initialized with a const string. | |
61 std::string* json_string_; | |
62 // String for reading. Data is owned by the caller of the constructor. If | 77 // String for reading. Data is owned by the caller of the constructor. If |
Matt Giuca
2015/02/16 00:18:27
As above, rewrite this comment. "String for readin
| |
63 // |json_string_| is non-null, this is a view onto |json_string_|. | 78 // |json_string_| is non-null, this is a view onto |json_string_|. |
64 base::StringPiece json_string_readonly_; | 79 base::StringPiece json_string_readonly_; |
Matt Giuca
2015/02/16 00:18:27
Rename this to |json_string_|. There is no need fo
| |
65 bool pretty_print_; // If true, serialization will span multiple lines. | |
66 // If true, deserialization will allow trailing commas. | 80 // If true, deserialization will allow trailing commas. |
67 bool allow_trailing_comma_; | 81 bool allow_trailing_comma_; |
68 | 82 |
69 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 83 DISALLOW_COPY_AND_ASSIGN(JSONStringValueDeserializer); |
70 }; | 84 }; |
71 | 85 |
72 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ | 86 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
73 | |
OLD | NEW |