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