|
| Value (const Value &M) |
|
| Value (Value &&M) |
|
| Value (std::initializer_list< Value > Elements) |
|
| Value (json::Array &&Elements) |
|
template<typename Elt > |
| Value (const std::vector< Elt > &C) |
|
| Value (json::Object &&Properties) |
|
template<typename Elt > |
| Value (const std::map< std::string, Elt > &C) |
|
| Value (std::string V) |
|
| Value (const llvm::SmallVectorImpl< char > &V) |
|
| Value (const llvm::formatv_object_base &V) |
|
| Value (StringRef V) |
|
| Value (const char *V) |
|
| Value (std::nullptr_t) |
|
template<typename T , typename = std::enable_if_t<std::is_same_v<T, bool>>, bool = false> |
| Value (T B) |
|
template<typename T , typename = std::enable_if_t<is_uint_64_bit_v<T>>> |
| Value (T V) |
|
template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>, typename = std::enable_if_t<!std::is_same_v<T, bool>>, typename = std::enable_if_t<!is_uint_64_bit_v<T>>> |
| Value (T I) |
|
template<typename T , typename = std::enable_if_t<std::is_floating_point_v<T>>, double * = nullptr> |
| Value (T D) |
|
template<typename T , typename = std::enable_if_t< std::is_same_v<Value, decltype(toJSON(*(const T *)nullptr))>>, Value * = nullptr> |
| Value (const T &V) |
|
Value & | operator= (const Value &M) |
|
Value & | operator= (Value &&M) |
|
| ~Value () |
|
Kind | kind () const |
|
std::optional< std::nullptr_t > | getAsNull () const |
|
std::optional< bool > | getAsBoolean () const |
|
std::optional< double > | getAsNumber () const |
|
std::optional< int64_t > | getAsInteger () const |
|
std::optional< uint64_t > | getAsUINT64 () const |
|
std::optional< llvm::StringRef > | getAsString () const |
|
const json::Object * | getAsObject () const |
|
json::Object * | getAsObject () |
|
const json::Array * | getAsArray () const |
|
json::Array * | getAsArray () |
|
A Value is an JSON value of unknown type.
They can be copied, but should generally be moved.
=== Composing values ===
You can implicitly construct Values from:
- strings: std::string, SmallString, formatv, StringRef, char* (char*, and StringRef are references, not copies!)
- numbers
- booleans
- null: nullptr
- arrays: {"foo", 42.0, false}
- serializable things: types with toJSON(const T&)->Value, found by ADL
They can also be constructed from object/array helpers:
- json::Object is a type like map<ObjectKey, Value>
- json::Array is a type like vector<Value> These can be list-initialized, or used to build up collections in a loop. json::ary(Collection) converts all items in a collection to Values.
=== Inspecting values ===
Each Value is one of the JSON kinds: null (nullptr_t) boolean (bool) number (double, int64 or uint64) string (StringRef) array (json::Array) object (json::Object)
The kind can be queried directly, or implicitly via the typed accessors: if (std::optional<StringRef> S = E.getAsString() assert(E.kind() == Value::String);
Array and Object also have typed indexing accessors for easy traversal: Expected<Value> E = parse(R"( {"options": {"font": "sans-serif"}} )"); if (Object* O = E->getAsObject()) if (Object* Opts = O->getObject("options")) if (std::optional<StringRef> Font = Opts->getString("font")) assert(Opts->at("font").kind() == Value::String);
=== Converting JSON values to C++ types ===
The convention is to have a deserializer function findable via ADL: fromJSON(const json::Value&, T&, Path) -> bool
The return value indicates overall success, and Path is used for precise error reporting. (The Path::Root passed in at the top level fromJSON call captures any nested error and can render it in context). If conversion fails, fromJSON calls Path::report() and immediately returns. This ensures that the first fatal error survives.
Deserializers are provided for:
- bool
- int and int64_t
- double
- std::string
- vector<T>, where T is deserializable
- map<string, T>, where T is deserializable
- std::optional<T>, where T is deserializable ObjectMapper can help writing fromJSON() functions for object types.
For conversion in the other direction, the serializer function is: toJSON(const T&) -> json::Value If this exists, then it also allows constructing Value from T, and can be used to serialize vector<T>, map<string, T>, and std::optional<T>.
=== Serialization ===
Values can be serialized to JSON: 1) raw_ostream << Value // Basic formatting. 2) raw_ostream << formatv("{0}", Value) // Basic formatting. 3) raw_ostream << formatv("{0:2}", Value) // Pretty-print with indent 2.
And parsed: Expected<Value> E = json::parse("[1, 2, null]"); assert(E && E->kind() == Value::Array);
Definition at line 288 of file JSON.h.