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
3 // found in the LICENSE file.
5 #include "chromecast/base/serializers.h"
7 #include "base/json/json_file_value_serializer.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/logging.h"
11 namespace chromecast
{
13 scoped_ptr
<base::Value
> DeserializeFromJson(const std::string
& text
) {
14 JSONStringValueDeserializer
deserializer(text
);
17 std::string error_msg
;
18 scoped_ptr
<base::Value
> value(
19 deserializer
.Deserialize(&error_code
, &error_msg
));
20 DLOG_IF(ERROR
, !value
) << "JSON error " << error_code
<< ":" << error_msg
;
22 // Value will hold the nullptr in case of an error.
26 scoped_ptr
<std::string
> SerializeToJson(const base::Value
& value
) {
27 scoped_ptr
<std::string
> json_str(new std::string());
28 JSONStringValueSerializer
serializer(json_str
.get());
29 if (!serializer
.Serialize(value
))
30 json_str
.reset(nullptr);
31 return json_str
.Pass();
34 scoped_ptr
<base::Value
> DeserializeJsonFromFile(const base::FilePath
& path
) {
35 JSONFileValueDeserializer
deserializer(path
);
38 std::string error_msg
;
39 scoped_ptr
<base::Value
> value(
40 deserializer
.Deserialize(&error_code
, &error_msg
));
41 DLOG_IF(ERROR
, !value
) << "JSON error " << error_code
<< ":" << error_msg
;
43 // Value will hold the nullptr in case of an error.
47 bool SerializeJsonToFile(const base::FilePath
& path
, const base::Value
& value
) {
48 JSONFileValueSerializer
serializer(path
);
49 return serializer
.Serialize(value
);
52 } // namespace chromecast