class Value
Declaration
class Value { /* full declaration omitted */ };
Description
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 (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 (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 - 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 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);
Declared at: llvm/include/llvm/Support/JSON.h:282
Member Variables
- private llvm::json::Value::ValueType Type
- private llvm::AlignedCharArrayUnion<bool, double, int64_t, uint64_t, llvm::StringRef, std::string, json::Array, json::Object> Union
Method Overview
- public Value(const char * V)
- public template <typename Elt> Value(const std::vector<Elt> & C)
- public template <typename Elt> Value(const std::map<std::string, Elt> & C)
- public template <typename T, typename = std::enable_if_t<std::is_same<T, bool>::value>, bool = false> Value(T B)
- public template <typename T, typename = std::enable_if_t<std::is_same<T, uint64_t>::value>, bool = false, bool = false> Value(T V)
- public template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>, typename = std::enable_if_t<!std::is_same<T, bool>::value>, typename = std::enable_if_t<!std::is_same<T, uint64_t>::value>> Value(T I)
- public template <typename T, typename = std::enable_if_t<std::is_floating_point<T>::value>, double * = nullptr> Value(T D)
- public template <typename T, typename = std::enable_if_t<std::is_same<Value, decltype(toJSON(* (const T *)nullptr))>::value>, llvm::json::Value * = nullptr> Value(const T & V)
- public Value(const llvm::json::Value & M)
- public Value(std::nullptr_t)
- public Value(llvm::StringRef V)
- public Value(const llvm::formatv_object_base & V)
- public Value(const llvm::SmallVectorImpl<char> & V)
- public Value(std::string V)
- public Value(json::Object && Properties)
- public Value(json::Array && Elements)
- public Value(std::initializer_list<Value> Elements)
- public Value(llvm::json::Value && M)
- private template <typename T>T & as() const
- private void copyFrom(const llvm::json::Value & M)
- private template <typename T, typename... U>void create(U &&... V)
- private void destroy()
- public const json::Array * getAsArray() const
- public json::Array * getAsArray()
- public llvm::Optional<bool> getAsBoolean() const
- public llvm::Optional<int64_t> getAsInteger() const
- public llvm::Optional<std::nullptr_t> getAsNull() const
- public llvm::Optional<double> getAsNumber() const
- public const json::Object * getAsObject() const
- public json::Object * getAsObject()
- public llvm::Optional<llvm::StringRef> getAsString() const
- public llvm::Optional<uint64_t> getAsUINT64() const
- public llvm::json::Value::Kind kind() const
- private void moveFrom(const llvm::json::Value && M)
- public ~Value()
Methods
¶Value(const char* V)
Value(const char* V)
Declared at: llvm/include/llvm/Support/JSON.h:328
Parameters
- const char* V
¶template <typename Elt>
Value(const std::vector<Elt>& C)
template <typename Elt>
Value(const std::vector<Elt>& C)
Declared at: llvm/include/llvm/Support/JSON.h:303
Templates
- Elt
Parameters
- const std::vector<Elt>& C
¶template <typename Elt>
Value(const std::map<std::string, Elt>& C)
template <typename Elt>
Value(const std::map<std::string, Elt>& C)
Declared at: llvm/include/llvm/Support/JSON.h:308
Templates
- Elt
Parameters
- const std::map<std::string, Elt>& C
¶template <typename T,
typename = std::enable_if_t<
std::is_same<T, bool>::value>,
bool = false>
Value(T B)
template <typename T,
typename = std::enable_if_t<
std::is_same<T, bool>::value>,
bool = false>
Value(T B)
Declared at: llvm/include/llvm/Support/JSON.h:335
Templates
- T
- = std::enable_if_t<std::is_same<T, bool>::value>
- bool = false
Parameters
- T B
¶template <typename T,
typename = std::enable_if_t<
std::is_same<T, uint64_t>::value>,
bool = false,
bool = false>
Value(T V)
template <typename T,
typename = std::enable_if_t<
std::is_same<T, uint64_t>::value>,
bool = false,
bool = false>
Value(T V)
Declared at: llvm/include/llvm/Support/JSON.h:343
Templates
- T
- = std::enable_if_t<std::is_same<T, uint64_t>::value>
- bool = false
- bool = false
Parameters
- T V
¶template <typename T,
typename = std::enable_if_t<
std::is_integral<T>::value>,
typename = std::enable_if_t<
!std::is_same<T, bool>::value>,
typename = std::enable_if_t<
!std::is_same<T, uint64_t>::value>>
Value(T I)
template <typename T,
typename = std::enable_if_t<
std::is_integral<T>::value>,
typename = std::enable_if_t<
!std::is_same<T, bool>::value>,
typename = std::enable_if_t<
!std::is_same<T, uint64_t>::value>>
Value(T I)
Declared at: llvm/include/llvm/Support/JSON.h:352
Templates
- T
- = std::enable_if_t<std::is_integral<T>::value>
- = std::enable_if_t<!std::is_same<T, bool>::value>
- = std::enable_if_t<!std::is_same<T, uint64_t>::value>
Parameters
- T I
¶template <typename T,
typename = std::enable_if_t<
std::is_floating_point<T>::value>,
double* = nullptr>
Value(T D)
template <typename T,
typename = std::enable_if_t<
std::is_floating_point<T>::value>,
double* = nullptr>
Value(T D)
Declared at: llvm/include/llvm/Support/JSON.h:359
Templates
- T
- = std::enable_if_t<std::is_floating_point<T>::value>
- double * = nullptr
Parameters
- T D
¶template <
typename T,
typename = std::enable_if_t<std::is_same<
Value,
decltype(toJSON(
*(const T*)nullptr))>::value>,
llvm::json::Value* = nullptr>
Value(const T& V)
template <
typename T,
typename = std::enable_if_t<std::is_same<
Value,
decltype(toJSON(
*(const T*)nullptr))>::value>,
llvm::json::Value* = nullptr>
Value(const T& V)
Declared at: llvm/include/llvm/Support/JSON.h:367
Templates
- T
- = std::enable_if_t<std::is_same<Value, decltype(toJSON(* (const T *)nullptr))>::value>
- llvm::json::Value * = nullptr
Parameters
- const T& V
¶Value(const llvm::json::Value& M)
Value(const llvm::json::Value& M)
Declared at: llvm/include/llvm/Support/JSON.h:296
Parameters
- const llvm::json::Value& M
¶Value(std::nullptr_t)
Value(std::nullptr_t)
Declared at: llvm/include/llvm/Support/JSON.h:329
Parameters
¶Value(llvm::StringRef V)
Value(llvm::StringRef V)
Declared at: llvm/include/llvm/Support/JSON.h:321
Parameters
¶Value(const llvm::formatv_object_base& V)
Value(const llvm::formatv_object_base& V)
Declared at: llvm/include/llvm/Support/JSON.h:319
Parameters
- const llvm::formatv_object_base& V
¶Value(const llvm::SmallVectorImpl<char>& V)
Value(const llvm::SmallVectorImpl<char>& V)
Declared at: llvm/include/llvm/Support/JSON.h:317
Parameters
- const llvm::SmallVectorImpl<char>& V
¶Value(std::string V)
Value(std::string V)
Declared at: llvm/include/llvm/Support/JSON.h:310
Parameters
¶Value(json::Object&& Properties)
Value(json::Object&& Properties)
Declared at: llvm/include/llvm/Support/JSON.h:304
Parameters
- json::Object&& Properties
¶Value(json::Array&& Elements)
Value(json::Array&& Elements)
Declared at: llvm/include/llvm/Support/JSON.h:299
Parameters
- json::Array&& Elements
¶Value(std::initializer_list<Value> Elements)
Value(std::initializer_list<Value> Elements)
Declared at: llvm/include/llvm/Support/JSON.h:298
Parameters
- std::initializer_list<Value> Elements
¶Value(llvm::json::Value&& M)
Value(llvm::json::Value&& M)
Declared at: llvm/include/llvm/Support/JSON.h:297
Parameters
¶template <typename T>
T& as() const
template <typename T>
T& as() const
Declared at: llvm/include/llvm/Support/JSON.h:478
Templates
- T
¶void copyFrom(const llvm::json::Value& M)
void copyFrom(const llvm::json::Value& M)
Declared at: llvm/include/llvm/Support/JSON.h:467
Parameters
- const llvm::json::Value& M
¶template <typename T, typename... U>
void create(U&&... V)
template <typename T, typename... U>
void create(U&&... V)
Declared at: llvm/include/llvm/Support/JSON.h:475
Templates
- T
- U
Parameters
- U&&... V
¶void destroy()
void destroy()
Declared at: llvm/include/llvm/Support/JSON.h:466
¶const json::Array* getAsArray() const
const json::Array* getAsArray() const
Declared at: llvm/include/llvm/Support/JSON.h:458
¶json::Array* getAsArray()
json::Array* getAsArray()
Declared at: llvm/include/llvm/Support/JSON.h:461
¶llvm::Optional<bool> getAsBoolean() const
llvm::Optional<bool> getAsBoolean() const
Declared at: llvm/include/llvm/Support/JSON.h:408
¶llvm::Optional<int64_t> getAsInteger() const
llvm::Optional<int64_t> getAsInteger() const
Declared at: llvm/include/llvm/Support/JSON.h:423
¶llvm::Optional<std::nullptr_t> getAsNull() const
llvm::Optional<std::nullptr_t> getAsNull() const
Declared at: llvm/include/llvm/Support/JSON.h:403
¶llvm::Optional<double> getAsNumber() const
llvm::Optional<double> getAsNumber() const
Declared at: llvm/include/llvm/Support/JSON.h:413
¶const json::Object* getAsObject() const
const json::Object* getAsObject() const
Declared at: llvm/include/llvm/Support/JSON.h:452
¶json::Object* getAsObject()
json::Object* getAsObject()
Declared at: llvm/include/llvm/Support/JSON.h:455
¶llvm::Optional<llvm::StringRef> getAsString()
const
llvm::Optional<llvm::StringRef> getAsString()
const
Declared at: llvm/include/llvm/Support/JSON.h:445
¶llvm::Optional<uint64_t> getAsUINT64() const
llvm::Optional<uint64_t> getAsUINT64() const
Declared at: llvm/include/llvm/Support/JSON.h:435
¶llvm::json::Value::Kind kind() const
llvm::json::Value::Kind kind() const
Declared at: llvm/include/llvm/Support/JSON.h:381
¶void moveFrom(const llvm::json::Value&& M)
void moveFrom(const llvm::json::Value&& M)
Declared at: llvm/include/llvm/Support/JSON.h:471
Parameters
- const llvm::json::Value&& M
¶~Value()
~Value()
Declared at: llvm/include/llvm/Support/JSON.h:379