summaryrefslogtreecommitdiffstats
path: root/chromecast/base/serializers.cc
blob: 87e9dc51cef477a7f7b4fe626ccb246e6e4dbccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromecast/base/serializers.h"

#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"

namespace chromecast {

scoped_ptr<base::Value> DeserializeFromJson(const std::string& text) {
  JSONStringValueDeserializer deserializer(text);

  int error_code = -1;
  std::string error_msg;
  scoped_ptr<base::Value> value =
      deserializer.Deserialize(&error_code, &error_msg);
  DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg;

  // Value will hold the nullptr in case of an error.
  return value;
}

scoped_ptr<std::string> SerializeToJson(const base::Value& value) {
  scoped_ptr<std::string> json_str(new std::string());
  JSONStringValueSerializer serializer(json_str.get());
  if (!serializer.Serialize(value))
    json_str.reset(nullptr);
  return json_str;
}

scoped_ptr<base::Value> DeserializeJsonFromFile(const base::FilePath& path) {
  JSONFileValueDeserializer deserializer(path);

  int error_code = -1;
  std::string error_msg;
  scoped_ptr<base::Value> value =
      deserializer.Deserialize(&error_code, &error_msg);
  DLOG_IF(ERROR, !value) << "JSON error " << error_code << ":" << error_msg;

  // Value will hold the nullptr in case of an error.
  return value;
}

bool SerializeJsonToFile(const base::FilePath& path, const base::Value& value) {
  JSONFileValueSerializer serializer(path);
  return serializer.Serialize(value);
}

}  // namespace chromecast