1 // Copyright (c) 2012 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 "tools/json_schema_compiler/util.h"
7 #include "base/stl_util.h"
8 #include "base/values.h"
10 namespace json_schema_compiler
{
13 bool PopulateItem(const base::Value
& from
, int* out
) {
14 return from
.GetAsInteger(out
);
17 bool PopulateItem(const base::Value
& from
, bool* out
) {
18 return from
.GetAsBoolean(out
);
21 bool PopulateItem(const base::Value
& from
, double* out
) {
22 return from
.GetAsDouble(out
);
25 bool PopulateItem(const base::Value
& from
, std::string
* out
) {
26 return from
.GetAsString(out
);
29 bool PopulateItem(const base::Value
& from
, std::vector
<char>* out
) {
30 const base::BinaryValue
* binary
= nullptr;
31 if (!from
.GetAsBinary(&binary
))
33 out
->assign(binary
->GetBuffer(), binary
->GetBuffer() + binary
->GetSize());
37 bool PopulateItem(const base::Value
& from
, linked_ptr
<base::Value
>* out
) {
38 *out
= make_linked_ptr(from
.DeepCopy());
42 bool PopulateItem(const base::Value
& from
,
43 linked_ptr
<base::DictionaryValue
>* out
) {
44 const base::DictionaryValue
* dict
= nullptr;
45 if (!from
.GetAsDictionary(&dict
))
47 *out
= make_linked_ptr(dict
->DeepCopy());
51 void AddItemToList(const int from
, base::ListValue
* out
) {
52 out
->Append(new base::FundamentalValue(from
));
55 void AddItemToList(const bool from
, base::ListValue
* out
) {
56 out
->Append(new base::FundamentalValue(from
));
59 void AddItemToList(const double from
, base::ListValue
* out
) {
60 out
->Append(new base::FundamentalValue(from
));
63 void AddItemToList(const std::string
& from
, base::ListValue
* out
) {
64 out
->Append(new base::StringValue(from
));
67 void AddItemToList(const std::vector
<char>& from
, base::ListValue
* out
) {
68 out
->Append(base::BinaryValue::CreateWithCopiedBuffer(vector_as_array(&from
),
72 void AddItemToList(const linked_ptr
<base::Value
>& from
, base::ListValue
* out
) {
73 out
->Append(from
->DeepCopy());
76 void AddItemToList(const linked_ptr
<base::DictionaryValue
>& from
,
77 base::ListValue
* out
) {
78 out
->Append(static_cast<base::Value
*>(from
->DeepCopy()));
81 std::string
ValueTypeToString(base::Value::Type type
) {
83 case base::Value::TYPE_NULL
:
85 case base::Value::TYPE_BOOLEAN
:
87 case base::Value::TYPE_INTEGER
:
89 case base::Value::TYPE_DOUBLE
:
91 case base::Value::TYPE_STRING
:
93 case base::Value::TYPE_BINARY
:
95 case base::Value::TYPE_DICTIONARY
:
97 case base::Value::TYPE_LIST
:
105 } // namespace json_schema_compiler