| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
| 6 | #define BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |
| 7 | |
| Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 8 | #include <memory> |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 9 | #include <string> |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 10 | #include <string_view> |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 11 | |
| 12 | #include "base/base_export.h" |
| Robert Sesek | 115268f2 | 2021-12-14 16:30:29 | [diff] [blame] | 13 | #include "base/json/json_reader.h" |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 15 | #include "base/values.h" |
| 16 | |
| Lei Zhang | 9e75761 | 2025-09-04 20:46:49 | [diff] [blame] | 17 | // Use this class to work with code that takes `base::ValueSerializer`. |
| 18 | // To just serialize a JSON string, use base/json/json_writer.h instead. |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 19 | class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { |
| 20 | public: |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 21 | // |json_string| is the string that will be the destination of the |
| 22 | // serialization. The caller of the constructor retains ownership of the |
| 23 | // string. |json_string| must not be null. |
| mgiuca | 4ea5490 | 2015-02-05 01:22:21 | [diff] [blame] | 24 | explicit JSONStringValueSerializer(std::string* json_string); |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 25 | |
| Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 26 | JSONStringValueSerializer(const JSONStringValueSerializer&) = delete; |
| 27 | JSONStringValueSerializer& operator=(const JSONStringValueSerializer&) = |
| 28 | delete; |
| 29 | |
| dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 30 | ~JSONStringValueSerializer() override; |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 31 | |
| 32 | // Attempt to serialize the data structure represented by Value into |
| 33 | // JSON. If the return value is true, the result will have been written |
| 34 | // into the string passed into the constructor. |
| Daniel Cheng | 8ac305b | 2022-02-17 00:05:11 | [diff] [blame] | 35 | bool Serialize(base::ValueView root) override; |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 36 | |
| 37 | // Equivalent to Serialize(root) except binary values are omitted from the |
| 38 | // output. |
| Daniel Cheng | 8ac305b | 2022-02-17 00:05:11 | [diff] [blame] | 39 | bool SerializeAndOmitBinaryValues(base::ValueView root); |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 40 | |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 41 | void set_pretty_print(bool new_value) { pretty_print_ = new_value; } |
| 42 | bool pretty_print() { return pretty_print_; } |
| 43 | |
| 44 | private: |
| Daniel Cheng | 8ac305b | 2022-02-17 00:05:11 | [diff] [blame] | 45 | bool SerializeInternal(base::ValueView root, bool omit_binary_values); |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 46 | |
| 47 | // Owned by the caller of the constructor. |
| Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 48 | raw_ptr<std::string> json_string_; |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 49 | bool pretty_print_; // If true, serialization will span multiple lines. |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 50 | }; |
| 51 | |
| Lei Zhang | 9e75761 | 2025-09-04 20:46:49 | [diff] [blame] | 52 | // Use this class to work with code that takes `base::ValueDeserializer`. |
| 53 | // To just deserialize a JSON string, use base/json/json_reader.h instead. |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 54 | class BASE_EXPORT JSONStringValueDeserializer : public base::ValueDeserializer { |
| 55 | public: |
| 56 | // This retains a reference to the contents of |json_string|, so the data |
| sky | cc7f72d | 2016-11-11 01:12:37 | [diff] [blame] | 57 | // must outlive the JSONStringValueDeserializer. |options| is a bitmask of |
| 58 | // JSONParserOptions. |
| Robert Sesek | 115268f2 | 2021-12-14 16:30:29 | [diff] [blame] | 59 | explicit JSONStringValueDeserializer( |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 60 | std::string_view json_string, |
| Robert Sesek | 115268f2 | 2021-12-14 16:30:29 | [diff] [blame] | 61 | int options = base::JSON_PARSE_CHROMIUM_EXTENSIONS); |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 62 | |
| Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 63 | JSONStringValueDeserializer(const JSONStringValueDeserializer&) = delete; |
| 64 | JSONStringValueDeserializer& operator=(const JSONStringValueDeserializer&) = |
| 65 | delete; |
| 66 | |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 67 | ~JSONStringValueDeserializer() override; |
| 68 | |
| Caitlin Fischer | aac06dc | 2021-12-17 00:21:32 | [diff] [blame] | 69 | // Attempts to deserialize |json_string_| into a structure of Value objects. |
| 70 | // If the return value is null, then |
| 71 | // (1) |error_code| will be filled with an integer error code |
| 72 | // (base::ValueDeserializer::kErrorCodeInvalidFormat) if a non-null |
| 73 | // |error_code| was given. |
| 74 | // (2) |error_message| will be filled with a formatted error message, |
| 75 | // including the location of the error (if appropriate), if a non-null |
| 76 | // |error_message| was given. |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 77 | // The caller takes ownership of the returned value. |
| dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 78 | std::unique_ptr<base::Value> Deserialize(int* error_code, |
| 79 | std::string* error_message) override; |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 80 | |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 81 | private: |
| prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 82 | // Data is owned by the caller of the constructor. |
| Helmut Januschka | 4e477ef4 | 2024-02-27 19:26:45 | [diff] [blame] | 83 | std::string_view json_string_; |
| sky | cc7f72d | 2016-11-11 01:12:37 | [diff] [blame] | 84 | const int options_; |
| [email protected] | 2055349 | 2013-02-14 02:06:52 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ |