1 // Copyright 2012 The Chromium Authors 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 8 #include <memory> 9 #include <string> 10 11 #include "base/base_export.h" 12 #include "base/json/json_reader.h" 13 #include "base/memory/raw_ptr.h" 14 #include "base/strings/string_piece.h" 15 #include "base/values.h" 16 17 class BASE_EXPORT JSONStringValueSerializer : public base::ValueSerializer { 18 public: 19 // |json_string| is the string that will be the destination of the 20 // serialization. The caller of the constructor retains ownership of the 21 // string. |json_string| must not be null. 22 explicit JSONStringValueSerializer(std::string* json_string); 23 24 JSONStringValueSerializer(const JSONStringValueSerializer&) = delete; 25 JSONStringValueSerializer& operator=(const JSONStringValueSerializer&) = 26 delete; 27 28 ~JSONStringValueSerializer() override; 29 30 // Attempt to serialize the data structure represented by Value into 31 // JSON. If the return value is true, the result will have been written 32 // into the string passed into the constructor. 33 bool Serialize(base::ValueView root) override; 34 35 // Equivalent to Serialize(root) except binary values are omitted from the 36 // output. 37 bool SerializeAndOmitBinaryValues(base::ValueView root); 38 set_pretty_print(bool new_value)39 void set_pretty_print(bool new_value) { pretty_print_ = new_value; } pretty_print()40 bool pretty_print() { return pretty_print_; } 41 42 private: 43 bool SerializeInternal(base::ValueView root, bool omit_binary_values); 44 45 // Owned by the caller of the constructor. 46 raw_ptr<std::string> json_string_; 47 bool pretty_print_; // If true, serialization will span multiple lines. 48 }; 49 50 class BASE_EXPORT JSONStringValueDeserializer : public base::ValueDeserializer { 51 public: 52 // This retains a reference to the contents of |json_string|, so the data 53 // must outlive the JSONStringValueDeserializer. |options| is a bitmask of 54 // JSONParserOptions. 55 explicit JSONStringValueDeserializer( 56 const base::StringPiece& json_string, 57 int options = base::JSON_PARSE_CHROMIUM_EXTENSIONS); 58 59 JSONStringValueDeserializer(const JSONStringValueDeserializer&) = delete; 60 JSONStringValueDeserializer& operator=(const JSONStringValueDeserializer&) = 61 delete; 62 63 ~JSONStringValueDeserializer() override; 64 65 // Attempts to deserialize |json_string_| into a structure of Value objects. 66 // If the return value is null, then 67 // (1) |error_code| will be filled with an integer error code 68 // (base::ValueDeserializer::kErrorCodeInvalidFormat) if a non-null 69 // |error_code| was given. 70 // (2) |error_message| will be filled with a formatted error message, 71 // including the location of the error (if appropriate), if a non-null 72 // |error_message| was given. 73 // The caller takes ownership of the returned value. 74 std::unique_ptr<base::Value> Deserialize(int* error_code, 75 std::string* error_message) override; 76 77 private: 78 // Data is owned by the caller of the constructor. 79 base::StringPiece json_string_; 80 const int options_; 81 }; 82 83 #endif // BASE_JSON_JSON_STRING_VALUE_SERIALIZER_H_ 84