| 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 scoped_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 scoped_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.Pass(); | 23 return value.Pass(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 scoped_ptr<std::string> SerializeToJson(const base::Value& value) { | 26 scoped_ptr<std::string> SerializeToJson(const base::Value& value) { |
| 27 scoped_ptr<std::string> json_str(new std::string()); | 27 scoped_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.Pass(); | 31 return json_str.Pass(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 scoped_ptr<base::Value> DeserializeJsonFromFile(const base::FilePath& path) { | 34 scoped_ptr<base::Value> DeserializeJsonFromFile(const base::FilePath& path) { |
| 35 JSONFileValueDeserializer deserializer(path); | 35 JSONFileValueDeserializer deserializer(path); |
| 36 | 36 |
| 37 int error_code = -1; | 37 int error_code = -1; |
| 38 std::string error_msg; | 38 std::string error_msg; |
| 39 scoped_ptr<base::Value> value( | 39 scoped_ptr<base::Value> value = |
| 40 deserializer.Deserialize(&error_code, &error_msg)); | 40 deserializer.Deserialize(&error_code, &error_msg); |
| 41 DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg; | 41 DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg; |
| 42 | 42 |
| 43 // Value will hold the nullptr in case of an error. | 43 // Value will hold the nullptr in case of an error. |
| 44 return value.Pass(); | 44 return value.Pass(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool SerializeJsonToFile(const base::FilePath& path, const base::Value& value) { | 47 bool SerializeJsonToFile(const base::FilePath& path, const base::Value& value) { |
| 48 JSONFileValueSerializer serializer(path); | 48 JSONFileValueSerializer serializer(path); |
| 49 return serializer.Serialize(value); | 49 return serializer.Serialize(value); |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace chromecast | 52 } // namespace chromecast |
| OLD | NEW |