OLD | NEW |
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 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) { |
20 if (!json_string_ || initialized_with_const_string_) | 30 if (!json_string_ || initialized_with_const_string_) |
21 return false; | 31 return false; |
22 | 32 |
23 base::JSONWriter::Write(&root, pretty_print_, json_string_); | 33 base::JSONWriter::WriteWithOptions( |
| 34 &root, |
| 35 pretty_print_, |
| 36 omit_binary_values ? base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES : 0, |
| 37 json_string_); |
24 return true; | 38 return true; |
25 } | 39 } |
26 | 40 |
27 Value* JSONStringValueSerializer::Deserialize(int* error_code, | 41 Value* JSONStringValueSerializer::Deserialize(int* error_code, |
28 std::string* error_str) { | 42 std::string* error_str) { |
29 if (!json_string_) | 43 if (!json_string_) |
30 return NULL; | 44 return NULL; |
31 | 45 |
32 return base::JSONReader::ReadAndReturnError(*json_string_, | 46 return base::JSONReader::ReadAndReturnError(*json_string_, |
33 allow_trailing_comma_, | 47 allow_trailing_comma_, |
34 error_code, | 48 error_code, |
35 error_str); | 49 error_str); |
36 } | 50 } |
37 | 51 |
38 /******* File Serializer *******/ | 52 /******* File Serializer *******/ |
39 | 53 |
40 bool JSONFileValueSerializer::Serialize(const Value& root) { | 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) { |
41 std::string json_string; | 64 std::string json_string; |
42 JSONStringValueSerializer serializer(&json_string); | 65 JSONStringValueSerializer serializer(&json_string); |
43 serializer.set_pretty_print(true); | 66 serializer.set_pretty_print(true); |
44 bool result = serializer.Serialize(root); | 67 bool result = omit_binary_values ? |
| 68 serializer.SerializeAndOmitBinaryValues(root) : |
| 69 serializer.Serialize(root); |
45 if (!result) | 70 if (!result) |
46 return false; | 71 return false; |
47 | 72 |
48 int data_size = static_cast<int>(json_string.size()); | 73 int data_size = static_cast<int>(json_string.size()); |
49 if (file_util::WriteFile(json_file_path_, | 74 if (file_util::WriteFile(json_file_path_, |
50 json_string.data(), | 75 json_string.data(), |
51 data_size) != data_size) | 76 data_size) != data_size) |
52 return false; | 77 return false; |
53 | 78 |
54 return true; | 79 return true; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 if (error_code) | 124 if (error_code) |
100 *error_code = error; | 125 *error_code = error; |
101 if (error_str) | 126 if (error_str) |
102 *error_str = GetErrorMessageForCode(error); | 127 *error_str = GetErrorMessageForCode(error); |
103 return NULL; | 128 return NULL; |
104 } | 129 } |
105 | 130 |
106 JSONStringValueSerializer serializer(json_string); | 131 JSONStringValueSerializer serializer(json_string); |
107 return serializer.Deserialize(error_code, error_str); | 132 return serializer.Deserialize(error_code, error_str); |
108 } | 133 } |
OLD | NEW |