| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/json/json_file_value_serializer.h" | |
| 6 | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/json/json_string_value_serializer.h" | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 using base::FilePath; | |
| 12 | |
| 13 const char JSONFileValueDeserializer::kAccessDenied[] = "Access denied."; | |
| 14 const char JSONFileValueDeserializer::kCannotReadFile[] = "Can't read file."; | |
| 15 const char JSONFileValueDeserializer::kFileLocked[] = "File locked."; | |
| 16 const char JSONFileValueDeserializer::kNoSuchFile[] = "File doesn't exist."; | |
| 17 | |
| 18 JSONFileValueSerializer::JSONFileValueSerializer( | |
| 19 const base::FilePath& json_file_path) | |
| 20 : json_file_path_(json_file_path) { | |
| 21 } | |
| 22 | |
| 23 JSONFileValueSerializer::~JSONFileValueSerializer() { | |
| 24 } | |
| 25 | |
| 26 bool JSONFileValueSerializer::Serialize(const base::Value& root) { | |
| 27 return SerializeInternal(root, false); | |
| 28 } | |
| 29 | |
| 30 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues( | |
| 31 const base::Value& root) { | |
| 32 return SerializeInternal(root, true); | |
| 33 } | |
| 34 | |
| 35 bool JSONFileValueSerializer::SerializeInternal(const base::Value& root, | |
| 36 bool omit_binary_values) { | |
| 37 std::string json_string; | |
| 38 JSONStringValueSerializer serializer(&json_string); | |
| 39 serializer.set_pretty_print(true); | |
| 40 bool result = omit_binary_values ? | |
| 41 serializer.SerializeAndOmitBinaryValues(root) : | |
| 42 serializer.Serialize(root); | |
| 43 if (!result) | |
| 44 return false; | |
| 45 | |
| 46 int data_size = static_cast<int>(json_string.size()); | |
| 47 if (base::WriteFile(json_file_path_, json_string.data(), data_size) != | |
| 48 data_size) | |
| 49 return false; | |
| 50 | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 JSONFileValueDeserializer::JSONFileValueDeserializer( | |
| 55 const base::FilePath& json_file_path) | |
| 56 : json_file_path_(json_file_path), | |
| 57 allow_trailing_comma_(false), | |
| 58 last_read_size_(0U) { | |
| 59 } | |
| 60 | |
| 61 JSONFileValueDeserializer::~JSONFileValueDeserializer() { | |
| 62 } | |
| 63 | |
| 64 int JSONFileValueDeserializer::ReadFileToString(std::string* json_string) { | |
| 65 DCHECK(json_string); | |
| 66 if (!base::ReadFileToString(json_file_path_, json_string)) { | |
| 67 #if defined(OS_WIN) | |
| 68 int error = ::GetLastError(); | |
| 69 if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) { | |
| 70 return JSON_FILE_LOCKED; | |
| 71 } else if (error == ERROR_ACCESS_DENIED) { | |
| 72 return JSON_ACCESS_DENIED; | |
| 73 } | |
| 74 #endif | |
| 75 if (!base::PathExists(json_file_path_)) | |
| 76 return JSON_NO_SUCH_FILE; | |
| 77 else | |
| 78 return JSON_CANNOT_READ_FILE; | |
| 79 } | |
| 80 | |
| 81 last_read_size_ = json_string->size(); | |
| 82 return JSON_NO_ERROR; | |
| 83 } | |
| 84 | |
| 85 const char* JSONFileValueDeserializer::GetErrorMessageForCode(int error_code) { | |
| 86 switch (error_code) { | |
| 87 case JSON_NO_ERROR: | |
| 88 return ""; | |
| 89 case JSON_ACCESS_DENIED: | |
| 90 return kAccessDenied; | |
| 91 case JSON_CANNOT_READ_FILE: | |
| 92 return kCannotReadFile; | |
| 93 case JSON_FILE_LOCKED: | |
| 94 return kFileLocked; | |
| 95 case JSON_NO_SUCH_FILE: | |
| 96 return kNoSuchFile; | |
| 97 default: | |
| 98 NOTREACHED(); | |
| 99 return ""; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 base::Value* JSONFileValueDeserializer::Deserialize(int* error_code, | |
| 104 std::string* error_str) { | |
| 105 std::string json_string; | |
| 106 int error = ReadFileToString(&json_string); | |
| 107 if (error != JSON_NO_ERROR) { | |
| 108 if (error_code) | |
| 109 *error_code = error; | |
| 110 if (error_str) | |
| 111 *error_str = GetErrorMessageForCode(error); | |
| 112 return NULL; | |
| 113 } | |
| 114 | |
| 115 JSONStringValueDeserializer deserializer(json_string); | |
| 116 deserializer.set_allow_trailing_comma(allow_trailing_comma_); | |
| 117 return deserializer.Deserialize(error_code, error_str); | |
| 118 } | |
| OLD | NEW |