Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(866)

Side by Side Diff: base/json/json_value_serializer.h

Issue 9465030: Break two classes defined in json_value_serializer.cc, .h into separate files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/json/json_string_value_serializer.cc ('k') | base/json/json_value_serializer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #ifndef BASE_JSON_JSON_VALUE_SERIALIZER_H_
6 #define BASE_JSON_JSON_VALUE_SERIALIZER_H_
7 #pragma once
8
9 #include <string>
10
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/file_path.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 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),
24 pretty_print_(false),
25 allow_trailing_comma_(false) {
26 }
27
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),
33 pretty_print_(false),
34 allow_trailing_comma_(false) {
35 }
36
37 virtual ~JSONStringValueSerializer();
38
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;
43
44 // Equivalent to Serialize(root) except binary values are omitted from the
45 // output.
46 bool SerializeAndOmitBinaryValues(const Value& root);
47
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;
57
58 void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
59 bool pretty_print() { return pretty_print_; }
60
61 void set_allow_trailing_comma(bool new_value) {
62 allow_trailing_comma_ = new_value;
63 }
64
65 private:
66 bool SerializeInternal(const Value& root, bool omit_binary_values);
67
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_;
73
74 DISALLOW_COPY_AND_ASSIGN(JSONStringValueSerializer);
75 };
76
77 class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
78 public:
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) {}
86
87 virtual ~JSONFileValueSerializer() {}
88
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
92 // the file thread.
93 //
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;
98
99 // Equivalent to Serialize(root) except binary values are omitted from the
100 // output.
101 bool SerializeAndOmitBinaryValues(const Value& root);
102
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;
112
113 // This enum is designed to safely overlap with JSONReader::JsonParseError.
114 enum JsonFileError {
115 JSON_NO_ERROR = 0,
116 JSON_ACCESS_DENIED = 1000,
117 JSON_CANNOT_READ_FILE,
118 JSON_FILE_LOCKED,
119 JSON_NO_SUCH_FILE
120 };
121
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;
127
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);
131
132 void set_allow_trailing_comma(bool new_value) {
133 allow_trailing_comma_ = new_value;
134 }
135
136 private:
137 bool SerializeInternal(const Value& root, bool omit_binary_values);
138
139 FilePath json_file_path_;
140 bool allow_trailing_comma_;
141
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);
145
146 DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
147 };
148
149 #endif // BASE_JSON_JSON_VALUE_SERIALIZER_H_
OLDNEW
« no previous file with comments | « base/json/json_string_value_serializer.cc ('k') | base/json/json_value_serializer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698