1 // Copyright 2014 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 "components/cloud_devices/common/cloud_device_description.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "components/cloud_devices/common/cloud_device_description_consts.h"
13 namespace cloud_devices
{
15 CloudDeviceDescription::CloudDeviceDescription() {
19 CloudDeviceDescription::~CloudDeviceDescription() {
22 void CloudDeviceDescription::Reset() {
23 root_
.reset(new base::DictionaryValue
);
24 root_
->SetString(json::kVersion
, json::kVersion10
);
27 bool CloudDeviceDescription::InitFromDictionary(
28 scoped_ptr
<base::DictionaryValue
> root
) {
34 root_
->GetString(json::kVersion
, &version
);
35 return version
== json::kVersion10
;
38 bool CloudDeviceDescription::InitFromString(const std::string
& json
) {
39 scoped_ptr
<base::Value
> parsed
= base::JSONReader::Read(json
);
40 base::DictionaryValue
* description
= NULL
;
41 if (!parsed
|| !parsed
->GetAsDictionary(&description
))
43 ignore_result(parsed
.release());
44 return InitFromDictionary(make_scoped_ptr(description
));
47 std::string
CloudDeviceDescription::ToString() const {
49 base::JSONWriter::WriteWithOptions(
50 *root_
, base::JSONWriter::OPTIONS_PRETTY_PRINT
, &json
);
54 const base::DictionaryValue
* CloudDeviceDescription::GetItem(
55 const std::string
& path
) const {
56 const base::DictionaryValue
* value
= NULL
;
57 root_
->GetDictionary(path
, &value
);
61 base::DictionaryValue
* CloudDeviceDescription::CreateItem(
62 const std::string
& path
) {
63 base::DictionaryValue
* value
= new base::DictionaryValue
;
64 root_
->Set(path
, value
);
68 const base::ListValue
* CloudDeviceDescription::GetListItem(
69 const std::string
& path
) const {
70 const base::ListValue
* value
= NULL
;
71 root_
->GetList(path
, &value
);
75 base::ListValue
* CloudDeviceDescription::CreateListItem(
76 const std::string
& path
) {
77 base::ListValue
* value
= new base::ListValue
;
78 root_
->Set(path
, value
);
82 } // namespace cloud_devices