OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/common/json_value_serializer.h" | 5 #include "chrome/common/json_value_serializer.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/json_reader.h" | 8 #include "base/json_reader.h" |
9 #include "base/json_writer.h" | 9 #include "base/json_writer.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "chrome/common/logging_chrome.h" | 11 #include "chrome/common/logging_chrome.h" |
12 | 12 |
13 JSONStringValueSerializer::~JSONStringValueSerializer() {} | 13 JSONStringValueSerializer::~JSONStringValueSerializer() {} |
14 | 14 |
15 bool JSONStringValueSerializer::Serialize(const Value& root) { | 15 bool JSONStringValueSerializer::Serialize(const Value& root) { |
16 if (!json_string_ || initialized_with_const_string_) | 16 if (!json_string_ || initialized_with_const_string_) |
17 return false; | 17 return false; |
18 | 18 |
19 JSONWriter::Write(&root, pretty_print_, json_string_); | 19 JSONWriter::Write(&root, pretty_print_, json_string_); |
20 | 20 |
21 return true; | 21 return true; |
22 } | 22 } |
23 | 23 |
24 bool JSONStringValueSerializer::Deserialize(Value** root) { | 24 bool JSONStringValueSerializer::Deserialize(Value** root, |
| 25 std::string* error_message) { |
25 if (!json_string_) | 26 if (!json_string_) |
26 return false; | 27 return false; |
27 | 28 |
28 return JSONReader::Read(*json_string_, root, allow_trailing_comma_); | 29 return JSONReader::ReadAndReturnError(*json_string_, root, |
| 30 allow_trailing_comma_, |
| 31 error_message); |
29 } | 32 } |
30 | 33 |
31 /******* File Serializer *******/ | 34 /******* File Serializer *******/ |
32 | 35 |
33 bool JSONFileValueSerializer::Serialize(const Value& root) { | 36 bool JSONFileValueSerializer::Serialize(const Value& root) { |
34 std::string json_string; | 37 std::string json_string; |
35 JSONStringValueSerializer serializer(&json_string); | 38 JSONStringValueSerializer serializer(&json_string); |
36 serializer.set_pretty_print(true); | 39 serializer.set_pretty_print(true); |
37 bool result = serializer.Serialize(root); | 40 bool result = serializer.Serialize(root); |
38 if (!result) | 41 if (!result) |
39 return false; | 42 return false; |
40 | 43 |
41 int data_size = static_cast<int>(json_string.size()); | 44 int data_size = static_cast<int>(json_string.size()); |
42 if (file_util::WriteFile(json_file_path_, | 45 if (file_util::WriteFile(json_file_path_, |
43 json_string.data(), | 46 json_string.data(), |
44 data_size) != data_size) | 47 data_size) != data_size) |
45 return false; | 48 return false; |
46 | 49 |
47 return true; | 50 return true; |
48 } | 51 } |
49 | 52 |
50 bool JSONFileValueSerializer::Deserialize(Value** root) { | 53 bool JSONFileValueSerializer::Deserialize(Value** root, |
| 54 std::string* error_message) { |
51 std::string json_string; | 55 std::string json_string; |
52 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { | 56 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { |
53 return false; | 57 return false; |
54 } | 58 } |
55 JSONStringValueSerializer serializer(json_string); | 59 JSONStringValueSerializer serializer(json_string); |
56 return serializer.Deserialize(root); | 60 return serializer.Deserialize(root, error_message); |
57 } | 61 } |
58 | 62 |
OLD | NEW |