blob: fbaa3699811104dc67422b94407432e5e5c5f55c [file] [log] [blame]
[email protected]13502562012-05-09 21:54:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]9e4cda7332010-07-31 04:56:145// This file specifies a recursive data storage class called Value intended for
[email protected]2f03f532013-07-17 08:43:336// storing settings and other persistable data.
initial.commitd7cae122008-07-26 21:49:387//
[email protected]2f03f532013-07-17 08:43:338// A Value represents something that can be stored in JSON or passed to/from
9// JavaScript. As such, it is NOT a generalized variant type, since only the
10// types supported by JavaScript/JSON are supported.
initial.commitd7cae122008-07-26 21:49:3811//
avi9b6f42932015-12-26 22:15:1412// IN PARTICULAR this means that there is no support for int64_t or unsigned
[email protected]2f03f532013-07-17 08:43:3313// numbers. Writing JSON with such types would violate the spec. If you need
14// something like this, either use a double or make a string value containing
15// the number you want.
Daniel Cheng9d949902017-09-26 00:52:4316//
17// NOTE: A Value parameter that is always a Value::STRING should just be passed
18// as a std::string. Similarly for Values that are always Value::DICTIONARY
19// (should be flat_map), Value::LIST (should be std::vector), et cetera.
initial.commitd7cae122008-07-26 21:49:3820
[email protected]101d5422008-09-26 20:22:4221#ifndef BASE_VALUES_H_
22#define BASE_VALUES_H_
initial.commitd7cae122008-07-26 21:49:3823
[email protected]c014f2b32013-09-03 23:29:1224#include <stddef.h>
avi9b6f42932015-12-26 22:15:1425#include <stdint.h>
[email protected]c014f2b32013-09-03 23:29:1226
27#include <iosfwd>
initial.commitd7cae122008-07-26 21:49:3828#include <map>
dcheng093de9b2016-04-04 21:25:5129#include <memory>
[email protected]8e50b602009-03-03 22:59:4330#include <string>
[email protected]c014f2b32013-09-03 23:29:1231#include <utility>
initial.commitd7cae122008-07-26 21:49:3832#include <vector>
33
[email protected]0bea7252011-08-05 15:34:0034#include "base/base_export.h"
Jan Wilken Dörrie7e7a9792019-10-15 14:42:0535#include "base/containers/checked_iterators.h"
Jan Wilken Dörrie8d9034f12019-11-28 14:48:5736#include "base/containers/checked_range.h"
mkwstcd8067b2017-04-11 06:52:2137#include "base/containers/flat_map.h"
jdoerriecd022242017-08-23 08:38:2738#include "base/containers/span.h"
[email protected]c851cfd2013-06-10 20:11:1439#include "base/strings/string16.h"
asvitkinedbd26533e2015-06-23 18:22:5240#include "base/strings/string_piece.h"
jdoerrie44efa9d2017-07-14 14:47:2041#include "base/value_iterators.h"
Jan Wilken Dörrie79d022142020-08-19 18:18:3242#include "third_party/abseil-cpp/absl/types/variant.h"
initial.commitd7cae122008-07-26 21:49:3843
[email protected]f3a1c642011-07-12 19:15:0344namespace base {
45
initial.commitd7cae122008-07-26 21:49:3846class DictionaryValue;
47class ListValue;
[email protected]68d9d352011-02-21 16:35:0448class Value;
initial.commitd7cae122008-07-26 21:49:3849
[email protected]b59ea312011-08-05 18:20:0550// The Value class is the base class for Values. A Value can be instantiated
jdoerrie43ab3c02017-08-24 20:44:3651// via passing the appropriate type or backing storage to the constructor.
[email protected]2f03f532013-07-17 08:43:3352//
53// See the file-level comment above for more information.
Brett Wilson4bef8ee2017-09-01 20:11:4954//
55// base::Value is currently in the process of being refactored. Design doc:
56// https://docs.google.com/document/d/1uDLu5uTRlCWePxQUEHc8yNQdEoE1BDISYdpggWEABnw
57//
58// Previously (which is how most code that currently exists is written), Value
59// used derived types to implement the individual data types, and base::Value
60// was just a base class to refer to them. This required everything be heap
61// allocated.
62//
63// OLD WAY:
64//
65// std::unique_ptr<base::Value> GetFoo() {
66// std::unique_ptr<DictionaryValue> dict;
67// dict->SetString("mykey", foo);
68// return dict;
69// }
70//
71// The new design makes base::Value a variant type that holds everything in
72// a union. It is now recommended to pass by value with std::move rather than
73// use heap allocated values. The DictionaryValue and ListValue subclasses
74// exist only as a compatibility shim that we're in the process of removing.
75//
76// NEW WAY:
77//
78// base::Value GetFoo() {
79// base::Value dict(base::Value::Type::DICTIONARY);
80// dict.SetKey("mykey", base::Value(foo));
81// return dict;
82// }
Jan Wilken Dörriecf4ce5522020-10-27 14:41:0483//
84// The new design tries to avoid losing type information. Thus when migrating
85// off deprecated types, existing usages of base::ListValue should be replaced
86// by std::vector<base::Value>.
87//
88// Furthermore, existing usages of base::DictionaryValue should eventually be
89// replaced with base::flat_map<std::string, base::Value>. However, this
90// requires breaking changing the mapped type of Value::DictStorage first, and
91// thus usages of base::DictionaryValue should be kept for the time being.
[email protected]0bea7252011-08-05 15:34:0092class BASE_EXPORT Value {
initial.commitd7cae122008-07-26 21:49:3893 public:
jdoerrie9970f20e2018-07-20 21:41:1894 using BlobStorage = std::vector<uint8_t>;
jdoerriea5676c62017-04-11 18:09:1495 using ListStorage = std::vector<Value>;
Jan Wilken Dörrief961a372020-11-02 20:30:3496 using DictStorage = flat_map<std::string, Value>;
97
98 // Like `DictStorage`, but with std::unique_ptr in the mapped type. This is
99 // due to legacy reasons, and should be removed once no caller relies on
100 // stability of pointers anymore.
101 using LegacyDictStorage = flat_map<std::string, std::unique_ptr<Value>>;
Jan Wilken Dörrie8d9034f12019-11-28 14:48:57102
103 using ListView = CheckedContiguousRange<ListStorage>;
104 using ConstListView = CheckedContiguousConstRange<ListStorage>;
105
Jose Dapena Paz7685422a2019-04-03 18:35:04106 enum class Type : unsigned char {
jdoerriedc72ee942016-12-07 15:43:28107 NONE = 0,
108 BOOLEAN,
109 INTEGER,
110 DOUBLE,
111 STRING,
112 BINARY,
113 DICTIONARY,
jdoerriee1b1f3a2019-03-16 04:08:01114 LIST,
115 // TODO(crbug.com/859477): Remove once root cause is found.
116 DEAD
[email protected]2f03f532013-07-17 08:43:33117 // Note: Do not add more types. See the file-level comment above for why.
[email protected]a502bbe72011-01-07 18:06:45118 };
119
Lei Zhang30895d52017-10-23 19:14:46120 // Adaptors for converting from the old way to the new way and vice versa.
121 static Value FromUniquePtrValue(std::unique_ptr<Value> val);
122 static std::unique_ptr<Value> ToUniquePtrValue(Value val);
Lei Zhang8c1432b2019-10-08 18:48:54123 static const DictionaryValue& AsDictionaryValue(const Value& val);
124 static const ListValue& AsListValue(const Value& val);
Lei Zhang30895d52017-10-23 19:14:46125
Jan Wilken Dörrie79d022142020-08-19 18:18:32126 Value() noexcept;
brettwf78cc272017-03-24 16:36:42127 Value(Value&& that) noexcept;
jdoerriecc9f5732017-08-23 14:12:30128
129 // Value's copy constructor and copy assignment operator are deleted. Use this
130 // to obtain a deep copy explicitly.
131 Value Clone() const;
132
jdoerrie05eb3162017-02-01 10:36:56133 explicit Value(Type type);
134 explicit Value(bool in_bool);
135 explicit Value(int in_int);
136 explicit Value(double in_double);
137
jdoerrief38f37b2017-02-01 14:38:32138 // Value(const char*) and Value(const char16*) are required despite
jdoerrie9f90ad72017-09-11 17:23:26139 // Value(StringPiece) and Value(StringPiece16) because otherwise the
jdoerrief38f37b2017-02-01 14:38:32140 // compiler will choose the Value(bool) constructor for these arguments.
141 // Value(std::string&&) allow for efficient move construction.
jdoerrief38f37b2017-02-01 14:38:32142 explicit Value(const char* in_string);
jdoerrief38f37b2017-02-01 14:38:32143 explicit Value(StringPiece in_string);
jdoerrie9f90ad72017-09-11 17:23:26144 explicit Value(std::string&& in_string) noexcept;
145 explicit Value(const char16* in_string16);
146 explicit Value(StringPiece16 in_string16);
jdoerrief38f37b2017-02-01 14:38:32147
jdoerrie9970f20e2018-07-20 21:41:18148 explicit Value(const std::vector<char>& in_blob);
149 explicit Value(base::span<const uint8_t> in_blob);
jdoerrie5f12b6272017-04-18 10:22:41150 explicit Value(BlobStorage&& in_blob) noexcept;
jdoerriee03e80f2017-02-15 08:42:14151
jdoerriecc9f5732017-08-23 14:12:30152 explicit Value(const DictStorage& in_dict);
mkwstcd8067b2017-04-11 06:52:21153 explicit Value(DictStorage&& in_dict) noexcept;
154
Jan Wilken Dörrie53e009b2019-09-09 14:17:41155 explicit Value(span<const Value> in_list);
jdoerrie2b7d0fcd2017-04-19 07:15:38156 explicit Value(ListStorage&& in_list) noexcept;
157
jdoerrie17e71cc2017-03-30 06:40:29158 Value& operator=(Value&& that) noexcept;
David Bienvenu5f4d4f02020-09-27 16:55:03159 Value(const Value&) = delete;
160 Value& operator=(const Value&) = delete;
jdoerrie05eb3162017-02-01 10:36:56161
jdoerrie8e945542017-02-17 13:54:49162 ~Value();
jdoerrie05eb3162017-02-01 10:36:56163
thestig61709242016-07-19 00:39:30164 // Returns the name for a given |type|.
165 static const char* GetTypeName(Type type);
166
initial.commitd7cae122008-07-26 21:49:38167 // Returns the type of the value stored by the current Value object.
Jan Wilken Dörrie79d022142020-08-19 18:18:32168 Type type() const { return static_cast<Type>(data_.index()); }
initial.commitd7cae122008-07-26 21:49:38169
170 // Returns true if the current object represents a given type.
jdoerriecc9f5732017-08-23 14:12:30171 bool is_none() const { return type() == Type::NONE; }
jdoerrie05eb3162017-02-01 10:36:56172 bool is_bool() const { return type() == Type::BOOLEAN; }
173 bool is_int() const { return type() == Type::INTEGER; }
174 bool is_double() const { return type() == Type::DOUBLE; }
175 bool is_string() const { return type() == Type::STRING; }
176 bool is_blob() const { return type() == Type::BINARY; }
177 bool is_dict() const { return type() == Type::DICTIONARY; }
178 bool is_list() const { return type() == Type::LIST; }
179
Alexander Hendriche86ee512019-06-12 16:01:57180 // These will all CHECK that the type matches.
jdoerrie05eb3162017-02-01 10:36:56181 bool GetBool() const;
182 int GetInt() const;
183 double GetDouble() const; // Implicitly converts from int if necessary.
jdoerrief38f37b2017-02-01 14:38:32184 const std::string& GetString() const;
Dominic Battre08cbe972019-07-31 03:57:19185 std::string& GetString();
jdoerrie5f12b6272017-04-18 10:22:41186 const BlobStorage& GetBlob() const;
jdoerriee03e80f2017-02-15 08:42:14187
Jan Wilken Dörrie69a65a3a2020-01-16 15:03:30188 // Returns the Values in a list as a view. The mutable overload allows for
189 // modification of the underlying values, but does not allow changing the
190 // structure of the list. If this is desired, use TakeList(), perform the
191 // operations, and return the list back to the Value via move assignment.
192 ListView GetList();
Jan Wilken Dörrie8d9034f12019-11-28 14:48:57193 ConstListView GetList() const;
jdoerrie2b7d0fcd2017-04-19 07:15:38194
Lei Zhang20b21af82020-08-10 18:31:58195 // Transfers ownership of the underlying list to the caller. Subsequent
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05196 // calls to GetList() will return an empty list.
Jan Wilken Dörrie79d022142020-08-19 18:18:32197 // Note: This requires that type() is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05198 ListStorage TakeList();
199
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24200 // Appends |value| to the end of the list.
201 // Note: These CHECK that type() is Type::LIST.
202 void Append(bool value);
203 void Append(int value);
204 void Append(double value);
205 void Append(const char* value);
206 void Append(StringPiece value);
207 void Append(std::string&& value);
208 void Append(const char16* value);
209 void Append(StringPiece16 value);
210 void Append(Value&& value);
211
Jan Wilken Dörrie9065545e12019-10-30 10:44:51212 // Inserts |value| before |pos|.
Jan Wilken Dörrie69a65a3a2020-01-16 15:03:30213 // Note: This CHECK that type() is Type::LIST.
Jan Wilken Dörrie9065545e12019-10-30 10:44:51214 CheckedContiguousIterator<Value> Insert(
215 CheckedContiguousConstIterator<Value> pos,
216 Value&& value);
217
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05218 // Erases the Value pointed to by |iter|. Returns false if |iter| is out of
219 // bounds.
Jan Wilken Dörrie79d022142020-08-19 18:18:32220 // Note: This requires that type() is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05221 bool EraseListIter(CheckedContiguousConstIterator<Value> iter);
222
223 // Erases all Values that compare equal to |val|. Returns the number of
224 // deleted Values.
Jan Wilken Dörrie79d022142020-08-19 18:18:32225 // Note: This requires that type() is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05226 size_t EraseListValue(const Value& val);
227
228 // Erases all Values for which |pred| returns true. Returns the number of
229 // deleted Values.
Jan Wilken Dörrie79d022142020-08-19 18:18:32230 // Note: This requires that type() is Type::LIST.
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05231 template <typename Predicate>
232 size_t EraseListValueIf(Predicate pred) {
Jan Wilken Dörrie79d022142020-08-19 18:18:32233 return base::EraseIf(list(), pred);
Jan Wilken Dörrie7e7a9792019-10-15 14:42:05234 }
235
Jan Wilken Dörrie02577a22019-12-04 14:27:39236 // Erases all Values from the list.
Jan Wilken Dörrie79d022142020-08-19 18:18:32237 // Note: This requires that type() is Type::LIST.
Jan Wilken Dörrie02577a22019-12-04 14:27:39238 void ClearList();
239
jdoerrie44efa9d2017-07-14 14:47:20240 // |FindKey| looks up |key| in the underlying dictionary. If found, it returns
jdoerrie78ab7a22017-08-17 19:04:45241 // a pointer to the element. Otherwise it returns nullptr.
242 // returned. Callers are expected to perform a check against null before using
243 // the pointer.
Jan Wilken Dörrie79d022142020-08-19 18:18:32244 // Note: This requires that type() is Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45245 //
246 // Example:
247 // auto* found = FindKey("foo");
248 Value* FindKey(StringPiece key);
249 const Value* FindKey(StringPiece key) const;
jdoerrie44efa9d2017-07-14 14:47:20250
251 // |FindKeyOfType| is similar to |FindKey|, but it also requires the found
252 // value to have type |type|. If no type is found, or the found value is of a
jdoerrie78ab7a22017-08-17 19:04:45253 // different type nullptr is returned.
254 // Callers are expected to perform a check against null before using the
255 // pointer.
Jan Wilken Dörrie79d022142020-08-19 18:18:32256 // Note: This requires that type() is Type::DICTIONARY.
jdoerrie78ab7a22017-08-17 19:04:45257 //
258 // Example:
259 // auto* found = FindKey("foo", Type::DOUBLE);
260 Value* FindKeyOfType(StringPiece key, Type type);
261 const Value* FindKeyOfType(StringPiece key, Type type) const;
jdoerrie44efa9d2017-07-14 14:47:20262
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16263 // These are convenience forms of |FindKey|. They return |base::nullopt| if
264 // the value is not found or doesn't have the type specified in the
265 // function's name.
266 base::Optional<bool> FindBoolKey(StringPiece key) const;
267 base::Optional<int> FindIntKey(StringPiece key) const;
David 'Digit' Turnerc2c467f2019-03-20 21:41:09268 // Note FindDoubleKey() will auto-convert INTEGER keys to their double
269 // value, for consistency with GetDouble().
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16270 base::Optional<double> FindDoubleKey(StringPiece key) const;
271
272 // |FindStringKey| returns |nullptr| if value is not found or not a string.
273 const std::string* FindStringKey(StringPiece key) const;
Dominic Battre08cbe972019-07-31 03:57:19274 std::string* FindStringKey(StringPiece key);
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16275
David 'Digit' Turnerfca8c4b52019-03-26 11:14:06276 // Returns nullptr is value is not found or not a binary.
277 const BlobStorage* FindBlobKey(StringPiece key) const;
278
279 // Returns nullptr if value is not found or not a dictionary.
280 const Value* FindDictKey(StringPiece key) const;
281 Value* FindDictKey(StringPiece key);
282
283 // Returns nullptr if value is not found or not a list.
284 const Value* FindListKey(StringPiece key) const;
285 Value* FindListKey(StringPiece key);
286
jdoerrie44efa9d2017-07-14 14:47:20287 // |SetKey| looks up |key| in the underlying dictionary and sets the mapped
288 // value to |value|. If |key| could not be found, a new element is inserted.
jdoerrie78ab7a22017-08-17 19:04:45289 // A pointer to the modified item is returned.
Jan Wilken Dörrie79d022142020-08-19 18:18:32290 // Note: This requires that type() is Type::DICTIONARY.
David 'Digit' Turnere169e6c2019-03-28 22:06:29291 // Note: Prefer Set<Type>Key() for simple values.
jdoerrie78ab7a22017-08-17 19:04:45292 //
293 // Example:
294 // SetKey("foo", std::move(myvalue));
Sergey Abbakumov0e1215882019-03-15 22:32:16295 Value* SetKey(StringPiece key, Value&& value);
jdoerrie78ab7a22017-08-17 19:04:45296 // This overload results in a performance improvement for std::string&&.
Sergey Abbakumov0e1215882019-03-15 22:32:16297 Value* SetKey(std::string&& key, Value&& value);
jdoerrie46349472017-08-02 02:20:32298 // This overload is necessary to avoid ambiguity for const char* arguments.
Sergey Abbakumov0e1215882019-03-15 22:32:16299 Value* SetKey(const char* key, Value&& value);
jdoerrie44efa9d2017-07-14 14:47:20300
David 'Digit' Turnere169e6c2019-03-28 22:06:29301 // |Set<Type>Key| looks up |key| in the underlying dictionary and associates
302 // a corresponding Value() constructed from the second parameter. Compared
303 // to SetKey(), this avoids un-necessary temporary Value() creation, as well
304 // ambiguities in the value type.
305 Value* SetBoolKey(StringPiece key, bool val);
306 Value* SetIntKey(StringPiece key, int val);
307 Value* SetDoubleKey(StringPiece key, double val);
308 Value* SetStringKey(StringPiece key, StringPiece val);
Jan Wilken Dörrie293405a2020-05-12 19:45:11309 Value* SetStringKey(StringPiece key, StringPiece16 val);
310 // NOTE: The following two overloads are provided as performance / code
311 // generation optimizations.
David 'Digit' Turnere169e6c2019-03-28 22:06:29312 Value* SetStringKey(StringPiece key, const char* val);
313 Value* SetStringKey(StringPiece key, std::string&& val);
David 'Digit' Turnere169e6c2019-03-28 22:06:29314
jdoerriec209c7d2019-04-05 09:51:46315 // This attempts to remove the value associated with |key|. In case of
316 // failure, e.g. the key does not exist, false is returned and the underlying
jdoerrie64783162017-09-04 16:33:32317 // dictionary is not changed. In case of success, |key| is deleted from the
jdoerriec209c7d2019-04-05 09:51:46318 // dictionary and the method returns true.
Jan Wilken Dörrie79d022142020-08-19 18:18:32319 // Note: This requires that type() is Type::DICTIONARY.
jdoerrie64783162017-09-04 16:33:32320 //
321 // Example:
jdoerriec209c7d2019-04-05 09:51:46322 // bool success = dict.RemoveKey("foo");
jdoerrie64783162017-09-04 16:33:32323 bool RemoveKey(StringPiece key);
324
jdoerriec209c7d2019-04-05 09:51:46325 // This attempts to extract the value associated with |key|. In case of
326 // failure, e.g. the key does not exist, nullopt is returned and the
327 // underlying dictionary is not changed. In case of success, |key| is deleted
328 // from the dictionary and the method returns the extracted Value.
Jan Wilken Dörrie79d022142020-08-19 18:18:32329 // Note: This requires that type() is Type::DICTIONARY.
jdoerriec209c7d2019-04-05 09:51:46330 //
331 // Example:
332 // Optional<Value> maybe_value = dict.ExtractKey("foo");
333 Optional<Value> ExtractKey(StringPiece key);
334
Brett Wilsond16cf4ee2017-08-03 00:08:27335 // Searches a hierarchy of dictionary values for a given value. If a path
336 // of dictionaries exist, returns the item at that path. If any of the path
337 // components do not exist or if any but the last path components are not
338 // dictionaries, returns nullptr.
339 //
340 // The type of the leaf Value is not checked.
341 //
342 // Implementation note: This can't return an iterator because the iterator
343 // will actually be into another Value, so it can't be compared to iterators
jdoerrie78ab7a22017-08-17 19:04:45344 // from this one (in particular, the DictItems().end() iterator).
Brett Wilsond16cf4ee2017-08-03 00:08:27345 //
David 'Digit' Turner43ce6492019-04-04 16:04:44346 // This version takes a StringPiece for the path, using dots as separators.
Brett Wilsond16cf4ee2017-08-03 00:08:27347 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44348 // auto* found = FindPath("foo.bar");
349 Value* FindPath(StringPiece path);
350 const Value* FindPath(StringPiece path) const;
351
352 // There are also deprecated versions that take the path parameter
353 // as either a std::initializer_list<StringPiece> or a
354 // span<const StringPiece>. The latter is useful to use a
355 // std::vector<std::string> as a parameter but creates huge dynamic
jdoerriec209c7d2019-04-05 09:51:46356 // allocations and should be avoided!
357 // Note: If there is only one component in the path, use FindKey() instead.
358 //
David 'Digit' Turner43ce6492019-04-04 16:04:44359 // Example:
jdoerriecd022242017-08-23 08:38:27360 // std::vector<StringPiece> components = ...
361 // auto* found = FindPath(components);
362 Value* FindPath(std::initializer_list<StringPiece> path);
363 Value* FindPath(span<const StringPiece> path);
364 const Value* FindPath(std::initializer_list<StringPiece> path) const;
365 const Value* FindPath(span<const StringPiece> path) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27366
Lei Zhange0fc6f02017-10-27 00:27:23367 // Like FindPath() but will only return the value if the leaf Value type
Brett Wilsond16cf4ee2017-08-03 00:08:27368 // matches the given type. Will return nullptr otherwise.
David 'Digit' Turner43ce6492019-04-04 16:04:44369 // Note: Prefer Find<Type>Path() for simple values.
Lei Zhange0fc6f02017-10-27 00:27:23370 //
371 // Note: If there is only one component in the path, use FindKeyOfType()
David 'Digit' Turner43ce6492019-04-04 16:04:44372 // instead for slightly better performance.
373 Value* FindPathOfType(StringPiece path, Type type);
374 const Value* FindPathOfType(StringPiece path, Type type) const;
375
376 // Convenience accessors used when the expected type of a value is known.
377 // Similar to Find<Type>Key() but accepts paths instead of keys.
378 base::Optional<bool> FindBoolPath(StringPiece path) const;
379 base::Optional<int> FindIntPath(StringPiece path) const;
380 base::Optional<double> FindDoublePath(StringPiece path) const;
381 const std::string* FindStringPath(StringPiece path) const;
Dominic Battre08cbe972019-07-31 03:57:19382 std::string* FindStringPath(StringPiece path);
David 'Digit' Turner43ce6492019-04-04 16:04:44383 const BlobStorage* FindBlobPath(StringPiece path) const;
384 Value* FindDictPath(StringPiece path);
385 const Value* FindDictPath(StringPiece path) const;
386 Value* FindListPath(StringPiece path);
387 const Value* FindListPath(StringPiece path) const;
388
389 // The following forms are deprecated too, use the ones that take the path
390 // as a single StringPiece instead.
jdoerriecd022242017-08-23 08:38:27391 Value* FindPathOfType(std::initializer_list<StringPiece> path, Type type);
392 Value* FindPathOfType(span<const StringPiece> path, Type type);
393 const Value* FindPathOfType(std::initializer_list<StringPiece> path,
Brett Wilsond16cf4ee2017-08-03 00:08:27394 Type type) const;
jdoerriecd022242017-08-23 08:38:27395 const Value* FindPathOfType(span<const StringPiece> path, Type type) const;
Brett Wilsond16cf4ee2017-08-03 00:08:27396
397 // Sets the given path, expanding and creating dictionary keys as necessary.
398 //
jdoerrie64783162017-09-04 16:33:32399 // If the current value is not a dictionary, the function returns nullptr. If
400 // path components do not exist, they will be created. If any but the last
401 // components matches a value that is not a dictionary, the function will fail
402 // (it will not overwrite the value) and return nullptr. The last path
403 // component will be unconditionally overwritten if it exists, and created if
404 // it doesn't.
Brett Wilsond16cf4ee2017-08-03 00:08:27405 //
406 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44407 // value.SetPath("foo.bar", std::move(myvalue));
Lei Zhange0fc6f02017-10-27 00:27:23408 //
409 // Note: If there is only one component in the path, use SetKey() instead.
David 'Digit' Turner43ce6492019-04-04 16:04:44410 // Note: Using Set<Type>Path() might be more convenient and efficient.
411 Value* SetPath(StringPiece path, Value&& value);
412
413 // These setters are more convenient and efficient than the corresponding
414 // SetPath(...) call.
415 Value* SetBoolPath(StringPiece path, bool value);
416 Value* SetIntPath(StringPiece path, int value);
417 Value* SetDoublePath(StringPiece path, double value);
418 Value* SetStringPath(StringPiece path, StringPiece value);
419 Value* SetStringPath(StringPiece path, const char* value);
420 Value* SetStringPath(StringPiece path, std::string&& value);
421 Value* SetStringPath(StringPiece path, StringPiece16 value);
422
423 // Deprecated: use the ones that take a StringPiece path parameter instead.
Sergey Abbakumov0e1215882019-03-15 22:32:16424 Value* SetPath(std::initializer_list<StringPiece> path, Value&& value);
425 Value* SetPath(span<const StringPiece> path, Value&& value);
Brett Wilsond16cf4ee2017-08-03 00:08:27426
jdoerrie64783162017-09-04 16:33:32427 // Tries to remove a Value at the given path.
428 //
jdoerriec209c7d2019-04-05 09:51:46429 // If the current value is not a dictionary or any path component does not
jdoerrie64783162017-09-04 16:33:32430 // exist, this operation fails, leaves underlying Values untouched and returns
431 // |false|. In case intermediate dictionaries become empty as a result of this
432 // path removal, they will be removed as well.
jdoerriec209c7d2019-04-05 09:51:46433 // Note: If there is only one component in the path, use ExtractKey() instead.
jdoerrie64783162017-09-04 16:33:32434 //
435 // Example:
David 'Digit' Turner43ce6492019-04-04 16:04:44436 // bool success = value.RemovePath("foo.bar");
David 'Digit' Turner43ce6492019-04-04 16:04:44437 bool RemovePath(StringPiece path);
438
jdoerriec209c7d2019-04-05 09:51:46439 // Tries to extract a Value at the given path.
440 //
441 // If the current value is not a dictionary or any path component does not
442 // exist, this operation fails, leaves underlying Values untouched and returns
443 // nullopt. In case intermediate dictionaries become empty as a result of this
444 // path removal, they will be removed as well. Returns the extracted value on
445 // success.
446 // Note: If there is only one component in the path, use ExtractKey() instead.
447 //
448 // Example:
449 // Optional<Value> maybe_value = value.ExtractPath("foo.bar");
450 Optional<Value> ExtractPath(StringPiece path);
451
jdoerrie78ab7a22017-08-17 19:04:45452 using dict_iterator_proxy = detail::dict_iterator_proxy;
453 using const_dict_iterator_proxy = detail::const_dict_iterator_proxy;
jdoerrie44efa9d2017-07-14 14:47:20454
455 // |DictItems| returns a proxy object that exposes iterators to the underlying
456 // dictionary. These are intended for iteration over all items in the
457 // dictionary and are compatible with for-each loops and standard library
458 // algorithms.
David Van Cleve373381d2020-01-07 18:16:13459 //
460 // Unlike with std::map, a range-for over the non-const version of DictItems()
461 // will range over items of type pair<const std::string&, Value&>, so code of
462 // the form
463 // for (auto kv : my_value.DictItems())
464 // Mutate(kv.second);
465 // will actually alter |my_value| in place (if it isn't const).
466 //
Alexander Hendriche86ee512019-06-12 16:01:57467 // Note: These CHECK that type() is Type::DICTIONARY.
jdoerrie44efa9d2017-07-14 14:47:20468 dict_iterator_proxy DictItems();
469 const_dict_iterator_proxy DictItems() const;
470
Jan Wilken Dörrief961a372020-11-02 20:30:34471 // Transfers ownership of the underlying dict to the caller. Subsequent
472 // calls to DictItems() will return an empty dict.
473 // Note: This requires that type() is Type::DICTIONARY.
474 DictStorage TakeDict();
475
Panos Astithas0532a862020-10-29 04:15:07476 // Returns the size of the dictionary, if the dictionary is empty, and clears
477 // the dictionary. Note: These CHECK that type() is Type::DICTIONARY.
Lei Zhange823c512018-05-07 20:27:30478 size_t DictSize() const;
479 bool DictEmpty() const;
Panos Astithas0532a862020-10-29 04:15:07480 void DictClear();
Lei Zhange823c512018-05-07 20:27:30481
jdoerriec1091282018-08-01 17:28:12482 // Merge |dictionary| into this value. This is done recursively, i.e. any
483 // sub-dictionaries will be merged as well. In case of key collisions, the
484 // passed in dictionary takes precedence and data already present will be
485 // replaced. Values within |dictionary| are deep-copied, so |dictionary| may
486 // be freed any time after this call.
Jan Wilken Dörrie79d022142020-08-19 18:18:32487 // Note: This requires that type() and dictionary->type() is Type::DICTIONARY.
jdoerriec1091282018-08-01 17:28:12488 void MergeDictionary(const Value* dictionary);
489
[email protected]2f03f532013-07-17 08:43:33490 // These methods allow the convenient retrieval of the contents of the Value.
491 // If the current object can be converted into the given type, the value is
492 // returned through the |out_value| parameter and true is returned;
493 // otherwise, false is returned and |out_value| is unchanged.
jdoerried4b852612017-06-06 11:48:43494 // DEPRECATED, use GetBool() instead.
jdoerrie8e945542017-02-17 13:54:49495 bool GetAsBoolean(bool* out_value) const;
jdoerried4b852612017-06-06 11:48:43496 // DEPRECATED, use GetInt() instead.
jdoerrie8e945542017-02-17 13:54:49497 bool GetAsInteger(int* out_value) const;
jdoerried4b852612017-06-06 11:48:43498 // DEPRECATED, use GetDouble() instead.
jdoerrie8e945542017-02-17 13:54:49499 bool GetAsDouble(double* out_value) const;
jdoerried4b852612017-06-06 11:48:43500 // DEPRECATED, use GetString() instead.
jdoerrie8e945542017-02-17 13:54:49501 bool GetAsString(std::string* out_value) const;
502 bool GetAsString(string16* out_value) const;
jdoerrie122c4da2017-03-06 11:12:04503 bool GetAsString(const Value** out_value) const;
jdoerrie8e945542017-02-17 13:54:49504 bool GetAsString(StringPiece* out_value) const;
lukaszad1485da72016-11-01 21:49:46505 // ListValue::From is the equivalent for std::unique_ptr conversions.
jdoerried4b852612017-06-06 11:48:43506 // DEPRECATED, use GetList() instead.
jdoerrie8e945542017-02-17 13:54:49507 bool GetAsList(ListValue** out_value);
508 bool GetAsList(const ListValue** out_value) const;
lukaszad1485da72016-11-01 21:49:46509 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
jdoerrie8e945542017-02-17 13:54:49510 bool GetAsDictionary(DictionaryValue** out_value);
511 bool GetAsDictionary(const DictionaryValue** out_value) const;
[email protected]2f03f532013-07-17 08:43:33512 // Note: Do not add more types. See the file-level comment above for why.
initial.commitd7cae122008-07-26 21:49:38513
514 // This creates a deep copy of the entire Value tree, and returns a pointer
jdoerrie05eb3162017-02-01 10:36:56515 // to the copy. The caller gets ownership of the copy, of course.
[email protected]16f47e082011-01-18 02:16:59516 // Subclasses return their own type directly in their overrides;
517 // this works because C++ supports covariant return types.
jdoerriecc9f5732017-08-23 14:12:30518 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17519 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49520 Value* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30521 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43522 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51523 std::unique_ptr<Value> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38524
jdoerrie5c1cee112017-03-28 17:52:00525 // Comparison operators so that Values can easily be used with standard
526 // library algorithms and associative containers.
527 BASE_EXPORT friend bool operator==(const Value& lhs, const Value& rhs);
528 BASE_EXPORT friend bool operator!=(const Value& lhs, const Value& rhs);
529 BASE_EXPORT friend bool operator<(const Value& lhs, const Value& rhs);
530 BASE_EXPORT friend bool operator>(const Value& lhs, const Value& rhs);
531 BASE_EXPORT friend bool operator<=(const Value& lhs, const Value& rhs);
532 BASE_EXPORT friend bool operator>=(const Value& lhs, const Value& rhs);
533
initial.commitd7cae122008-07-26 21:49:38534 // Compares if two Value objects have equal contents.
jdoerrie5c1cee112017-03-28 17:52:00535 // DEPRECATED, use operator==(const Value& lhs, const Value& rhs) instead.
536 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49537 bool Equals(const Value* other) const;
initial.commitd7cae122008-07-26 21:49:38538
Eric Secklerf6c544f2020-06-02 10:49:21539 // Estimates dynamic memory usage. Requires tracing support
540 // (enable_base_tracing gn flag), otherwise always returns 0. See
541 // base/trace_event/memory_usage_estimator.h for more info.
Alexander Yashkinab504e72017-11-30 11:54:45542 size_t EstimateMemoryUsage() const;
543
jdoerrie8e945542017-02-17 13:54:49544 protected:
Jan Wilken Dörrie79d022142020-08-19 18:18:32545 // Checked convenience accessors for dict and list.
Jan Wilken Dörrief961a372020-11-02 20:30:34546 const LegacyDictStorage& dict() const {
547 return absl::get<LegacyDictStorage>(data_);
548 }
549 LegacyDictStorage& dict() { return absl::get<LegacyDictStorage>(data_); }
Jan Wilken Dörrie79d022142020-08-19 18:18:32550 const ListStorage& list() const { return absl::get<ListStorage>(data_); }
551 ListStorage& list() { return absl::get<ListStorage>(data_); }
552
Jan Wilken Dörrief961a372020-11-02 20:30:34553 // Internal constructors, allowing the simplify the implementation of Clone().
554 explicit Value(const LegacyDictStorage& storage);
555 explicit Value(LegacyDictStorage&& storage) noexcept;
556
Jan Wilken Dörrie79d022142020-08-19 18:18:32557 private:
David 'Digit' Turner2f287312019-04-03 14:32:09558 // Special case for doubles, which are aligned to 8 bytes on some
559 // 32-bit architectures. In this case, a simple declaration as a
560 // double member would make the whole union 8 byte-aligned, which
561 // would also force 4 bytes of wasted padding space before it in
562 // the Value layout.
David 'Digit' Turner806dedb82019-03-06 17:43:11563 //
David 'Digit' Turner2f287312019-04-03 14:32:09564 // To override this, store the value as an array of 32-bit integers, and
565 // perform the appropriate bit casts when reading / writing to it.
Jan Wilken Dörrie79d022142020-08-19 18:18:32566 using DoubleStorage = struct { alignas(4) char v[sizeof(double)]; };
David 'Digit' Turner2f287312019-04-03 14:32:09567
Jan Wilken Dörrie79d022142020-08-19 18:18:32568 // Internal constructors, allowing the simplify the implementation of Clone().
569 explicit Value(absl::monostate);
570 explicit Value(DoubleStorage storage);
jdoerrie8e945542017-02-17 13:54:49571
David 'Digit' Turner806dedb82019-03-06 17:43:11572 friend class ValuesTest_SizeOfValue_Test;
David 'Digit' Turner2f287312019-04-03 14:32:09573 double AsDoubleInternal() const;
jdoerriecc9f5732017-08-23 14:12:30574
David 'Digit' Turnere169e6c2019-03-28 22:06:29575 // NOTE: Using a movable reference here is done for performance (it avoids
576 // creating + moving + destroying a temporary unique ptr).
577 Value* SetKeyInternal(StringPiece key, std::unique_ptr<Value>&& val_ptr);
David 'Digit' Turner43ce6492019-04-04 16:04:44578 Value* SetPathInternal(StringPiece path, std::unique_ptr<Value>&& value_ptr);
David 'Digit' Turnere169e6c2019-03-28 22:06:29579
Jan Wilken Dörrie79d022142020-08-19 18:18:32580 absl::variant<absl::monostate,
581 bool,
582 int,
583 DoubleStorage,
584 std::string,
585 BlobStorage,
Jan Wilken Dörrief961a372020-11-02 20:30:34586 LegacyDictStorage,
Jan Wilken Dörrie79d022142020-08-19 18:18:32587 ListStorage>
588 data_;
initial.commitd7cae122008-07-26 21:49:38589};
590
[email protected]9e4cda7332010-07-31 04:56:14591// DictionaryValue provides a key-value dictionary with (optional) "path"
592// parsing for recursive access; see the comment at the top of the file. Keys
593// are |std::string|s and should be UTF-8 encoded.
[email protected]0bea7252011-08-05 15:34:00594class BASE_EXPORT DictionaryValue : public Value {
initial.commitd7cae122008-07-26 21:49:38595 public:
Jan Wilken Dörrief961a372020-11-02 20:30:34596 using const_iterator = LegacyDictStorage::const_iterator;
597 using iterator = LegacyDictStorage::iterator;
Johan Tibell71bba86c2017-05-17 05:21:12598
reillyg259c0a32015-09-11 00:25:54599 // Returns |value| if it is a dictionary, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51600 static std::unique_ptr<DictionaryValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54601
[email protected]3a3d47472010-07-15 21:03:54602 DictionaryValue();
Jan Wilken Dörrief961a372020-11-02 20:30:34603 explicit DictionaryValue(const LegacyDictStorage& in_dict);
604 explicit DictionaryValue(LegacyDictStorage&& in_dict) noexcept;
[email protected]5cf906f82011-11-26 01:11:44605
initial.commitd7cae122008-07-26 21:49:38606 // Returns true if the current dictionary has a value for the given key.
jdoerrie43ab3c02017-08-24 20:44:36607 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11608 bool HasKey(StringPiece key) const;
initial.commitd7cae122008-07-26 21:49:38609
[email protected]fb804132c2009-04-15 00:17:53610 // Returns the number of Values in this dictionary.
Jan Wilken Dörrie79d022142020-08-19 18:18:32611 size_t size() const { return dict().size(); }
[email protected]4dad9ad82009-11-25 20:47:52612
613 // Returns whether the dictionary is empty.
Jan Wilken Dörrie79d022142020-08-19 18:18:32614 bool empty() const { return dict().empty(); }
[email protected]fb804132c2009-04-15 00:17:53615
initial.commitd7cae122008-07-26 21:49:38616 // Clears any current contents of this dictionary.
Panos Astithas0532a862020-10-29 04:15:07617 // DEPRECATED, use Value::DictClear() instead.
[email protected]af5ed4a2008-08-04 13:56:25618 void Clear();
initial.commitd7cae122008-07-26 21:49:38619
620 // Sets the Value associated with the given path starting from this object.
621 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
622 // into the next DictionaryValue down. Obviously, "." can't be used
623 // within a key, but there are no other restrictions on keys.
624 // If the key at any step of the way doesn't exist, or exists but isn't
625 // a DictionaryValue, a new DictionaryValue will be created and attached
estadeca798482015-01-06 20:06:50626 // to the path in that location. |in_value| must be non-null.
jdoerrieb94e5422017-04-28 21:52:58627 // Returns a pointer to the inserted value.
jdoerrie43ab3c02017-08-24 20:44:36628 // DEPRECATED, use Value::SetPath(path, value) instead.
jdoerrieb94e5422017-04-28 21:52:58629 Value* Set(StringPiece path, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38630
631 // Convenience forms of Set(). These methods will replace any existing
632 // value at that path, even if it has a different type.
David 'Digit' Turner43ce6492019-04-04 16:04:44633 // DEPRECATED, use Value::SetBoolKey() or Value::SetBoolPath().
jdoerrieb94e5422017-04-28 21:52:58634 Value* SetBoolean(StringPiece path, bool in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44635 // DEPRECATED, use Value::SetIntPath().
jdoerrieb94e5422017-04-28 21:52:58636 Value* SetInteger(StringPiece path, int in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44637 // DEPRECATED, use Value::SetDoublePath().
jdoerrieb94e5422017-04-28 21:52:58638 Value* SetDouble(StringPiece path, double in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44639 // DEPRECATED, use Value::SetStringPath().
jdoerrieb94e5422017-04-28 21:52:58640 Value* SetString(StringPiece path, StringPiece in_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44641 // DEPRECATED, use Value::SetStringPath().
jdoerrieb94e5422017-04-28 21:52:58642 Value* SetString(StringPiece path, const string16& in_value);
Lei Zhange9c1bf22020-10-02 01:48:38643 // DEPRECATED, use Value::SetPath().
jdoerrieb94e5422017-04-28 21:52:58644 DictionaryValue* SetDictionary(StringPiece path,
645 std::unique_ptr<DictionaryValue> in_value);
Lei Zhange9c1bf22020-10-02 01:48:38646 // DEPRECATED, use Value::SetPath().
jdoerrieb94e5422017-04-28 21:52:58647 ListValue* SetList(StringPiece path, std::unique_ptr<ListValue> in_value);
[email protected]4dad9ad82009-11-25 20:47:52648
649 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
650 // be used as paths.
jdoerrie43ab3c02017-08-24 20:44:36651 // DEPRECATED, use Value::SetKey(key, value) instead.
jdoerrieb94e5422017-04-28 21:52:58652 Value* SetWithoutPathExpansion(StringPiece key,
653 std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38654
655 // Gets the Value associated with the given path starting from this object.
656 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
657 // into the next DictionaryValue down. If the path can be resolved
658 // successfully, the value for the last key in the path will be returned
[email protected]35213ce92010-04-08 19:06:15659 // through the |out_value| parameter, and the function will return true.
660 // Otherwise, it will return false and |out_value| will be untouched.
initial.commitd7cae122008-07-26 21:49:38661 // Note that the dictionary always owns the value that's returned.
[email protected]78c03a42014-03-09 07:13:23662 // |out_value| is optional and will only be set if non-NULL.
jdoerrie43ab3c02017-08-24 20:44:36663 // DEPRECATED, use Value::FindPath(path) instead.
asvitkinedbd26533e2015-06-23 18:22:52664 bool Get(StringPiece path, const Value** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36665 // DEPRECATED, use Value::FindPath(path) instead.
asvitkinedbd26533e2015-06-23 18:22:52666 bool Get(StringPiece path, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38667
668 // These are convenience forms of Get(). The value will be retrieved
669 // and the return value will be true if the path is valid and the value at
670 // the end of the path can be returned in the form specified.
[email protected]78c03a42014-03-09 07:13:23671 // |out_value| is optional and will only be set if non-NULL.
David 'Digit' Turner43ce6492019-04-04 16:04:44672 // DEPRECATED, use Value::FindBoolPath(path) instead.
dcheng16d6f532016-08-25 16:07:11673 bool GetBoolean(StringPiece path, bool* out_value) const;
Rainhard Findlingb01268b2020-03-12 17:45:02674 // DEPRECATED, use Value::FindIntPath(path) instead.
dcheng16d6f532016-08-25 16:07:11675 bool GetInteger(StringPiece path, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28676 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23677 // doubles.
David 'Digit' Turner43ce6492019-04-04 16:04:44678 // DEPRECATED, use Value::FindDoublePath(path).
dcheng16d6f532016-08-25 16:07:11679 bool GetDouble(StringPiece path, double* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44680 // DEPRECATED, use Value::FindStringPath(path) instead.
dcheng16d6f532016-08-25 16:07:11681 bool GetString(StringPiece path, std::string* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44682 // DEPRECATED, use Value::FindStringPath(path) instead.
dcheng16d6f532016-08-25 16:07:11683 bool GetString(StringPiece path, string16* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44684 // DEPRECATED, use Value::FindString(path) and IsAsciiString() instead.
dcheng16d6f532016-08-25 16:07:11685 bool GetStringASCII(StringPiece path, std::string* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44686 // DEPRECATED, use Value::FindBlobPath(path) instead.
jdoerrie14b25da2017-04-11 07:45:50687 bool GetBinary(StringPiece path, const Value** out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44688 // DEPRECATED, use Value::FindBlobPath(path) instead.
jdoerrie14b25da2017-04-11 07:45:50689 bool GetBinary(StringPiece path, Value** out_value);
jdoerrie43ab3c02017-08-24 20:44:36690 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32691 bool GetDictionary(StringPiece path, const DictionaryValue** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36692 // DEPRECATED, use Value::FindPath(path) and Value's Dictionary API instead.
asvitkinedbd26533e2015-06-23 18:22:52693 bool GetDictionary(StringPiece path, DictionaryValue** out_value);
jdoerrie43ab3c02017-08-24 20:44:36694 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11695 bool GetList(StringPiece path, const ListValue** out_value) const;
jdoerrie43ab3c02017-08-24 20:44:36696 // DEPRECATED, use Value::FindPath(path) and Value::GetList() instead.
dcheng16d6f532016-08-25 16:07:11697 bool GetList(StringPiece path, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38698
[email protected]4dad9ad82009-11-25 20:47:52699 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
700 // be used as paths.
jdoerrie1e4eeb82017-08-02 23:25:52701 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11702 bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
jdoerrie1e4eeb82017-08-02 23:25:52703 // DEPRECATED, use Value::FindKey(key) instead.
dcheng16d6f532016-08-25 16:07:11704 bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16705 // DEPRECATED, use Value::FindBoolKey(key) instead.
dcheng16d6f532016-08-25 16:07:11706 bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16707 // DEPRECATED, use Value::FindIntKey(key) instead.
dcheng16d6f532016-08-25 16:07:11708 bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16709 // DEPRECATED, use Value::FindDoubleKey(key) instead.
dcheng16d6f532016-08-25 16:07:11710 bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16711 // DEPRECATED, use Value::FindStringKey(key) instead.
dcheng16d6f532016-08-25 16:07:11712 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]4dad9ad82009-11-25 20:47:52713 std::string* out_value) const;
Vladislav Kuzkokov193a2ba2019-01-08 11:33:16714 // DEPRECATED, use Value::FindStringKey(key) and UTF8ToUTF16() instead.
dcheng16d6f532016-08-25 16:07:11715 bool GetStringWithoutPathExpansion(StringPiece key,
[email protected]698f7f42010-08-04 19:35:33716 string16* out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44717 // DEPRECATED, use Value::FindDictKey(key) instead.
[email protected]a61890e2012-07-27 22:27:11718 bool GetDictionaryWithoutPathExpansion(
dcheng16d6f532016-08-25 16:07:11719 StringPiece key,
[email protected]a61890e2012-07-27 22:27:11720 const DictionaryValue** out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44721 // DEPRECATED, use Value::FindDictKey(key) instead.
dcheng16d6f532016-08-25 16:07:11722 bool GetDictionaryWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11723 DictionaryValue** out_value);
David 'Digit' Turner43ce6492019-04-04 16:04:44724 // DEPRECATED, use Value::FindListKey(key) instead.
dcheng16d6f532016-08-25 16:07:11725 bool GetListWithoutPathExpansion(StringPiece key,
[email protected]a61890e2012-07-27 22:27:11726 const ListValue** out_value) const;
David 'Digit' Turner43ce6492019-04-04 16:04:44727 // DEPRECATED, use Value::FindListKey(key) instead.
dcheng16d6f532016-08-25 16:07:11728 bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
[email protected]4dad9ad82009-11-25 20:47:52729
initial.commitd7cae122008-07-26 21:49:38730 // Removes the Value with the specified path from this dictionary (or one
731 // of its child dictionaries, if the path is more than just a local key).
[email protected]d814a8852013-08-06 13:33:04732 // If |out_value| is non-NULL, the removed Value will be passed out via
733 // |out_value|. If |out_value| is NULL, the removed value will be deleted.
734 // This method returns true if |path| is a valid path; otherwise it will
735 // return false and the DictionaryValue object will be unchanged.
jdoerriec209c7d2019-04-05 09:51:46736 // DEPRECATED, use Value::RemovePath(path) or Value::ExtractPath(path)
737 // instead.
dcheng5f9cf762016-11-29 05:30:31738 bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38739
[email protected]4dad9ad82009-11-25 20:47:52740 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
741 // to be used as paths.
jdoerriec209c7d2019-04-05 09:51:46742 // DEPRECATED, use Value::RemoveKey(key) or Value::ExtractKey(key) instead.
jdoerrie8e945542017-02-17 13:54:49743 bool RemoveWithoutPathExpansion(StringPiece key,
744 std::unique_ptr<Value>* out_value);
[email protected]4dad9ad82009-11-25 20:47:52745
[email protected]aa3283392013-11-27 01:38:24746 // Removes a path, clearing out all dictionaries on |path| that remain empty
747 // after removing the value at |path|.
jdoerriec209c7d2019-04-05 09:51:46748 // DEPRECATED, use Value::RemovePath(path) or Value::ExtractPath(path)
749 // instead.
dcheng5f9cf762016-11-29 05:30:31750 bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
[email protected]aa3283392013-11-27 01:38:24751
jdoerrie64783162017-09-04 16:33:32752 using Value::RemovePath; // DictionaryValue::RemovePath shadows otherwise.
753
[email protected]ec330b52009-12-02 00:20:32754 // Makes a copy of |this| but doesn't include empty dictionaries and lists in
755 // the copy. This never returns NULL, even if |this| itself is empty.
dcheng093de9b2016-04-04 21:25:51756 std::unique_ptr<DictionaryValue> DeepCopyWithoutEmptyChildren() const;
[email protected]ec330b52009-12-02 00:20:32757
[email protected]ec5263a2011-05-10 09:23:39758 // Swaps contents with the |other| dictionary.
jdoerrie8e945542017-02-17 13:54:49759 void Swap(DictionaryValue* other);
[email protected]ec5263a2011-05-10 09:23:39760
[email protected]32c0e002011-11-08 21:26:41761 // This class provides an iterator over both keys and values in the
762 // dictionary. It can't be used to modify the dictionary.
jdoerrie43ab3c02017-08-24 20:44:36763 // DEPRECATED, use Value::DictItems() instead.
[email protected]a34cc092012-08-10 12:45:35764 class BASE_EXPORT Iterator {
[email protected]32c0e002011-11-08 21:26:41765 public:
[email protected]a34cc092012-08-10 12:45:35766 explicit Iterator(const DictionaryValue& target);
vmpstre65942b2016-02-25 00:50:31767 Iterator(const Iterator& other);
[email protected]a8d94b42013-12-10 18:52:22768 ~Iterator();
[email protected]32c0e002011-11-08 21:26:41769
Jan Wilken Dörrie79d022142020-08-19 18:18:32770 bool IsAtEnd() const { return it_ == target_.end(); }
[email protected]32c0e002011-11-08 21:26:41771 void Advance() { ++it_; }
772
773 const std::string& key() const { return it_->first; }
774 const Value& value() const { return *it_->second; }
775
776 private:
777 const DictionaryValue& target_;
Jan Wilken Dörrief961a372020-11-02 20:30:34778 LegacyDictStorage::const_iterator it_;
[email protected]32c0e002011-11-08 21:26:41779 };
780
Johan Tibell71bba86c2017-05-17 05:21:12781 // Iteration.
jdoerrie43ab3c02017-08-24 20:44:36782 // DEPRECATED, use Value::DictItems() instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32783 iterator begin() { return dict().begin(); }
784 iterator end() { return dict().end(); }
Johan Tibell71bba86c2017-05-17 05:21:12785
jdoerrie43ab3c02017-08-24 20:44:36786 // DEPRECATED, use Value::DictItems() instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32787 const_iterator begin() const { return dict().begin(); }
788 const_iterator end() const { return dict().end(); }
Johan Tibell71bba86c2017-05-17 05:21:12789
jdoerriecc9f5732017-08-23 14:12:30790 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17791 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49792 DictionaryValue* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30793 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43794 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51795 std::unique_ptr<DictionaryValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38796};
797
798// This type of Value represents a list of other Value values.
Jan Wilken Dörriecf4ce5522020-10-27 14:41:04799// DEPRECATED: Use std::vector<base::Value> instead.
[email protected]0bea7252011-08-05 15:34:00800class BASE_EXPORT ListValue : public Value {
initial.commitd7cae122008-07-26 21:49:38801 public:
Jan Wilken Dörrie46992f022020-02-20 11:25:47802 using const_iterator = ListView::const_iterator;
803 using iterator = ListView::iterator;
[email protected]a502bbe72011-01-07 18:06:45804
reillyg259c0a32015-09-11 00:25:54805 // Returns |value| if it is a list, nullptr otherwise.
dcheng093de9b2016-04-04 21:25:51806 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value);
reillyg259c0a32015-09-11 00:25:54807
[email protected]3a3d47472010-07-15 21:03:54808 ListValue();
Jan Wilken Dörrie53e009b2019-09-09 14:17:41809 explicit ListValue(span<const Value> in_list);
jdoerrie52939ed2017-04-26 18:19:42810 explicit ListValue(ListStorage&& in_list) noexcept;
initial.commitd7cae122008-07-26 21:49:38811
initial.commitd7cae122008-07-26 21:49:38812 // Clears the contents of this ListValue
Jan Wilken Dörrie02577a22019-12-04 14:27:39813 // DEPRECATED, use ClearList() instead.
initial.commitd7cae122008-07-26 21:49:38814 void Clear();
815
816 // Returns the number of Values in this list.
jdoerried4b852612017-06-06 11:48:43817 // DEPRECATED, use GetList()::size() instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32818 size_t GetSize() const { return list().size(); }
initial.commitd7cae122008-07-26 21:49:38819
[email protected]ec330b52009-12-02 00:20:32820 // Returns whether the list is empty.
jdoerried4b852612017-06-06 11:48:43821 // DEPRECATED, use GetList()::empty() instead.
Jan Wilken Dörrie79d022142020-08-19 18:18:32822 bool empty() const { return list().empty(); }
[email protected]ec330b52009-12-02 00:20:32823
initial.commitd7cae122008-07-26 21:49:38824 // Sets the list item at the given index to be the Value specified by
825 // the value given. If the index beyond the current end of the list, null
826 // Values will be used to pad out the list.
827 // Returns true if successful, or false if the index was negative or
828 // the value is a null pointer.
jdoerried4b852612017-06-06 11:48:43829 // DEPRECATED, use GetList()::operator[] instead.
dcheng093de9b2016-04-04 21:25:51830 bool Set(size_t index, std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38831
[email protected]35213ce92010-04-08 19:06:15832 // Gets the Value at the given index. Modifies |out_value| (and returns true)
initial.commitd7cae122008-07-26 21:49:38833 // only if the index falls within the current list range.
[email protected]35213ce92010-04-08 19:06:15834 // Note that the list always owns the Value passed out via |out_value|.
[email protected]78c03a42014-03-09 07:13:23835 // |out_value| is optional and will only be set if non-NULL.
jdoerried4b852612017-06-06 11:48:43836 // DEPRECATED, use GetList()::operator[] instead.
[email protected]5d30f92bf2012-08-03 08:43:37837 bool Get(size_t index, const Value** out_value) const;
838 bool Get(size_t index, Value** out_value);
initial.commitd7cae122008-07-26 21:49:38839
[email protected]35213ce92010-04-08 19:06:15840 // Convenience forms of Get(). Modifies |out_value| (and returns true)
841 // only if the index is valid and the Value at that index can be returned
842 // in the specified form.
[email protected]78c03a42014-03-09 07:13:23843 // |out_value| is optional and will only be set if non-NULL.
jdoerried4b852612017-06-06 11:48:43844 // DEPRECATED, use GetList()::operator[]::GetBool() instead.
[email protected]f82fb4952009-01-20 21:05:32845 bool GetBoolean(size_t index, bool* out_value) const;
jdoerried4b852612017-06-06 11:48:43846 // DEPRECATED, use GetList()::operator[]::GetInt() instead.
[email protected]f82fb4952009-01-20 21:05:32847 bool GetInteger(size_t index, int* out_value) const;
jdoerriedc72ee942016-12-07 15:43:28848 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
[email protected]78c03a42014-03-09 07:13:23849 // doubles.
jdoerried4b852612017-06-06 11:48:43850 // DEPRECATED, use GetList()::operator[]::GetDouble() instead.
[email protected]fb534c92011-02-01 01:02:07851 bool GetDouble(size_t index, double* out_value) const;
jdoerried4b852612017-06-06 11:48:43852 // DEPRECATED, use GetList()::operator[]::GetString() instead.
[email protected]f82fb4952009-01-20 21:05:32853 bool GetString(size_t index, std::string* out_value) const;
[email protected]698f7f42010-08-04 19:35:33854 bool GetString(size_t index, string16* out_value) const;
jdoerried4b852612017-06-06 11:48:43855
[email protected]5d30f92bf2012-08-03 08:43:37856 bool GetDictionary(size_t index, const DictionaryValue** out_value) const;
857 bool GetDictionary(size_t index, DictionaryValue** out_value);
jdoerrie52939ed2017-04-26 18:19:42858
859 using Value::GetList;
jdoerried4b852612017-06-06 11:48:43860 // DEPRECATED, use GetList()::operator[]::GetList() instead.
[email protected]5d30f92bf2012-08-03 08:43:37861 bool GetList(size_t index, const ListValue** out_value) const;
862 bool GetList(size_t index, ListValue** out_value);
initial.commitd7cae122008-07-26 21:49:38863
864 // Removes the Value with the specified index from this list.
865 // If |out_value| is non-NULL, the removed Value AND ITS OWNERSHIP will be
[email protected]b930d132009-01-05 18:37:51866 // passed out via |out_value|. If |out_value| is NULL, the removed value will
initial.commitd7cae122008-07-26 21:49:38867 // be deleted. This method returns true if |index| is valid; otherwise
868 // it will return false and the ListValue object will be unchanged.
jdoerried4b852612017-06-06 11:48:43869 // DEPRECATED, use GetList()::erase() instead.
jdoerrie8e945542017-02-17 13:54:49870 bool Remove(size_t index, std::unique_ptr<Value>* out_value);
initial.commitd7cae122008-07-26 21:49:38871
[email protected]6832cbe2009-11-30 19:59:11872 // Removes the first instance of |value| found in the list, if any, and
[email protected]4fc3c5642011-08-13 17:34:31873 // deletes it. |index| is the location where |value| was found. Returns false
874 // if not found.
jdoerried4b852612017-06-06 11:48:43875 // DEPRECATED, use GetList()::erase() instead.
[email protected]4fc3c5642011-08-13 17:34:31876 bool Remove(const Value& value, size_t* index);
[email protected]e7f5c6f2009-05-09 00:33:04877
[email protected]3cbe0812012-07-03 02:51:43878 // Removes the element at |iter|. If |out_value| is NULL, the value will be
879 // deleted, otherwise ownership of the value is passed back to the caller.
[email protected]a8d379cc2013-02-18 16:31:45880 // Returns an iterator pointing to the location of the element that
881 // followed the erased element.
jdoerried4b852612017-06-06 11:48:43882 // DEPRECATED, use GetList()::erase() instead.
dcheng093de9b2016-04-04 21:25:51883 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value);
[email protected]3cbe0812012-07-03 02:51:43884
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24885 using Value::Append;
initial.commitd7cae122008-07-26 21:49:38886 // Appends a Value to the end of the list.
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24887 // DEPRECATED, use Value::Append() instead.
dcheng093de9b2016-04-04 21:25:51888 void Append(std::unique_ptr<Value> in_value);
initial.commitd7cae122008-07-26 21:49:38889
[email protected]095812b2012-09-14 02:14:01890 // Convenience forms of Append.
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24891 // DEPRECATED, use Value::Append() instead.
[email protected]095812b2012-09-14 02:14:01892 void AppendBoolean(bool in_value);
893 void AppendInteger(int in_value);
894 void AppendDouble(double in_value);
dcheng16d6f532016-08-25 16:07:11895 void AppendString(StringPiece in_value);
[email protected]095812b2012-09-14 02:14:01896 void AppendString(const string16& in_value);
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24897 // DEPRECATED, use Value::Append() in a loop instead.
[email protected]095812b2012-09-14 02:14:01898 void AppendStrings(const std::vector<std::string>& in_values);
[email protected]095812b2012-09-14 02:14:01899
dcheng66c7a4c2016-09-14 05:49:58900 // Appends a Value if it's not already present. Returns true if successful,
901 // or false if the value was already
Jan Wilken Dörrie55b0b2b2019-09-10 05:40:24902 // DEPRECATED, use std::find() with Value::Append() instead.
dcheng66c7a4c2016-09-14 05:49:58903 bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
[email protected]840642202010-04-12 21:48:10904
Jan Wilken Dörrie9065545e12019-10-30 10:44:51905 using Value::Insert;
[email protected]86c008e82009-08-28 20:26:05906 // Insert a Value at index.
907 // Returns true if successful, or false if the index was out of range.
Jan Wilken Dörrie9065545e12019-10-30 10:44:51908 // DEPRECATED, use Value::Insert() instead.
dcheng66c7a4c2016-09-14 05:49:58909 bool Insert(size_t index, std::unique_ptr<Value> in_value);
[email protected]86c008e82009-08-28 20:26:05910
[email protected]5fb35372011-09-19 15:23:10911 // Searches for the first instance of |value| in the list using the Equals
912 // method of the Value type.
913 // Returns a const_iterator to the found item or to end() if none exists.
jdoerried4b852612017-06-06 11:48:43914 // DEPRECATED, use std::find() instead.
[email protected]5fb35372011-09-19 15:23:10915 const_iterator Find(const Value& value) const;
916
[email protected]8b8e7c92010-08-19 18:05:56917 // Swaps contents with the |other| list.
jdoerried4b852612017-06-06 11:48:43918 // DEPRECATED, use GetList()::swap() instead.
jdoerrie8e945542017-02-17 13:54:49919 void Swap(ListValue* other);
[email protected]8b8e7c92010-08-19 18:05:56920
[email protected]e8789192011-08-11 20:56:32921 // Iteration.
jdoerried4b852612017-06-06 11:48:43922 // DEPRECATED, use GetList()::begin() instead.
Jan Wilken Dörrie46992f022020-02-20 11:25:47923 iterator begin() { return GetList().begin(); }
jdoerried4b852612017-06-06 11:48:43924 // DEPRECATED, use GetList()::end() instead.
Jan Wilken Dörrie46992f022020-02-20 11:25:47925 iterator end() { return GetList().end(); }
initial.commitd7cae122008-07-26 21:49:38926
jdoerried4b852612017-06-06 11:48:43927 // DEPRECATED, use GetList()::begin() instead.
Jan Wilken Dörrie46992f022020-02-20 11:25:47928 const_iterator begin() const { return GetList().begin(); }
jdoerried4b852612017-06-06 11:48:43929 // DEPRECATED, use GetList()::end() instead.
Jan Wilken Dörrie46992f022020-02-20 11:25:47930 const_iterator end() const { return GetList().end(); }
initial.commitd7cae122008-07-26 21:49:38931
jdoerriecc9f5732017-08-23 14:12:30932 // DEPRECATED, use Value::Clone() instead.
jdoerriee964d9a2017-04-05 06:44:17933 // TODO(crbug.com/646113): Delete this and migrate callsites.
jdoerrie8e945542017-02-17 13:54:49934 ListValue* DeepCopy() const;
jdoerriecc9f5732017-08-23 14:12:30935 // DEPRECATED, use Value::Clone() instead.
jdoerried4b852612017-06-06 11:48:43936 // TODO(crbug.com/646113): Delete this and migrate callsites.
dcheng093de9b2016-04-04 21:25:51937 std::unique_ptr<ListValue> CreateDeepCopy() const;
initial.commitd7cae122008-07-26 21:49:38938};
939
prashhir54a994502015-03-05 09:30:57940// This interface is implemented by classes that know how to serialize
941// Value objects.
[email protected]0bea7252011-08-05 15:34:00942class BASE_EXPORT ValueSerializer {
initial.commitd7cae122008-07-26 21:49:38943 public:
[email protected]3a3d47472010-07-15 21:03:54944 virtual ~ValueSerializer();
[email protected]abb9d0c2008-08-06 15:46:59945
initial.commitd7cae122008-07-26 21:49:38946 virtual bool Serialize(const Value& root) = 0;
prashhir54a994502015-03-05 09:30:57947};
948
949// This interface is implemented by classes that know how to deserialize Value
950// objects.
951class BASE_EXPORT ValueDeserializer {
952 public:
953 virtual ~ValueDeserializer();
initial.commitd7cae122008-07-26 21:49:38954
955 // This method deserializes the subclass-specific format into a Value object.
[email protected]b4cebf82008-12-29 19:59:08956 // If the return value is non-NULL, the caller takes ownership of returned
Nigel Tao410788e2020-06-24 07:12:27957 // Value.
958 //
959 // If the return value is nullptr, and if |error_code| is non-nullptr,
960 // |*error_code| will be set to an integer value representing the underlying
961 // error. See "enum ErrorCode" below for more detail about the integer value.
962 //
963 // If |error_message| is non-nullptr, it will be filled in with a formatted
[email protected]ba399672010-04-06 15:42:39964 // error message including the location of the error if appropriate.
dcheng093de9b2016-04-04 21:25:51965 virtual std::unique_ptr<Value> Deserialize(int* error_code,
Nigel Tao410788e2020-06-24 07:12:27966 std::string* error_message) = 0;
967
968 // The integer-valued error codes form four groups:
969 // - The value 0 means no error.
970 // - Values between 1 and 999 inclusive mean an error in the data (i.e.
971 // content). The bytes being deserialized are not in the right format.
972 // - Values 1000 and above mean an error in the metadata (i.e. context). The
973 // file could not be read, the network is down, etc.
974 // - Negative values are reserved.
975 enum ErrorCode {
976 kErrorCodeNoError = 0,
977 // kErrorCodeInvalidFormat is a generic error code for "the data is not in
978 // the right format". Subclasses of ValueDeserializer may return other
979 // values for more specific errors.
980 kErrorCodeInvalidFormat = 1,
981 // kErrorCodeFirstMetadataError is the minimum value (inclusive) of the
982 // range of metadata errors.
983 kErrorCodeFirstMetadataError = 1000,
984 };
985
986 // The |error_code| argument can be one of the ErrorCode values, but it is
987 // not restricted to only being 0, 1 or 1000. Subclasses of ValueDeserializer
988 // can define their own error code values.
989 static inline bool ErrorCodeIsDataError(int error_code) {
990 return (kErrorCodeInvalidFormat <= error_code) &&
991 (error_code < kErrorCodeFirstMetadataError);
992 }
initial.commitd7cae122008-07-26 21:49:38993};
994
[email protected]ea0ec052013-04-16 09:04:02995// Stream operator so Values can be used in assertion statements. In order that
996// gtest uses this operator to print readable output on test failures, we must
997// override each specific type. Otherwise, the default template implementation
998// is preferred over an upcast.
[email protected]e4ef8312012-09-12 03:39:35999BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
1000
[email protected]ea0ec052013-04-16 09:04:021001BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
[email protected]ea0ec052013-04-16 09:04:021002 const DictionaryValue& value) {
1003 return out << static_cast<const Value&>(value);
1004}
1005
1006BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
1007 const ListValue& value) {
1008 return out << static_cast<const Value&>(value);
1009}
1010
jdoerriedc72ee942016-12-07 15:43:281011// Stream operator so that enum class Types can be used in log statements.
1012BASE_EXPORT std::ostream& operator<<(std::ostream& out,
1013 const Value::Type& type);
1014
[email protected]f3a1c642011-07-12 19:15:031015} // namespace base
1016
[email protected]101d5422008-09-26 20:22:421017#endif // BASE_VALUES_H_