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/values.h" | 14 #include "base/values.h" |
14 | 15 |
15 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { | 16 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { |
16 public: | 17 public: |
17 // |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 |
18 // or the destination of the serialization. The caller of the constructor | 19 // or the destination of the serialization. The caller of the constructor |
19 // retains ownership of the string. |json_string| must not be null. | 20 // retains ownership of the string. |json_string| must not be null. |
20 explicit JSONStringValueSerializer(std::string* json_string); | 21 explicit JSONStringValueSerializer(std::string* json_string); |
21 | 22 |
22 // This version allows initialization with a const string reference for | 23 // This version allows initialization with a StringPiece for deserialization |
23 // deserialization only. Retains a reference to |json_string|, so the string | 24 // only. Retains a reference to the contents of |json_string|, so the data |
24 // argument must outlive the JSONStringValueSerializer. | 25 // must outlive the JSONStringValueSerializer. |
25 explicit JSONStringValueSerializer(const std::string& json_string); | 26 explicit JSONStringValueSerializer(const base::StringPiece& json_string); |
26 | 27 |
27 ~JSONStringValueSerializer() override; | 28 ~JSONStringValueSerializer() override; |
28 | 29 |
29 // Attempt to serialize the data structure represented by Value into | 30 // Attempt to serialize the data structure represented by Value into |
30 // JSON. If the return value is true, the result will have been written | 31 // JSON. If the return value is true, the result will have been written |
31 // into the string passed into the constructor. | 32 // into the string passed into the constructor. |
32 bool Serialize(const base::Value& root) override; | 33 bool Serialize(const base::Value& root) override; |
33 | 34 |
34 // Equivalent to Serialize(root) except binary values are omitted from the | 35 // Equivalent to Serialize(root) except binary values are omitted from the |
35 // output. | 36 // output. |
(...skipping 12 matching lines...) Expand all Loading... |
48 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } | 49 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
49 bool pretty_print() { return pretty_print_; } | 50 bool pretty_print() { return pretty_print_; } |
50 | 51 |
51 void set_allow_trailing_comma(bool new_value) { | 52 void set_allow_trailing_comma(bool new_value) { |
52 allow_trailing_comma_ = new_value; | 53 allow_trailing_comma_ = new_value; |
53 } | 54 } |
54 | 55 |
55 private: | 56 private: |
56 bool SerializeInternal(const base::Value& root, bool omit_binary_values); | 57 bool SerializeInternal(const base::Value& root, bool omit_binary_values); |
57 | 58 |
58 std::string* json_string_; // Not null. | 59 // String for writing. Owned by the caller of the constructor. Will be null if |
59 bool initialized_with_const_string_; | 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 |
| 63 // |json_string_| is non-null, this is a view onto |json_string_|. |
| 64 base::StringPiece json_string_readonly_; |
60 bool pretty_print_; // If true, serialization will span multiple lines. | 65 bool pretty_print_; // If true, serialization will span multiple lines. |
61 // If true, deserialization will allow trailing commas. | 66 // If true, deserialization will allow trailing commas. |
62 bool allow_trailing_comma_; | 67 bool allow_trailing_comma_; |
63 | 68 |
64 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); | 69 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer); |
65 }; | 70 }; |
66 | 71 |
67 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ | 72 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
68 | 73 |
OLD | NEW |