LLVM  14.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. More...
 
 operator bool () const
 True if the expression is an object. More...
 
template<typename T >
bool map (StringLiteral Prop, T &Out)
 Maps a property to a field. More...
 
template<typename T >
bool map (StringLiteral Prop, llvm::Optional< T > &Out)
 Maps a property to a field, if it exists. More...
 
template<typename T >
bool mapOptional (StringLiteral Prop, T &Out)
 Maps a property to a field, if it exists. More...
 

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

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

References P.

Member Function Documentation

◆ map() [1/2]

template<typename T >
bool llvm::json::ObjectMapper::map ( StringLiteral  Prop,
llvm::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 808 of file JSON.h.

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

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

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

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


The documentation for this class was generated from the following file:
llvm::json::ObjectMapper::ObjectMapper
ObjectMapper(const Value &E, Path P)
If O is not an object, this mapper is invalid and an error is reported.
Definition: JSON.h:786
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:198
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::json::fromJSON
bool fromJSON(const Value &E, std::string &Out, Path P)
Definition: JSON.h:672
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27