1 // Copyright (c) 2011 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.
5 #ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_
6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/file_path.h"
14 #include "base/values.h"
16 class BASE_EXPORT JSONStringValueSerializer
: public base::ValueSerializer
{
18 // json_string is the string that will be source of the deserialization
19 // or the destination of the serialization. The caller of the constructor
20 // retains ownership of the string.
21 explicit JSONStringValueSerializer(std::string
* json_string
)
22 : json_string_(json_string
),
23 initialized_with_const_string_(false),
25 allow_trailing_comma_(false) {
28 // This version allows initialization with a const string reference for
29 // deserialization only.
30 explicit JSONStringValueSerializer(const std::string
& json_string
)
31 : json_string_(&const_cast<std::string
&>(json_string
)),
32 initialized_with_const_string_(true),
34 allow_trailing_comma_(false) {
37 virtual ~JSONStringValueSerializer();
39 // Attempt to serialize the data structure represented by Value into
40 // JSON. If the return value is true, the result will have been written
41 // into the string passed into the constructor.
42 virtual bool Serialize(const Value
& root
) OVERRIDE
;
44 // Equivalent to Serialize(root) except binary values are omitted from the
46 bool SerializeAndOmitBinaryValues(const Value
& root
);
48 // Attempt to deserialize the data structure encoded in the string passed
49 // in to the constructor into a structure of Value objects. If the return
50 // value is NULL, and if |error_code| is non-null, |error_code| will
51 // contain an integer error code (either JsonFileError or JsonParseError).
52 // If |error_message| is non-null, it will be filled in with a formatted
53 // error message including the location of the error if appropriate.
54 // The caller takes ownership of the returned value.
55 virtual Value
* Deserialize(int* error_code
,
56 std::string
* error_message
) OVERRIDE
;
58 void set_pretty_print(bool new_value
) { pretty_print_
= new_value
; }
59 bool pretty_print() { return pretty_print_
; }
61 void set_allow_trailing_comma(bool new_value
) {
62 allow_trailing_comma_
= new_value
;
66 bool SerializeInternal(const Value
& root
, bool omit_binary_values
);
68 std::string
* json_string_
;
69 bool initialized_with_const_string_
;
70 bool pretty_print_
; // If true, serialization will span multiple lines.
71 // If true, deserialization will allow trailing commas.
72 bool allow_trailing_comma_
;
74 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer
);
77 class BASE_EXPORT JSONFileValueSerializer
: public base::ValueSerializer
{
79 // json_file_patch is the path of a file that will be source of the
80 // deserialization or the destination of the serialization.
81 // When deserializing, the file should exist, but when serializing, the
82 // serializer will attempt to create the file at the specified location.
83 explicit JSONFileValueSerializer(const FilePath
& json_file_path
)
84 : json_file_path_(json_file_path
),
85 allow_trailing_comma_(false) {}
87 virtual ~JSONFileValueSerializer() {}
89 // DO NOT USE except in unit tests to verify the file was written properly.
90 // We should never serialize directly to a file since this will block the
91 // thread. Instead, serialize to a string and write to the file you want on
94 // Attempt to serialize the data structure represented by Value into
95 // JSON. If the return value is true, the result will have been written
96 // into the file whose name was passed into the constructor.
97 virtual bool Serialize(const Value
& root
) OVERRIDE
;
99 // Equivalent to Serialize(root) except binary values are omitted from the
101 bool SerializeAndOmitBinaryValues(const Value
& root
);
103 // Attempt to deserialize the data structure encoded in the file passed
104 // in to the constructor into a structure of Value objects. If the return
105 // value is NULL, and if |error_code| is non-null, |error_code| will
106 // contain an integer error code (either JsonFileError or JsonParseError).
107 // If |error_message| is non-null, it will be filled in with a formatted
108 // error message including the location of the error if appropriate.
109 // The caller takes ownership of the returned value.
110 virtual Value
* Deserialize(int* error_code
,
111 std::string
* error_message
) OVERRIDE
;
113 // This enum is designed to safely overlap with JSONReader::JsonParseError.
116 JSON_ACCESS_DENIED
= 1000,
117 JSON_CANNOT_READ_FILE
,
122 // File-specific error messages that can be returned.
123 static const char* kAccessDenied
;
124 static const char* kCannotReadFile
;
125 static const char* kFileLocked
;
126 static const char* kNoSuchFile
;
128 // Convert an error code into an error message. |error_code| is assumed to
129 // be a JsonFileError.
130 static const char* GetErrorMessageForCode(int error_code
);
132 void set_allow_trailing_comma(bool new_value
) {
133 allow_trailing_comma_
= new_value
;
137 bool SerializeInternal(const Value
& root
, bool omit_binary_values
);
139 FilePath json_file_path_
;
140 bool allow_trailing_comma_
;
142 // A wrapper for file_util::ReadFileToString which returns a non-zero
143 // JsonFileError if there were file errors.
144 int ReadFileToString(std::string
* json_string
);
146 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer
);
149 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_