LLVM: llvm::json::Value Class Reference (original) (raw)
A Value is an JSON value of unknown type. More...
#include "[llvm/Support/JSON.h](JSON%5F8h%5Fsource.html)"
| Public Types | |
|---|---|
| enum | Kind { Null, Boolean, Number, String, Array, Object } |
| Public Member Functions | |
|---|---|
| Value (const Value &M) | |
| Value (Value &&M) | |
| LLVM_ABI | Value (std::initializer_list< Value > Elements) |
| Value (json::Array &&Elements) | |
| template | |
| Value (const std::vector< Elt > &C) | |
| Value (json::Object &&Properties) | |
| template | |
| 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>> | |
| Value (T V) | |
| template<typename T, typename = std::enable_if_t<std::is_integral_v>, typename = std::enable_if_t<!std::is_same_v<T, bool>>, typename = std::enable_if_t<!is_uint_64_bit_v>> | |
| Value (T I) | |
| template<typename T, typename = std::enable_if_t<std::is_floating_point_v>, 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 () |
| LLVM_ABI void | print (llvm::raw_ostream &OS) const |
| LLVM_DUMP_METHOD void | dump () const |
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 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 S = E.getAsString() assert(E.kind() == Value::String);
Array and Object also have typed indexing accessors for easy traversal: Expected E = parse(R"( {"options": {"font": "sans-serif"}} )"); if (Object* O = E->getAsObject()) if (Object* Opts = O->getObject("options")) if (std::optional 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](classllvm%5F1%5F1json%5F1%5F1Path.html "A "cursor" marking a position within a Value.") 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, where T is deserializable
- map<string, T>, where T is deserializable
- std::optional, 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, map<string, T>, and std::optional.
=== 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 E = json::parse("[1, 2, null]"); assert(E && E->kind() == Value::Array);
Definition at line 291 of file JSON.h.
◆ Kind
| Enumerator | |
|---|---|
| Null | |
| Boolean | |
| Number | Number values can store both int64s and doubles at full precision, depending on what they were constructed/parsed from. |
| String | |
| Array | |
| Object |
Definition at line 293 of file JSON.h.
◆ Value() [1/18]
| llvm::json::Value::Value ( const Value & M) | inline |
|---|
Definition at line 305 of file JSON.h.
References Value().
Referenced by operator=(), operator=(), operator==, Value(), Value(), Value(), Value(), Value(), Value(), Value(), and Value().
◆ Value() [2/18]
| llvm::json::Value::Value ( Value && M) | inline |
|---|
◆ Value() [3/18]
| llvm::json::Value::Value | ( | std::initializer_list< Value > | Elements | ) |
|---|
◆ Value() [4/18]
◆ Value() [5/18]
template
| llvm::json::Value::Value ( const std::vector< Elt > & C) | inline |
|---|
◆ Value() [6/18]
◆ Value() [7/18]
template
| llvm::json::Value::Value ( const std::map< std::string, Elt > & C) | inline |
|---|
◆ Value() [8/18]
| llvm::json::Value::Value ( std::string V) | inline |
|---|
◆ Value() [9/18]
◆ Value() [10/18]
◆ Value() [11/18]
| llvm::json::Value::Value ( StringRef V) | inline |
|---|
◆ Value() [12/18]
| llvm::json::Value::Value ( const char * V) | inline |
|---|
◆ Value() [13/18]
| llvm::json::Value::Value ( std::nullptr_t ) | inline |
|---|
Definition at line 338 of file JSON.h.
◆ Value() [14/18]
template<typename T, typename = std::enable_if_t<std::is_same_v<T, bool>>, bool = false>
| llvm::json::Value::Value ( T B) | inline |
|---|
◆ Value() [15/18]
template<typename T, typename = std::enable_if_t<is_uint_64_bit_v>>
| llvm::json::Value::Value ( T V) | inline |
|---|
Definition at line 349 of file JSON.h.
References T.
◆ Value() [16/18]
template<typename T, typename = std::enable_if_t<std::is_integral_v>, typename = std::enable_if_t<!std::is_same_v<T, bool>>, typename = std::enable_if_t<!is_uint_64_bit_v>>
| llvm::json::Value::Value ( T I) | inline |
|---|
Definition at line 358 of file JSON.h.
◆ Value() [17/18]
template<typename T, typename = std::enable_if_t<std::is_floating_point_v>, double * = nullptr>
| llvm::json::Value::Value ( T D) | inline |
|---|
◆ Value() [18/18]
template<typename T, typename = std::enable_if_t< std::is_same_v<Value, decltype(toJSON(*(const T *)nullptr))>>, Value * = nullptr>
| llvm::json::Value::Value ( const T & V) | inline |
|---|
Definition at line 373 of file JSON.h.
References T.
| llvm::json::Value::~Value ( ) | inline |
|---|
Definition at line 385 of file JSON.h.
◆ dump()
◆ getAsArray() [1/2]
◆ getAsArray() [2/2]
◆ getAsBoolean()
| std::optional< bool > llvm::json::Value::getAsBoolean ( ) const | inline |
|---|
◆ getAsInteger()
| std::optional< int64_t > llvm::json::Value::getAsInteger ( ) const | inline |
|---|
◆ getAsNull()
| std::optional< std::nullptr_t > llvm::json::Value::getAsNull ( ) const | inline |
|---|
◆ getAsNumber()
| std::optional< double > llvm::json::Value::getAsNumber ( ) const | inline |
|---|
◆ getAsObject() [1/2]
◆ getAsObject() [2/2]
◆ getAsString()
| std::optional< llvm::StringRef > llvm::json::Value::getAsString ( ) const | inline |
|---|
◆ getAsUINT64()
| std::optional< uint64_t > llvm::json::Value::getAsUINT64 ( ) const | inline |
|---|
◆ kind()
| Kind llvm::json::Value::kind ( ) const | inline |
|---|
◆ operator=() [1/2]
◆ operator=() [2/2]
| Value & llvm::json::Value::operator= ( Value && M) | inline |
|---|
◆ print()
◆ Array
◆ Object
◆ operator==
◆ OStream
The documentation for this class was generated from the following files: