| OLD | NEW |
| 1 // Copyright (c) 2011 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_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_reader.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | |
| 11 | 10 |
| 12 const char* JSONFileValueSerializer::kAccessDenied = "Access denied."; | 11 const char* JSONFileValueSerializer::kAccessDenied = "Access denied."; |
| 13 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file."; | 12 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file."; |
| 14 const char* JSONFileValueSerializer::kFileLocked = "File locked."; | 13 const char* JSONFileValueSerializer::kFileLocked = "File locked."; |
| 15 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist."; | 14 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist."; |
| 16 | 15 |
| 17 JSONStringValueSerializer::~JSONStringValueSerializer() {} | |
| 18 | |
| 19 bool JSONStringValueSerializer::Serialize(const Value& root) { | |
| 20 return SerializeInternal(root, false); | |
| 21 } | |
| 22 | |
| 23 bool JSONStringValueSerializer::SerializeAndOmitBinaryValues( | |
| 24 const Value& root) { | |
| 25 return SerializeInternal(root, true); | |
| 26 } | |
| 27 | |
| 28 bool JSONStringValueSerializer::SerializeInternal(const Value& root, | |
| 29 bool omit_binary_values) { | |
| 30 if (!json_string_ || initialized_with_const_string_) | |
| 31 return false; | |
| 32 | |
| 33 base::JSONWriter::WriteWithOptions( | |
| 34 &root, | |
| 35 pretty_print_, | |
| 36 omit_binary_values ? base::JSONWriter::OPTIONS_OMIT_BINARY_VALUES : 0, | |
| 37 json_string_); | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 Value* JSONStringValueSerializer::Deserialize(int* error_code, | |
| 42 std::string* error_str) { | |
| 43 if (!json_string_) | |
| 44 return NULL; | |
| 45 | |
| 46 return base::JSONReader::ReadAndReturnError(*json_string_, | |
| 47 allow_trailing_comma_, | |
| 48 error_code, | |
| 49 error_str); | |
| 50 } | |
| 51 | |
| 52 /******* File Serializer *******/ | |
| 53 | |
| 54 bool JSONFileValueSerializer::Serialize(const Value& root) { | 16 bool JSONFileValueSerializer::Serialize(const Value& root) { |
| 55 return SerializeInternal(root, false); | 17 return SerializeInternal(root, false); |
| 56 } | 18 } |
| 57 | 19 |
| 58 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(const Value& root) { | 20 bool JSONFileValueSerializer::SerializeAndOmitBinaryValues(const Value& root) { |
| 59 return SerializeInternal(root, true); | 21 return SerializeInternal(root, true); |
| 60 } | 22 } |
| 61 | 23 |
| 62 bool JSONFileValueSerializer::SerializeInternal(const Value& root, | 24 bool JSONFileValueSerializer::SerializeInternal(const Value& root, |
| 63 bool omit_binary_values) { | 25 bool omit_binary_values) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 *error_code = error; | 87 *error_code = error; |
| 126 if (error_str) | 88 if (error_str) |
| 127 *error_str = GetErrorMessageForCode(error); | 89 *error_str = GetErrorMessageForCode(error); |
| 128 return NULL; | 90 return NULL; |
| 129 } | 91 } |
| 130 | 92 |
| 131 JSONStringValueSerializer serializer(json_string); | 93 JSONStringValueSerializer serializer(json_string); |
| 132 serializer.set_allow_trailing_comma(allow_trailing_comma_); | 94 serializer.set_allow_trailing_comma(allow_trailing_comma_); |
| 133 return serializer.Deserialize(error_code, error_str); | 95 return serializer.Deserialize(error_code, error_str); |
| 134 } | 96 } |
| OLD | NEW |