LLVM 19.0.0git
Public Member Functions | List of all members
llvm::json::ObjectMapper Class Reference

Helper for mapping JSON objects onto protocol structs. More...

#include "llvm/Support/JSON.h"

Public Member Functions

 ObjectMapper (const Value &E, Path P)
 If O is not an object, this mapper is invalid and an error is reported.
 
 operator bool () const
 True if the expression is an object.
 
template<typename T >
bool map (StringLiteral Prop, T &Out)
 Maps a property to a field.
 
template<typename T >
bool map (StringLiteral Prop, std::optional< T > &Out)
 Maps a property to a field, if it exists.
 
template<typename T >
bool mapOptional (StringLiteral Prop, T &Out)
 Maps a property to a field, if it exists.
 

Detailed Description

Helper for mapping JSON objects onto protocol structs.

Example:

bool fromJSON(const Value &E, MyStruct &R, Path P) {
ObjectMapper O(E, P);
// When returning false, error details were already reported.
return O && O.map("mandatory_field", R.MandatoryField) &&
O.mapOptional("optional_field", R.OptionalField);
}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Helper for mapping JSON objects onto protocol structs.
Definition: JSON.h:840
A "cursor" marking a position within a Value.
Definition: JSON.h:653
A Value is an JSON value of unknown type.
Definition: JSON.h:288
bool fromJSON(const Value &E, std::string &Out, Path P)
Definition: JSON.h:729

Definition at line 840 of file JSON.h.

Constructor & Destructor Documentation

◆ ObjectMapper()

llvm::json::ObjectMapper::ObjectMapper ( const Value E,
Path  P 
)
inline

If O is not an object, this mapper is invalid and an error is reported.

Definition at line 843 of file JSON.h.

References P.

Member Function Documentation

◆ map() [1/2]

template<typename T >
bool llvm::json::ObjectMapper::map ( StringLiteral  Prop,
std::optional< T > &  Out 
)
inline

Maps a property to a field, if it exists.

If the property exists and is invalid, reports an error. (Optional requires special handling, because missing keys are OK).

Definition at line 865 of file JSON.h.

References assert(), E, llvm::json::Path::field(), llvm::json::fromJSON(), and llvm::json::Object::get().

◆ map() [2/2]

template<typename T >
bool llvm::json::ObjectMapper::map ( StringLiteral  Prop,
T Out 
)
inline

Maps a property to a field.

If the property is missing or invalid, reports an error.

Definition at line 854 of file JSON.h.

References assert(), E, llvm::json::Path::field(), llvm::json::fromJSON(), llvm::json::Object::get(), and llvm::json::Path::report().

Referenced by llvm::getTensorSpecFromJSON().

◆ mapOptional()

template<typename T >
bool llvm::json::ObjectMapper::mapOptional ( StringLiteral  Prop,
T Out 
)
inline

Maps a property to a field, if it exists.

If the property exists and is invalid, reports an error. If the property does not exist, Out is unchanged.

Definition at line 876 of file JSON.h.

References assert(), E, llvm::json::Path::field(), llvm::json::fromJSON(), and llvm::json::Object::get().

◆ operator bool()

llvm::json::ObjectMapper::operator bool ( ) const
inline

True if the expression is an object.

Must be checked before calling map().

Definition at line 850 of file JSON.h.


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