| 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/json_reader.h" | 8 #include "base/json_reader.h" |
| 8 #include "base/json_writer.h" | 9 #include "base/json_writer.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "chrome/common/logging_chrome.h" | 11 #include "chrome/common/logging_chrome.h" |
| 11 | 12 |
| 12 JSONStringValueSerializer::~JSONStringValueSerializer() {} | 13 JSONStringValueSerializer::~JSONStringValueSerializer() {} |
| 13 | 14 |
| 14 bool JSONStringValueSerializer::Serialize(const Value& root) { | 15 bool JSONStringValueSerializer::Serialize(const Value& root) { |
| 15 if (!json_string_ || initialized_with_const_string_) | 16 if (!json_string_ || initialized_with_const_string_) |
| 16 return false; | 17 return false; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 30 /******* File Serializer *******/ | 31 /******* File Serializer *******/ |
| 31 | 32 |
| 32 bool JSONFileValueSerializer::Serialize(const Value& root) { | 33 bool JSONFileValueSerializer::Serialize(const Value& root) { |
| 33 std::string json_string; | 34 std::string json_string; |
| 34 JSONStringValueSerializer serializer(&json_string); | 35 JSONStringValueSerializer serializer(&json_string); |
| 35 serializer.set_pretty_print(true); | 36 serializer.set_pretty_print(true); |
| 36 bool result = serializer.Serialize(root); | 37 bool result = serializer.Serialize(root); |
| 37 if (!result) | 38 if (!result) |
| 38 return false; | 39 return false; |
| 39 | 40 |
| 40 FILE* file = NULL; | 41 return file_util::WriteFile(json_file_path_, json_string.data(), |
| 41 _wfopen_s(&file, json_file_path_.c_str(), L"wb"); | 42 json_string.size()); |
| 42 if (!file) | |
| 43 return false; | |
| 44 | |
| 45 size_t amount_written = | |
| 46 fwrite(json_string.data(), 1, json_string.size(), file); | |
| 47 fclose(file); | |
| 48 | |
| 49 return amount_written == json_string.size(); | |
| 50 } | 43 } |
| 51 | 44 |
| 52 bool JSONFileValueSerializer::Deserialize(Value** root) { | 45 bool JSONFileValueSerializer::Deserialize(Value** root) { |
| 53 FILE* file = NULL; | 46 std::string json_string; |
| 54 _wfopen_s(&file, json_file_path_.c_str(), L"rb"); | 47 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { |
| 55 if (!file) | |
| 56 return false; | 48 return false; |
| 57 | |
| 58 fseek(file, 0, SEEK_END); | |
| 59 size_t file_size = ftell(file); | |
| 60 rewind(file); | |
| 61 | |
| 62 bool result = false; | |
| 63 std::string json_string; | |
| 64 size_t chars_read = fread( | |
| 65 // WriteInto assumes the last character is a null, and it's not in this | |
| 66 // case, so we need to add 1 to our size to ensure the last character | |
| 67 // doesn't get cut off. | |
| 68 WriteInto(&json_string, file_size + 1), 1, file_size, file); | |
| 69 if (chars_read == file_size) { | |
| 70 JSONStringValueSerializer serializer(json_string); | |
| 71 result = serializer.Deserialize(root); | |
| 72 } | 49 } |
| 73 | 50 JSONStringValueSerializer serializer(json_string); |
| 74 fclose(file); | 51 return serializer.Deserialize(root); |
| 75 return result; | |
| 76 } | 52 } |
| 77 | 53 |
| OLD | NEW |