| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chromecast/base/serializers.h" | 5 #include "chromecast/base/serializers.h" |
| 6 | 6 |
| 7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.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 namespace chromecast { | 11 namespace chromecast { |
| 12 | 12 |
| 13 scoped_ptr<base::Value> DeserializeFromJson(const std::string& text) { | 13 std::unique_ptr<base::Value> DeserializeFromJson(const std::string& text) { |
| 14 JSONStringValueDeserializer deserializer(text); | 14 JSONStringValueDeserializer deserializer(text); |
| 15 | 15 |
| 16 int error_code = -1; | 16 int error_code = -1; |
| 17 std::string error_msg; | 17 std::string error_msg; |
| 18 scoped_ptr<base::Value> value = | 18 std::unique_ptr<base::Value> value = |
| 19 deserializer.Deserialize(&error_code, &error_msg); | 19 deserializer.Deserialize(&error_code, &error_msg); |
| 20 DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg; | 20 DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg; |
| 21 | 21 |
| 22 // Value will hold the nullptr in case of an error. | 22 // Value will hold the nullptr in case of an error. |
| 23 return value; | 23 return value; |
| 24 } | 24 } |
| 25 | 25 |
| 26 scoped_ptr<std::string> SerializeToJson(const base::Value& value) { | 26 std::unique_ptr<std::string> SerializeToJson(const base::Value& value) { |
| 27 scoped_ptr<std::string> json_str(new std::string()); | 27 std::unique_ptr<std::string> json_str(new std::string()); |
| 28 JSONStringValueSerializer serializer(json_str.get()); | 28 JSONStringValueSerializer serializer(json_str.get()); |
| 29 if (!serializer.Serialize(value)) | 29 if (!serializer.Serialize(value)) |
| 30 json_str.reset(nullptr); | 30 json_str.reset(nullptr); |
| 31 return json_str; | 31 return json_str; |
| 32 } | 32 } |
| 33 | 33 |
| 34 scoped_ptr<base::Value> DeserializeJsonFromFile(const base::FilePath& path) { | 34 std::unique_ptr<base::Value> DeserializeJsonFromFile( |
| 35 const base::FilePath& path) { |
| 35 JSONFileValueDeserializer deserializer(path); | 36 JSONFileValueDeserializer deserializer(path); |
| 36 | 37 |
| 37 int error_code = -1; | 38 int error_code = -1; |
| 38 std::string error_msg; | 39 std::string error_msg; |
| 39 scoped_ptr<base::Value> value = | 40 std::unique_ptr<base::Value> value = |
| 40 deserializer.Deserialize(&error_code, &error_msg); | 41 deserializer.Deserialize(&error_code, &error_msg); |
| 41 DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg; | 42 DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg; |
| 42 | 43 |
| 43 // Value will hold the nullptr in case of an error. | 44 // Value will hold the nullptr in case of an error. |
| 44 return value; | 45 return value; |
| 45 } | 46 } |
| 46 | 47 |
| 47 bool SerializeJsonToFile(const base::FilePath& path, const base::Value& value) { | 48 bool SerializeJsonToFile(const base::FilePath& path, const base::Value& value) { |
| 48 JSONFileValueSerializer serializer(path); | 49 JSONFileValueSerializer serializer(path); |
| 49 return serializer.Serialize(value); | 50 return serializer.Serialize(value); |
| 50 } | 51 } |
| 51 | 52 |
| 52 } // namespace chromecast | 53 } // namespace chromecast |
| OLD | NEW |