LLVM 22.0.0git
llvm::json::Value Class Reference

A Value is an JSON value of unknown type. More...

#include "llvm/Support/JSON.h"

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<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)
Valueoperator= (const Value &M)
Valueoperator= (Value &&M)
 ~Value ()
Kind kind () const
std::optional< std::nullptr_t > getAsNull () const
std::optional< boolgetAsBoolean () const
std::optional< double > getAsNumber () const
std::optional< int64_t > getAsInteger () const
std::optional< uint64_tgetAsUINT64 () const
std::optional< llvm::StringRefgetAsString () const
const json::ObjectgetAsObject () const
json::ObjectgetAsObject ()
const json::ArraygetAsArray () const
json::ArraygetAsArray ()
LLVM_ABI void print (llvm::raw_ostream &OS) const
LLVM_DUMP_METHOD void dump () const

Friends

class Array
class Object
class OStream
LLVM_ABI friend bool operator== (const Value &L, const Value &R)

Detailed 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 (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 290 of file JSON.h.

Member Enumeration Documentation

◆ 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 292 of file JSON.h.

Constructor & Destructor Documentation

◆ Value() [1/18]

llvm::json::Value::Value ( const Value & M)
inline

Definition at line 304 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

Definition at line 305 of file JSON.h.

References Value().

◆ Value() [3/18]

llvm::json::Value::Value ( std::initializer_list< Value > Elements)

Definition at line 97 of file JSON.cpp.

References Array, and Value().

◆ Value() [4/18]

llvm::json::Value::Value ( json::Array && Elements)
inline

Definition at line 307 of file JSON.h.

◆ Value() [5/18]

template<typename Elt>
llvm::json::Value::Value ( const std::vector< Elt > & C)
inline

Definition at line 311 of file JSON.h.

References Array, llvm::CallingConv::C, and Value().

◆ Value() [6/18]

llvm::json::Value::Value ( json::Object && Properties)
inline

Definition at line 312 of file JSON.h.

◆ Value() [7/18]

template<typename Elt>
llvm::json::Value::Value ( const std::map< std::string, Elt > & C)
inline

Definition at line 316 of file JSON.h.

References llvm::CallingConv::C, Object, and Value().

◆ Value() [8/18]

llvm::json::Value::Value ( std::string V)
inline

Definition at line 318 of file JSON.h.

References assert(), llvm::json::fixUTF8(), llvm::json::isUTF8(), and LLVM_UNLIKELY.

◆ Value() [9/18]

llvm::json::Value::Value ( const llvm::SmallVectorImpl< char > & V)
inline

Definition at line 325 of file JSON.h.

References Value().

◆ Value() [10/18]

llvm::json::Value::Value ( const llvm::formatv_object_base & V)
inline

Definition at line 327 of file JSON.h.

References Value().

◆ Value() [11/18]

llvm::json::Value::Value ( StringRef V)
inline

Definition at line 329 of file JSON.h.

References assert(), llvm::json::fixUTF8(), llvm::json::isUTF8(), LLVM_UNLIKELY, and llvm::Value.

◆ Value() [12/18]

llvm::json::Value::Value ( const char * V)
inline

Definition at line 336 of file JSON.h.

References Value().

◆ Value() [13/18]

llvm::json::Value::Value ( std::nullptr_t )
inline

Definition at line 337 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

Definition at line 342 of file JSON.h.

References B(), and T.

◆ Value() [15/18]

template<typename T, typename = std::enable_if_t<is_uint_64_bit_v<T>>>
llvm::json::Value::Value ( T V)
inline

Definition at line 348 of file JSON.h.

References T.

◆ Value() [16/18]

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>>>
llvm::json::Value::Value ( T I)
inline

Definition at line 357 of file JSON.h.

References I, and T.

◆ Value() [17/18]

template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>, double * = nullptr>
llvm::json::Value::Value ( T D)
inline

Definition at line 364 of file JSON.h.

References D(), and T.

◆ 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 372 of file JSON.h.

References T.

◆ ~Value()

llvm::json::Value::~Value ( )
inline

Definition at line 384 of file JSON.h.

Member Function Documentation

◆ dump()

LLVM_DUMP_METHOD void llvm::json::Value::dump ( ) const
inline

Definition at line 479 of file JSON.h.

References llvm::dbgs(), LLVM_DUMP_METHOD, and print().

◆ getAsArray() [1/2]

json::Array * llvm::json::Value::getAsArray ( )
inline

Definition at line 473 of file JSON.h.

References LLVM_LIKELY.

◆ getAsArray() [2/2]

const json::Array * llvm::json::Value::getAsArray ( ) const
inline

Definition at line 470 of file JSON.h.

References LLVM_LIKELY.

Referenced by llvm::mustache::ASTNode::render().

◆ getAsBoolean()

std::optional< bool > llvm::json::Value::getAsBoolean ( ) const
inline

Definition at line 414 of file JSON.h.

References LLVM_LIKELY.

◆ getAsInteger()

std::optional< int64_t > llvm::json::Value::getAsInteger ( ) const
inline

Definition at line 429 of file JSON.h.

References D(), and LLVM_LIKELY.

◆ getAsNull()

std::optional< std::nullptr_t > llvm::json::Value::getAsNull ( ) const
inline

Definition at line 409 of file JSON.h.

References LLVM_LIKELY.

◆ getAsNumber()

std::optional< double > llvm::json::Value::getAsNumber ( ) const
inline

Definition at line 419 of file JSON.h.

References LLVM_LIKELY.

◆ getAsObject() [1/2]

json::Object * llvm::json::Value::getAsObject ( )
inline

Definition at line 467 of file JSON.h.

References LLVM_LIKELY.

◆ getAsObject() [2/2]

const json::Object * llvm::json::Value::getAsObject ( ) const
inline

Definition at line 464 of file JSON.h.

References LLVM_LIKELY.

Referenced by mapOptOrNull().

◆ getAsString()

std::optional< llvm::StringRef > llvm::json::Value::getAsString ( ) const
inline

Definition at line 457 of file JSON.h.

References LLVM_LIKELY.

Referenced by readPropertyValueFromJSON().

◆ getAsUINT64()

std::optional< uint64_t > llvm::json::Value::getAsUINT64 ( ) const
inline

Definition at line 447 of file JSON.h.

References N.

Referenced by readPropertyValueFromJSON().

◆ kind()

Kind llvm::json::Value::kind ( ) const
inline

Definition at line 386 of file JSON.h.

References Array, Boolean, llvm_unreachable, Null, Number, Object, and String.

◆ operator=() [1/2]

Value & llvm::json::Value::operator= ( const Value & M)
inline

Definition at line 374 of file JSON.h.

References Value().

◆ operator=() [2/2]

Value & llvm::json::Value::operator= ( Value && M)
inline

Definition at line 379 of file JSON.h.

References Value().

◆ print()

void llvm::json::Value::print ( llvm::raw_ostream & OS) const

Definition at line 176 of file JSON.cpp.

Referenced by dump().

◆ Array

◆ Object

◆ operator==

LLVM_ABI friend bool operator== ( const Value & L,
const Value & R )
friend

Definition at line 178 of file JSON.cpp.

References Array, Boolean, llvm_unreachable, Null, Number, Object, String, and Value().

◆ OStream

friend class OStream
friend

Definition at line 517 of file JSON.h.

References OStream.

Referenced by OStream.


The documentation for this class was generated from the following files: