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 Serialize(root, false); | |
21 } | |
22 | |
23 bool JSONStringValueSerializer::Serialize(const Value& root, | |
24 bool ignore_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::Write( |
29 &root, | |
willchan no longer on Chromium
2011/11/10 18:52:27
Should be 4 space indent. See the example in http:
Eric Dingle
2011/11/10 23:19:57
Done.
| |
30 pretty_print_, | |
31 ignore_binary_values ? base::JSONWriter::OPTIONS_IGNORE_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 Serialize(root, false); | |
51 } | |
52 | |
53 bool JSONFileValueSerializer::Serialize(const Value& root, | |
54 bool ignore_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.Serialize(root, ignore_binary_values); |
45 if (!result) | 59 if (!result) |
46 return false; | 60 return false; |
47 | 61 |
48 int data_size = static_cast<int>(json_string.size()); | 62 int data_size = static_cast<int>(json_string.size()); |
49 if (file_util::WriteFile(json_file_path_, | 63 if (file_util::WriteFile(json_file_path_, |
50 json_string.data(), | 64 json_string.data(), |
51 data_size) != data_size) | 65 data_size) != data_size) |
52 return false; | 66 return false; |
53 | 67 |
54 return true; | 68 return true; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 if (error_code) | 113 if (error_code) |
100 *error_code = error; | 114 *error_code = error; |
101 if (error_str) | 115 if (error_str) |
102 *error_str = GetErrorMessageForCode(error); | 116 *error_str = GetErrorMessageForCode(error); |
103 return NULL; | 117 return NULL; |
104 } | 118 } |
105 | 119 |
106 JSONStringValueSerializer serializer(json_string); | 120 JSONStringValueSerializer serializer(json_string); |
107 return serializer.Deserialize(error_code, error_str); | 121 return serializer.Deserialize(error_code, error_str); |
108 } | 122 } |
OLD | NEW |