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/json_reader.h" |
9 #include "base/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 JSONStringValueSerializer::~JSONStringValueSerializer() {} | 12 JSONStringValueSerializer::~JSONStringValueSerializer() {} |
13 | 13 |
14 bool JSONStringValueSerializer::Serialize(const Value& root) { | 14 bool JSONStringValueSerializer::Serialize(const Value& root) { |
15 if (!json_string_ || initialized_with_const_string_) | 15 if (!json_string_ || initialized_with_const_string_) |
16 return false; | 16 return false; |
17 | 17 |
18 JSONWriter::Write(&root, pretty_print_, json_string_); | 18 base::JSONWriter::Write(&root, pretty_print_, json_string_); |
19 | |
20 return true; | 19 return true; |
21 } | 20 } |
22 | 21 |
23 Value* JSONStringValueSerializer::Deserialize(std::string* error_message) { | 22 Value* JSONStringValueSerializer::Deserialize(std::string* error_message) { |
24 if (!json_string_) | 23 if (!json_string_) |
25 return NULL; | 24 return NULL; |
26 | 25 |
27 return JSONReader::ReadAndReturnError(*json_string_, allow_trailing_comma_, | 26 return base::JSONReader::ReadAndReturnError(*json_string_, |
28 error_message); | 27 allow_trailing_comma_, |
| 28 error_message); |
29 } | 29 } |
30 | 30 |
31 /******* File Serializer *******/ | 31 /******* File Serializer *******/ |
32 | 32 |
33 bool JSONFileValueSerializer::Serialize(const Value& root) { | 33 bool JSONFileValueSerializer::Serialize(const Value& root) { |
34 std::string json_string; | 34 std::string json_string; |
35 JSONStringValueSerializer serializer(&json_string); | 35 JSONStringValueSerializer serializer(&json_string); |
36 serializer.set_pretty_print(true); | 36 serializer.set_pretty_print(true); |
37 bool result = serializer.Serialize(root); | 37 bool result = serializer.Serialize(root); |
38 if (!result) | 38 if (!result) |
39 return false; | 39 return false; |
40 | 40 |
41 int data_size = static_cast<int>(json_string.size()); | 41 int data_size = static_cast<int>(json_string.size()); |
42 if (file_util::WriteFile(json_file_path_, | 42 if (file_util::WriteFile(json_file_path_, |
43 json_string.data(), | 43 json_string.data(), |
44 data_size) != data_size) | 44 data_size) != data_size) |
45 return false; | 45 return false; |
46 | 46 |
47 return true; | 47 return true; |
48 } | 48 } |
49 | 49 |
50 Value* JSONFileValueSerializer::Deserialize(std::string* error_message) { | 50 Value* JSONFileValueSerializer::Deserialize(std::string* error_message) { |
51 std::string json_string; | 51 std::string json_string; |
52 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { | 52 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { |
53 return NULL; | 53 return NULL; |
54 } | 54 } |
55 JSONStringValueSerializer serializer(json_string); | 55 JSONStringValueSerializer serializer(json_string); |
56 return serializer.Deserialize(error_message); | 56 return serializer.Deserialize(error_message); |
57 } | 57 } |
OLD | NEW |