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

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

Issue 8505033: Allow JSONWriter and JSONValueSerializer to omit binary values when instructed to do so. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove extra virtual. Created 9 years, 1 month 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/json/json_value_serializer.h" 5 #include "base/json/json_value_serializer.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 11
12 const char* JSONFileValueSerializer::kAccessDenied = "Access denied."; 12 const char* JSONFileValueSerializer::kAccessDenied = "Access denied.";
13 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file."; 13 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file.";
14 const char* JSONFileValueSerializer::kFileLocked = "File locked."; 14 const char* JSONFileValueSerializer::kFileLocked = "File locked.";
15 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist."; 15 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist.";
16 16
17 JSONStringValueSerializer::~JSONStringValueSerializer() {} 17 JSONStringValueSerializer::~JSONStringValueSerializer() {}
18 18
19 bool JSONStringValueSerializer::Serialize(const Value& root) { 19 bool JSONStringValueSerializer::Serialize(const Value& root) {
20 return SerializeWithOptionalBinaryValues(root, false);
21 }
22
23 bool JSONStringValueSerializer::SerializeWithOptionalBinaryValues(
24 const Value& root, bool omit_binary_values) {
20 if (!json_string_ || initialized_with_const_string_) 25 if (!json_string_ || initialized_with_const_string_)
21 return false; 26 return false;
22 27
23 base::JSONWriter::Write(&root, pretty_print_, json_string_); 28 base::JSONWriter::WriteWithOptions(
29 &root,
30 pretty_print_,
31 omit_binary_values ? base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES : 0,
32 json_string_);
24 return true; 33 return true;
25 } 34 }
26 35
27 Value* JSONStringValueSerializer::Deserialize(int* error_code, 36 Value* JSONStringValueSerializer::Deserialize(int* error_code,
28 std::string* error_str) { 37 std::string* error_str) {
29 if (!json_string_) 38 if (!json_string_)
30 return NULL; 39 return NULL;
31 40
32 return base::JSONReader::ReadAndReturnError(*json_string_, 41 return base::JSONReader::ReadAndReturnError(*json_string_,
33 allow_trailing_comma_, 42 allow_trailing_comma_,
34 error_code, 43 error_code,
35 error_str); 44 error_str);
36 } 45 }
37 46
38 /******* File Serializer *******/ 47 /******* File Serializer *******/
39 48
40 bool JSONFileValueSerializer::Serialize(const Value& root) { 49 bool JSONFileValueSerializer::Serialize(const Value& root) {
50 return SerializeWithOptionalBinaryValues(root, false);
51 }
52
53 bool JSONFileValueSerializer::SerializeWithOptionalBinaryValues(
54 const Value& root, bool omit_binary_values) {
41 std::string json_string; 55 std::string json_string;
42 JSONStringValueSerializer serializer(&json_string); 56 JSONStringValueSerializer serializer(&json_string);
43 serializer.set_pretty_print(true); 57 serializer.set_pretty_print(true);
44 bool result = serializer.Serialize(root); 58 bool result = serializer.SerializeWithOptionalBinaryValues(
59 root, omit_binary_values);
45 if (!result) 60 if (!result)
46 return false; 61 return false;
47 62
48 int data_size = static_cast<int>(json_string.size()); 63 int data_size = static_cast<int>(json_string.size());
49 if (file_util::WriteFile(json_file_path_, 64 if (file_util::WriteFile(json_file_path_,
50 json_string.data(), 65 json_string.data(),
51 data_size) != data_size) 66 data_size) != data_size)
52 return false; 67 return false;
53 68
54 return true; 69 return true;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 if (error_code) 114 if (error_code)
100 *error_code = error; 115 *error_code = error;
101 if (error_str) 116 if (error_str)
102 *error_str = GetErrorMessageForCode(error); 117 *error_str = GetErrorMessageForCode(error);
103 return NULL; 118 return NULL;
104 } 119 }
105 120
106 JSONStringValueSerializer serializer(json_string); 121 JSONStringValueSerializer serializer(json_string);
107 return serializer.Deserialize(error_code, error_str); 122 return serializer.Deserialize(error_code, error_str);
108 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698