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