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/strings/utf_string_conversions.h"
9 #include "base/values.h"
11 namespace json_schema_compiler
{
14 bool PopulateItem(const base::Value
& from
, int* out
) {
15 return from
.GetAsInteger(out
);
18 bool PopulateItem(const base::Value
& from
, int* out
, base::string16
* error
) {
19 if (!from
.GetAsInteger(out
)) {
20 if (error
->length()) {
21 error
->append(base::UTF8ToUTF16("; "));
23 error
->append(base::UTF8ToUTF16("expected integer, got " +
24 ValueTypeToString(from
.GetType())));
30 bool PopulateItem(const base::Value
& from
, bool* out
) {
31 return from
.GetAsBoolean(out
);
34 bool PopulateItem(const base::Value
& from
, bool* out
, base::string16
* error
) {
35 if (!from
.GetAsBoolean(out
)) {
36 if (error
->length()) {
37 error
->append(base::UTF8ToUTF16("; "));
39 error
->append(base::UTF8ToUTF16("expected boolean, got " +
40 ValueTypeToString(from
.GetType())));
46 bool PopulateItem(const base::Value
& from
, double* out
) {
47 return from
.GetAsDouble(out
);
50 bool PopulateItem(const base::Value
& from
, double* out
, base::string16
* error
) {
51 if (!from
.GetAsDouble(out
)) {
52 if (error
->length()) {
53 error
->append(base::UTF8ToUTF16("; "));
55 error
->append(base::UTF8ToUTF16("expected double, got " +
56 ValueTypeToString(from
.GetType())));
62 bool PopulateItem(const base::Value
& from
, std::string
* out
) {
63 return from
.GetAsString(out
);
66 bool PopulateItem(const base::Value
& from
,
68 base::string16
* error
) {
69 if (!from
.GetAsString(out
)) {
70 if (error
->length()) {
71 error
->append(base::UTF8ToUTF16("; "));
73 error
->append(base::UTF8ToUTF16("expected string, got " +
74 ValueTypeToString(from
.GetType())));
80 bool PopulateItem(const base::Value
& from
, std::vector
<char>* out
) {
81 const base::BinaryValue
* binary
= nullptr;
82 if (!from
.GetAsBinary(&binary
))
84 out
->assign(binary
->GetBuffer(), binary
->GetBuffer() + binary
->GetSize());
88 bool PopulateItem(const base::Value
& from
,
89 std::vector
<char>* out
,
90 base::string16
* error
) {
91 const base::BinaryValue
* binary
= nullptr;
92 if (!from
.GetAsBinary(&binary
)) {
93 if (error
->length()) {
94 error
->append(base::UTF8ToUTF16("; "));
96 error
->append(base::UTF8ToUTF16("expected binary, got " +
97 ValueTypeToString(from
.GetType())));
100 out
->assign(binary
->GetBuffer(), binary
->GetBuffer() + binary
->GetSize());
104 bool PopulateItem(const base::Value
& from
, linked_ptr
<base::Value
>* out
) {
105 *out
= make_linked_ptr(from
.DeepCopy());
109 bool PopulateItem(const base::Value
& from
,
110 linked_ptr
<base::Value
>* out
,
111 base::string16
* error
) {
112 *out
= make_linked_ptr(from
.DeepCopy());
116 bool PopulateItem(const base::Value
& from
,
117 linked_ptr
<base::DictionaryValue
>* out
) {
118 const base::DictionaryValue
* dict
= nullptr;
119 if (!from
.GetAsDictionary(&dict
))
121 *out
= make_linked_ptr(dict
->DeepCopy());
125 bool PopulateItem(const base::Value
& from
,
126 linked_ptr
<base::DictionaryValue
>* out
,
127 base::string16
* error
) {
128 const base::DictionaryValue
* dict
= nullptr;
129 if (!from
.GetAsDictionary(&dict
)) {
130 if (error
->length()) {
131 error
->append(base::UTF8ToUTF16("; "));
133 error
->append(base::UTF8ToUTF16("expected dictionary, got " +
134 ValueTypeToString(from
.GetType())));
137 *out
= make_linked_ptr(dict
->DeepCopy());
141 void AddItemToList(const int from
, base::ListValue
* out
) {
142 out
->Append(new base::FundamentalValue(from
));
145 void AddItemToList(const bool from
, base::ListValue
* out
) {
146 out
->Append(new base::FundamentalValue(from
));
149 void AddItemToList(const double from
, base::ListValue
* out
) {
150 out
->Append(new base::FundamentalValue(from
));
153 void AddItemToList(const std::string
& from
, base::ListValue
* out
) {
154 out
->Append(new base::StringValue(from
));
157 void AddItemToList(const std::vector
<char>& from
, base::ListValue
* out
) {
158 out
->Append(base::BinaryValue::CreateWithCopiedBuffer(vector_as_array(&from
),
162 void AddItemToList(const linked_ptr
<base::Value
>& from
, base::ListValue
* out
) {
163 out
->Append(from
->DeepCopy());
166 void AddItemToList(const linked_ptr
<base::DictionaryValue
>& from
,
167 base::ListValue
* out
) {
168 out
->Append(static_cast<base::Value
*>(from
->DeepCopy()));
171 std::string
ValueTypeToString(base::Value::Type type
) {
173 case base::Value::TYPE_NULL
:
175 case base::Value::TYPE_BOOLEAN
:
177 case base::Value::TYPE_INTEGER
:
179 case base::Value::TYPE_DOUBLE
:
181 case base::Value::TYPE_STRING
:
183 case base::Value::TYPE_BINARY
:
185 case base::Value::TYPE_DICTIONARY
:
187 case base::Value::TYPE_LIST
:
195 } // namespace json_schema_compiler