JSONStringValueSerializer takes a StringPiece instead of std::string&.
[chromium-blink-merge.git] / tools / json_schema_compiler / util.h
blobc27e48adfc9762d15907c257ae2c2091d6b3d1d7
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 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
8 #include <string>
9 #include <vector>
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
15 namespace json_schema_compiler {
17 namespace util {
19 // Populates the item |out| from the value |from|. These are used by template
20 // specializations of |Get(Optional)ArrayFromList|.
21 bool PopulateItem(const base::Value& from, int* out);
22 bool PopulateItem(const base::Value& from, bool* out);
23 bool PopulateItem(const base::Value& from, double* out);
24 bool PopulateItem(const base::Value& from, std::string* out);
25 bool PopulateItem(const base::Value& from, std::vector<char>* out);
26 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out);
27 bool PopulateItem(const base::Value& from,
28 linked_ptr<base::DictionaryValue>* out);
30 // This template is used for types generated by tools/json_schema_compiler.
31 template <class T>
32 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) {
33 const base::DictionaryValue* dict = nullptr;
34 if (!from.GetAsDictionary(&dict))
35 return false;
36 scoped_ptr<T> obj(new T());
37 if (!T::Populate(*dict, obj.get()))
38 return false;
39 *out = linked_ptr<T>(obj.release());
40 return true;
43 // Populates |out| with |list|. Returns false if there is no list at the
44 // specified key or if the list has anything other than |T|.
45 template <class T>
46 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
47 out->clear();
48 T item;
49 for (const base::Value* value : list) {
50 if (!PopulateItem(*value, &item))
51 return false;
52 out->push_back(item);
55 return true;
58 // Creates a new vector containing |list| at |out|. Returns
59 // true on success or if there is nothing at the specified key. Returns false
60 // if anything other than a list of |T| is at the specified key.
61 template <class T>
62 bool PopulateOptionalArrayFromList(const base::ListValue& list,
63 scoped_ptr<std::vector<T>>* out) {
64 out->reset(new std::vector<T>());
65 if (!PopulateArrayFromList(list, out->get())) {
66 out->reset();
67 return false;
69 return true;
72 // Appends a Value newly created from |from| to |out|. These used by template
73 // specializations of |Set(Optional)ArrayToList|.
74 void AddItemToList(const int from, base::ListValue* out);
75 void AddItemToList(const bool from, base::ListValue* out);
76 void AddItemToList(const double from, base::ListValue* out);
77 void AddItemToList(const std::string& from, base::ListValue* out);
78 void AddItemToList(const std::vector<char>& from, base::ListValue* out);
79 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out);
80 void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
81 base::ListValue* out);
83 // This template is used for types generated by tools/json_schema_compiler.
84 template <class T>
85 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) {
86 out->Append(from->ToValue().release());
89 // Set |out| to the the contents of |from|. Requires PopulateItem to be
90 // implemented for |T|.
91 template <class T>
92 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
93 out->Clear();
94 for (const auto& item : from)
95 AddItemToList(item, out);
98 // Set |out| to the the contents of |from| if |from| is not null. Requires
99 // PopulateItem to be implemented for |T|.
100 template <class T>
101 void PopulateListFromOptionalArray(const scoped_ptr<std::vector<T>>& from,
102 base::ListValue* out) {
103 if (from.get())
104 PopulateListFromArray(*from, out);
107 template <class T>
108 scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
109 base::ListValue* list = new base::ListValue();
110 PopulateListFromArray(from, list);
111 return scoped_ptr<base::Value>(list);
114 template <class T>
115 scoped_ptr<base::Value> CreateValueFromOptionalArray(
116 const scoped_ptr<std::vector<T>>& from) {
117 if (from.get())
118 return CreateValueFromArray(*from);
119 return scoped_ptr<base::Value>();
122 std::string ValueTypeToString(base::Value::Type type);
124 } // namespace util
125 } // namespace json_schema_compiler
127 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__