ezconfig
Create C++ Objects from Yaml and Json
Loading...
Searching...
No Matches
json.hpp File Reference

Json factory. More...

#include <nlohmann/json.hpp>
#include "factory.hpp"
#include "json_fwd.hpp"
Include dependency graph for json.hpp:

Go to the source code of this file.

Concepts

concept  ezconfig::json::JsonParseable
 

Macros

#define EZ_JSON_DEFINE(Base)
 Define a global json factory for a base class.
 
#define EZ_JSON_REGISTER(Base, tag, Derived, ...)
 Register a tagged conversion with a json factory.
 

Functions

template<typename Base , typename Derived , typename Intermediate = Derived>
requires ( std::is_base_of_v<Base, Derived> && JsonParseable<Intermediate> && std::is_constructible_v<Derived, Intermediate &&>)
void ezconfig::json::Add (const std::string &tag)
 Add a factory method.
 
template<typename Base >
std::unique_ptr< Base > ezconfig::json::Create (const nlohmann::json &j)
 Create an object using the factory.
 

Detailed Description

Json factory.

Include this file in implementation file for the base class and any derived classes.

Definition in file json.hpp.

Macro Definition Documentation

◆ EZ_JSON_DEFINE

#define EZ_JSON_DEFINE ( Base)
Value:
EZ_FACTORY_DEFINE(Base, const nlohmann::json &); \
template std::unique_ptr<Base> ezconfig::json::Create<Base>(const nlohmann::json &); \
template struct nlohmann::adl_serializer<std::shared_ptr<Base>>; \
template struct nlohmann::adl_serializer<std::unique_ptr<Base>>

Define a global json factory for a base class.

Do this in the base class implementation file.

Parameters
Basefactory base class.

Example: Define a MyBase yaml factory.

#define EZ_YAML_DEFINE(Base)
Define a global yaml factory for a base class.
Definition yaml.hpp:29

Definition at line 29 of file json.hpp.

◆ EZ_JSON_REGISTER

#define EZ_JSON_REGISTER ( Base,
tag,
Derived,
... )
Value:
EZ_STATIC_INVOKE(&ezconfig::json::Add<Base, Derived __VA_OPT__(, ) __VA_ARGS__>, tag)

Register a tagged conversion with a json factory.

Do this in the implementation files for derived classes.

Parameters
Basefactory base class.
tagconversion identifier (string).
Derivedfactory derived class.
Intermediateoptional intermediate class.
Note
Intermediate can be omitted. In that case Derived must be json-parseable.
If provided, Intermediate must be a json-parsable type.

Example: Register a creator for MyDerived with tag "mytag".

EZ_JSON_REGISTER(MyBase, "mytag", MyDerived, MyDerivedConfig);
#define EZ_JSON_REGISTER(Base, tag, Derived,...)
Register a tagged conversion with a json factory.
Definition json.hpp:53

Definition at line 53 of file json.hpp.

Function Documentation

◆ Add()

template<typename Base , typename Derived , typename Intermediate = Derived>
requires ( std::is_base_of_v<Base, Derived> && JsonParseable<Intermediate> && std::is_constructible_v<Derived, Intermediate &&>)
void ezconfig::json::Add ( const std::string & tag)

Add a factory method.

Template Parameters
Derivedsub-class of Base.
Intermediatetype that is parseable from json, and that can construct Derived.

Adds conversions from yaml of the form

!tag object

to Derived, where object is yaml-converted to Intermediate.

Definition at line 82 of file json.hpp.

◆ Create()

template<typename Base >
std::unique_ptr< Base > ezconfig::json::Create ( const nlohmann::json & j)

Create an object using the factory.

Template Parameters
Basefactory base class
Parameters
jjson data
auto obj = JsonCreate<MyBase>(nlohmann::json::parse(data));

Definition at line 89 of file json.hpp.