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

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

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_value_serializer.h ('k') | base/json/json_value_serializer_unittest.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 #include "base/json/json_value_serializer.h"
6
7 #include "base/file_util.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/string_util.h"
11
12 const char* JSONFileValueSerializer::kAccessDenied = "Access denied.";
13 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file.";
14 const char* JSONFileValueSerializer::kFileLocked = "File locked.";
15 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist.";
16
17 JSONStringValueSerializer::~JSONStringValueSerializer() {}
18
19 bool JSONStringValueSerializer::Serialize(const Value& root) {
20 return SerializeInternal(root, false);
21 }
22
23 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues(
24 const Value& root) {
25 return SerializeInternal(root, true);
26 }
27
28 bool JSONStringValueSerializer::SerializeInternal(const Value& root,
29 bool omit_binary_values) {
30 if (!json_string_ || initialized_with_const_string_)
31 return false;
32
33 base::JSONWriter::WriteWithOptions(
34 &root,
35 pretty_print_,
36 omit_binary_values ? base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES : 0,
37 json_string_);
38 return true;
39 }
40
41 Value* JSONStringValueSerializer::Deserialize(int* error_code,
42 std::string* error_str) {
43 if (!json_string_)
44 return NULL;
45
46 return base::JSONReader::ReadAndReturnError(*json_string_,
47 allow_trailing_comma_,
48 error_code,
49 error_str);
50 }
51
52 /******* File Serializer *******/
53
54 bool JSONFileValueSerializer::Serialize(const Value& root) {
55 return SerializeInternal(root, false);
56 }
57
58 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(const Value& root) {
59 return SerializeInternal(root, true);
60 }
61
62 bool JSONFileValueSerializer::SerializeInternal(const Value& root,
63 bool omit_binary_values) {
64 std::string json_string;
65 JSONStringValueSerializer serializer(&json_string);
66 serializer.set_pretty_print(true);
67 bool result = omit_binary_values ?
68 serializer.SerializeAndOmitBinaryValues(root) :
69 serializer.Serialize(root);
70 if (!result)
71 return false;
72
73 int data_size = static_cast<int>(json_string.size());
74 if (file_util::WriteFile(json_file_path_,
75 json_string.data(),
76 data_size) != data_size)
77 return false;
78
79 return true;
80 }
81
82 int JSONFileValueSerializer::ReadFileToString(std::string* json_string) {
83 DCHECK(json_string);
84 if (!file_util::ReadFileToString(json_file_path_, json_string)) {
85 #if defined(OS_WIN)
86 int error = ::GetLastError();
87 if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
88 return JSON_FILE_LOCKED;
89 } else if (error == ERROR_ACCESS_DENIED) {
90 return JSON_ACCESS_DENIED;
91 }
92 #endif
93 if (!file_util::PathExists(json_file_path_))
94 return JSON_NO_SUCH_FILE;
95 else
96 return JSON_CANNOT_READ_FILE;
97 }
98 return JSON_NO_ERROR;
99 }
100
101 const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) {
102 switch (error_code) {
103 case JSON_NO_ERROR:
104 return "";
105 case JSON_ACCESS_DENIED:
106 return kAccessDenied;
107 case JSON_CANNOT_READ_FILE:
108 return kCannotReadFile;
109 case JSON_FILE_LOCKED:
110 return kFileLocked;
111 case JSON_NO_SUCH_FILE:
112 return kNoSuchFile;
113 default:
114 NOTREACHED();
115 return "";
116 }
117 }
118
119 Value* JSONFileValueSerializer::Deserialize(int* error_code,
120 std::string* error_str) {
121 std::string json_string;
122 int error = ReadFileToString(&json_string);
123 if (error != JSON_NO_ERROR) {
124 if (error_code)
125 *error_code = error;
126 if (error_str)
127 *error_str = GetErrorMessageForCode(error);
128 return NULL;
129 }
130
131 JSONStringValueSerializer serializer(json_string);
132 serializer.set_allow_trailing_comma(allow_trailing_comma_);
133 return serializer.Deserialize(error_code, error_str);
134 }
OLDNEW
« no previous file with comments | « base/json/json_value_serializer.h ('k') | base/json/json_value_serializer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698