Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 9cfa1ba88144596eeae5244d0ee2b70de5d03b29 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]20553492013-02-14 02:06:522// 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 Syed22f60eeb2021-08-26 05:16:268#include <memory>
[email protected]20553492013-02-14 02:06:529#include <string>
Helmut Januschka4e477ef42024-02-27 19:26:4510#include <string_view>
[email protected]20553492013-02-14 02:06:5211
12#include "base/base_export.h"
Robert Sesek115268f22021-12-14 16:30:2913#include "base/json/json_reader.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
[email protected]20553492013-02-14 02:06:5215#include "base/values.h"
16
Lei Zhang9e757612025-09-04 20:46:4917// 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]20553492013-02-14 02:06:5219class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer {
20 public:
prashhir54a994502015-03-05 09:30:5721 // |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.
mgiuca4ea54902015-02-05 01:22:2124 explicit JSONStringValueSerializer(std::string* json_string);
[email protected]20553492013-02-14 02:06:5225
Peter Boström7319bbd2021-09-15 22:59:3826 JSONStringValueSerializer(const JSONStringValueSerializer&) = delete;
27 JSONStringValueSerializer& operator=(const JSONStringValueSerializer&) =
28 delete;
29
dcheng56488182014-10-21 10:54:5130 ~JSONStringValueSerializer() override;
[email protected]20553492013-02-14 02:06:5231
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 Cheng8ac305b2022-02-17 00:05:1135 bool Serialize(base::ValueView root) override;
[email protected]20553492013-02-14 02:06:5236
37 // Equivalent to Serialize(root) except binary values are omitted from the
38 // output.
Daniel Cheng8ac305b2022-02-17 00:05:1139 bool SerializeAndOmitBinaryValues(base::ValueView root);
[email protected]20553492013-02-14 02:06:5240
prashhir54a994502015-03-05 09:30:5741 void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
42 bool pretty_print() { return pretty_print_; }
43
44 private:
Daniel Cheng8ac305b2022-02-17 00:05:1145 bool SerializeInternal(base::ValueView root, bool omit_binary_values);
prashhir54a994502015-03-05 09:30:5746
47 // Owned by the caller of the constructor.
Keishi Hattori0e45c022021-11-27 09:25:5248 raw_ptr<std::string> json_string_;
prashhir54a994502015-03-05 09:30:5749 bool pretty_print_; // If true, serialization will span multiple lines.
prashhir54a994502015-03-05 09:30:5750};
51
Lei Zhang9e757612025-09-04 20:46:4952// 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.
prashhir54a994502015-03-05 09:30:5754class BASE_EXPORT JSONStringValueDeserializer : public base::ValueDeserializer {
55 public:
56 // This retains a reference to the contents of |json_string|, so the data
skycc7f72d2016-11-11 01:12:3757 // must outlive the JSONStringValueDeserializer. |options| is a bitmask of
58 // JSONParserOptions.
Robert Sesek115268f22021-12-14 16:30:2959 explicit JSONStringValueDeserializer(
Helmut Januschka4e477ef42024-02-27 19:26:4560 std::string_view json_string,
Robert Sesek115268f22021-12-14 16:30:2961 int options = base::JSON_PARSE_CHROMIUM_EXTENSIONS);
prashhir54a994502015-03-05 09:30:5762
Peter Boström7319bbd2021-09-15 22:59:3863 JSONStringValueDeserializer(const JSONStringValueDeserializer&) = delete;
64 JSONStringValueDeserializer& operator=(const JSONStringValueDeserializer&) =
65 delete;
66
prashhir54a994502015-03-05 09:30:5767 ~JSONStringValueDeserializer() override;
68
Caitlin Fischeraac06dc2021-12-17 00:21:3269 // 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]20553492013-02-14 02:06:5277 // The caller takes ownership of the returned value.
dcheng093de9b2016-04-04 21:25:5178 std::unique_ptr<base::Value> Deserialize(int* error_code,
79 std::string* error_message) override;
[email protected]20553492013-02-14 02:06:5280
[email protected]20553492013-02-14 02:06:5281 private:
prashhir54a994502015-03-05 09:30:5782 // Data is owned by the caller of the constructor.
Helmut Januschka4e477ef42024-02-27 19:26:4583 std::string_view json_string_;
skycc7f72d2016-11-11 01:12:3784 const int options_;
[email protected]20553492013-02-14 02:06:5285};
86
87#endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_