LLVM  13.0.0git
Public Types | Public Member Functions | Friends | List of all members
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)
 
 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<T, bool>::value>, bool = false>
 Value (T B)
 
template<typename T , typename = std::enable_if_t<std::is_integral<T>::value>, typename = std::enable_if_t<!std::is_same<T, bool>::value>>
 Value (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_same< Value, decltype(toJSON(*(const T *)nullptr))>::value>, Value * = nullptr>
 Value (const T &V)
 
Valueoperator= (const Value &M)
 
Valueoperator= (Value &&M)
 
 ~Value ()
 
Kind kind () const
 
llvm::Optional< std::nullptr_t > getAsNull () const
 
llvm::Optional< bool > getAsBoolean () const
 
llvm::Optional< doublegetAsNumber () const
 
llvm::Optional< int64_t > getAsInteger () const
 
llvm::Optional< llvm::StringRefgetAsString () const
 
const json::ObjectgetAsObject () const
 
json::ObjectgetAsObject ()
 
const json::ArraygetAsArray () const
 
json::ArraygetAsArray ()
 

Friends

class Array
 
class Object
 
class OStream
 
bool operator== (const Value &, const Value &)
 

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:

They can also be constructed from object/array helpers:

=== Inspecting values ===

Each Value is one of the JSON kinds: null (nullptr_t) boolean (bool) number (double or int64) 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:

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);

Definition at line 289 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 291 of file JSON.h.

Constructor & Destructor Documentation

◆ Value() [1/17]

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

Definition at line 303 of file JSON.h.

References M.

Referenced by Value().

◆ Value() [2/17]

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

Definition at line 304 of file JSON.h.

References M, and move.

◆ Value() [3/17]

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

Definition at line 102 of file JSON.cpp.

◆ Value() [4/17]

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

Definition at line 306 of file JSON.h.

References move.

◆ Value() [5/17]

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

Definition at line 310 of file JSON.h.

◆ Value() [6/17]

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

Definition at line 311 of file JSON.h.

References move.

◆ Value() [7/17]

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

Definition at line 315 of file JSON.h.

◆ Value() [8/17]

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

Definition at line 317 of file JSON.h.

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

◆ Value() [9/17]

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

Definition at line 324 of file JSON.h.

References string.

◆ Value() [10/17]

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

Definition at line 326 of file JSON.h.

◆ Value() [11/17]

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

Definition at line 328 of file JSON.h.

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

◆ Value() [12/17]

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

Definition at line 335 of file JSON.h.

◆ Value() [13/17]

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

Definition at line 336 of file JSON.h.

◆ Value() [14/17]

template<typename T , typename = std::enable_if_t<std::is_same<T, bool>::value>, bool = false>
llvm::json::Value::Value ( B)
inline

Definition at line 342 of file JSON.h.

References B.

◆ Value() [15/17]

template<typename T , typename = std::enable_if_t<std::is_integral<T>::value>, typename = std::enable_if_t<!std::is_same<T, bool>::value>>
llvm::json::Value::Value ( I)
inline

Definition at line 348 of file JSON.h.

References I.

◆ Value() [16/17]

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

Definition at line 355 of file JSON.h.

References D.

◆ Value() [17/17]

template<typename T , typename = std::enable_if_t<std::is_same< Value, decltype(toJSON(*(const T *)nullptr))>::value>, Value * = nullptr>
llvm::json::Value::Value ( const T &  V)
inline

Definition at line 363 of file JSON.h.

◆ ~Value()

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

Definition at line 375 of file JSON.h.

Member Function Documentation

◆ getAsArray() [1/2]

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

Definition at line 444 of file JSON.h.

References LLVM_LIKELY.

◆ getAsArray() [2/2]

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

◆ getAsBoolean()

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

Definition at line 403 of file JSON.h.

References LLVM_LIKELY, and llvm::None.

Referenced by llvm::json::operator==(), and llvm::json::OStream::value().

◆ getAsInteger()

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

Definition at line 416 of file JSON.h.

References D, LLVM_LIKELY, llvm::max(), llvm::min(), and llvm::None.

Referenced by llvm::json::operator==(), and llvm::json::OStream::value().

◆ getAsNull()

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

Definition at line 398 of file JSON.h.

References LLVM_LIKELY, and llvm::None.

Referenced by llvm::json::operator==().

◆ getAsNumber()

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

Definition at line 408 of file JSON.h.

References LLVM_LIKELY, and llvm::None.

Referenced by llvm::json::operator==(), and llvm::json::OStream::value().

◆ getAsObject() [1/2]

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

Definition at line 438 of file JSON.h.

References LLVM_LIKELY.

◆ getAsObject() [2/2]

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

◆ getAsString()

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

Definition at line 428 of file JSON.h.

References LLVM_LIKELY, and llvm::None.

Referenced by llvm::json::operator==(), and llvm::json::OStream::value().

◆ kind()

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

Definition at line 377 of file JSON.h.

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

Referenced by llvm::json::operator==(), and llvm::json::OStream::value().

◆ operator=() [1/2]

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

Definition at line 365 of file JSON.h.

References M.

◆ operator=() [2/2]

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

Definition at line 370 of file JSON.h.

References M, and move.

Friends And Related Function Documentation

◆ Array

friend class Array
friend

Definition at line 455 of file JSON.h.

Referenced by kind(), llvm::json::operator==(), and llvm::json::OStream::value().

◆ Object

friend class Object
friend

Definition at line 456 of file JSON.h.

Referenced by kind(), llvm::json::operator==(), and llvm::json::OStream::value().

◆ operator==

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

Definition at line 178 of file JSON.cpp.

◆ OStream

friend class OStream
friend

Definition at line 468 of file JSON.h.


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