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