vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
Vkm::Engine::ReflectJson Namespace Reference

A reflected struct to JSON and back, field by field, by its VKM_REFLECT names. More...

Classes

struct  OwnFile
 Plain data in a project's own file: a field of the wrong type keeps its value and warns. More...
struct  PlainData
 The context for plain data in a scene, which names no assets: a number, string, list or struct of the wrong type throws, for the load to report. More...

Functions

std::string childPath (const char *parent, const std::string &step)
 Where step stands below parent, the steps joined by dots: "levels.2.mesh".
template<typename V, typename Ctx>
nlohmann::json valueToJson (const V &val, const Ctx &ctx)
 One value to JSON, whatever kind of value it is: a handle its name, a vector an array, a reflected struct an object, anything else a leaf.
template<typename V, typename Ctx>
void valueFromJson (const nlohmann::json &j, V &val, const Ctx &ctx, const char *field)
 The same in the other direction.
template<typename T>
nlohmann::json toJson (const T &obj)
 A reflected struct of plain data as a JSON object, a key a field.
template<typename T>
void fromJson (const nlohmann::json &j, T &obj)
 The fields j has written into obj; any it lacks, or holds wrongly, kept.

Detailed Description

A reflected struct to JSON and back, field by field, by its VKM_REFLECT names.

What the scene file is written with, and what a project keeps its own files with - its settings, a save. Leaves, scoped enums by their VKM_ENUM_NAMES, nested reflected structs and vectors of any of them go through; a field missing from the JSON keeps its value, so a file written before a field was added still loads. The context decides the rest: a Handle needs one that turns it into its asset's name and back (ComponentSerializer's), and a field holding the wrong type of value is an error in a scene - the load reports it, naming where it stood - but in a project's own file keeps its value and warns (OwnFile, which toJson and fromJson use). A glm value of the wrong shape warns and keeps its own in either.

Function Documentation

◆ childPath()

std::string Vkm::Engine::ReflectJson::childPath ( const char * parent,
const std::string & step )
inline

Where step stands below parent, the steps joined by dots: "levels.2.mesh".

Parameters
parentPath of the enclosing value; null or empty at the top.
stepA field's key, or an element's index.
Returns
The joined path.

◆ valueFromJson()

template<typename V, typename Ctx>
void Vkm::Engine::ReflectJson::valueFromJson ( const nlohmann::json & j,
V & val,
const Ctx & ctx,
const char * field )

The same in the other direction.

Parameters
fieldWhere the value is stored, as childPath joins it, which a handle's context may keep an unresolved name under.

◆ toJson()

template<typename T>
nlohmann::json Vkm::Engine::ReflectJson::toJson ( const T & obj)

A reflected struct of plain data as a JSON object, a key a field.

Parameters
objThe struct.
Returns
Its fields.

◆ fromJson()

template<typename T>
void Vkm::Engine::ReflectJson::fromJson ( const nlohmann::json & j,
T & obj )

The fields j has written into obj; any it lacks, or holds wrongly, kept.

Parameters
jAn object, as toJson writes; anything else changes nothing.
objThe struct.