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 Value* JSONStringValueSerializer::Deserialize(std::string* error_message) { |
25 std::string* error_message) { | |
26 if (!json_string_) | 25 if (!json_string_) |
27 return false; | 26 return false; |
eroman
2009/01/03 04:39:05
return NULL
| |
28 | 27 |
29 return JSONReader::ReadAndReturnError(*json_string_, root, | 28 return JSONReader::ReadAndReturnError(*json_string_, allow_trailing_comma_, |
30 allow_trailing_comma_, | |
31 error_message); | 29 error_message); |
32 } | 30 } |
33 | 31 |
34 /******* File Serializer *******/ | 32 /******* File Serializer *******/ |
35 | 33 |
36 bool JSONFileValueSerializer::Serialize(const Value& root) { | 34 bool JSONFileValueSerializer::Serialize(const Value& root) { |
37 std::string json_string; | 35 std::string json_string; |
38 JSONStringValueSerializer serializer(&json_string); | 36 JSONStringValueSerializer serializer(&json_string); |
39 serializer.set_pretty_print(true); | 37 serializer.set_pretty_print(true); |
40 bool result = serializer.Serialize(root); | 38 bool result = serializer.Serialize(root); |
41 if (!result) | 39 if (!result) |
42 return false; | 40 return false; |
43 | 41 |
44 int data_size = static_cast<int>(json_string.size()); | 42 int data_size = static_cast<int>(json_string.size()); |
45 if (file_util::WriteFile(json_file_path_, | 43 if (file_util::WriteFile(json_file_path_, |
46 json_string.data(), | 44 json_string.data(), |
47 data_size) != data_size) | 45 data_size) != data_size) |
48 return false; | 46 return false; |
49 | 47 |
50 return true; | 48 return true; |
51 } | 49 } |
52 | 50 |
53 bool JSONFileValueSerializer::Deserialize(Value** root, | 51 Value* JSONFileValueSerializer::Deserialize(std::string* error_message) { |
54 std::string* error_message) { | |
55 std::string json_string; | 52 std::string json_string; |
56 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { | 53 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { |
57 return false; | 54 return false; |
eroman
2009/01/03 04:39:05
return NULL
| |
58 } | 55 } |
59 JSONStringValueSerializer serializer(json_string); | 56 JSONStringValueSerializer serializer(json_string); |
60 return serializer.Deserialize(root, error_message); | 57 return serializer.Deserialize(error_message); |
61 } | 58 } |
62 | 59 |
OLD | NEW |