| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/json/json_file_value_serializer.h" | 5 #include "base/json/json_file_value_serializer.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 using base::FilePath; | 11 using base::FilePath; |
| 12 | 12 |
| 13 const char* JSONFileValueSerializer::kAccessDenied = "Access denied."; | 13 const char* JSONFileValueSerializer::kAccessDenied = "Access denied."; |
| 14 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file."; | 14 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file."; |
| 15 const char* JSONFileValueSerializer::kFileLocked = "File locked."; | 15 const char* JSONFileValueSerializer::kFileLocked = "File locked."; |
| 16 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist."; | 16 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist."; |
| 17 | 17 |
| 18 bool JSONFileValueSerializer::Serialize(const Value& root) { | 18 bool JSONFileValueSerializer::Serialize(const base::Value& root) { |
| 19 return SerializeInternal(root, false); | 19 return SerializeInternal(root, false); |
| 20 } | 20 } |
| 21 | 21 |
| 22 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(const Value& root) { | 22 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues( |
| 23 const base::Value& root) { |
| 23 return SerializeInternal(root, true); | 24 return SerializeInternal(root, true); |
| 24 } | 25 } |
| 25 | 26 |
| 26 bool JSONFileValueSerializer::SerializeInternal(const Value& root, | 27 bool JSONFileValueSerializer::SerializeInternal(const base::Value& root, |
| 27 bool omit_binary_values) { | 28 bool omit_binary_values) { |
| 28 std::string json_string; | 29 std::string json_string; |
| 29 JSONStringValueSerializer serializer(&json_string); | 30 JSONStringValueSerializer serializer(&json_string); |
| 30 serializer.set_pretty_print(true); | 31 serializer.set_pretty_print(true); |
| 31 bool result = omit_binary_values ? | 32 bool result = omit_binary_values ? |
| 32 serializer.SerializeAndOmitBinaryValues(root) : | 33 serializer.SerializeAndOmitBinaryValues(root) : |
| 33 serializer.Serialize(root); | 34 serializer.Serialize(root); |
| 34 if (!result) | 35 if (!result) |
| 35 return false; | 36 return false; |
| 36 | 37 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 case JSON_FILE_LOCKED: | 74 case JSON_FILE_LOCKED: |
| 74 return kFileLocked; | 75 return kFileLocked; |
| 75 case JSON_NO_SUCH_FILE: | 76 case JSON_NO_SUCH_FILE: |
| 76 return kNoSuchFile; | 77 return kNoSuchFile; |
| 77 default: | 78 default: |
| 78 NOTREACHED(); | 79 NOTREACHED(); |
| 79 return ""; | 80 return ""; |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 83 Value* JSONFileValueSerializer::Deserialize(int* error_code, | 84 base::Value* JSONFileValueSerializer::Deserialize(int* error_code, |
| 84 std::string* error_str) { | 85 std::string* error_str) { |
| 85 std::string json_string; | 86 std::string json_string; |
| 86 int error = ReadFileToString(&json_string); | 87 int error = ReadFileToString(&json_string); |
| 87 if (error != JSON_NO_ERROR) { | 88 if (error != JSON_NO_ERROR) { |
| 88 if (error_code) | 89 if (error_code) |
| 89 *error_code = error; | 90 *error_code = error; |
| 90 if (error_str) | 91 if (error_str) |
| 91 *error_str = GetErrorMessageForCode(error); | 92 *error_str = GetErrorMessageForCode(error); |
| 92 return NULL; | 93 return NULL; |
| 93 } | 94 } |
| 94 | 95 |
| 95 JSONStringValueSerializer serializer(json_string); | 96 JSONStringValueSerializer serializer(json_string); |
| 96 serializer.set_allow_trailing_comma(allow_trailing_comma_); | 97 serializer.set_allow_trailing_comma(allow_trailing_comma_); |
| 97 return serializer.Deserialize(error_code, error_str); | 98 return serializer.Deserialize(error_code, error_str); |
| 98 } | 99 } |
| OLD | NEW |