MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::IO::JSONSerializer Class Reference

Converts arbitrary C++ types to/from JSON strings and disk files. More...

#include <JSONSerializer.hpp>

+ Collaboration diagram for MayaFlux::IO::JSONSerializer:

Public Member Functions

template<typename T >
std::optional< Tdecode (const std::string &str)
 Parse str and deserialize into T.
 
template<typename T >
std::string encode (const T &value, int indent=2)
 Serialize value to a JSON string.
 
 JSONSerializer ()=default
 
 JSONSerializer (const JSONSerializer &)=delete
 
 JSONSerializer (JSONSerializer &&)=default
 
const std::string & last_error () const
 Last error message, empty if no error.
 
JSONSerializeroperator= (const JSONSerializer &)=delete
 
JSONSerializeroperator= (JSONSerializer &&)=default
 
template<typename T >
std::optional< Tread (const std::string &path)
 Read path and deserialize into T.
 
template<typename T >
bool write (const std::string &path, const T &value, int indent=2)
 Encode value and write to path (created or truncated).
 

Static Private Member Functions

template<typename T >
requires GlmSerializable<T>
static void decode_glm (const nlohmann::json &j, T &out)
 
template<typename Class , typename T >
static void decode_property (const nlohmann::json &j, Class &obj, const OptionalProperty< Class, T > &prop)
 
template<typename Class , typename T >
static void decode_property (const nlohmann::json &j, Class &obj, const Property< Class, T > &prop)
 
template<typename T >
requires GlmSerializable<T>
static nlohmann::json encode_glm (const T &v)
 
template<typename Class , typename T >
static void encode_property (nlohmann::json &j, const Class &obj, const OptionalProperty< Class, T > &prop)
 
template<typename Class , typename T >
static void encode_property (nlohmann::json &j, const Class &obj, const Property< Class, T > &prop)
 
template<typename T >
static void from_json (const nlohmann::json &j, T &out)
 
template<typename T >
static nlohmann::json to_json (const T &val)
 

Private Attributes

std::string m_last_error
 

Detailed Description

Converts arbitrary C++ types to/from JSON strings and disk files.

Encoding and decoding are driven by the Reflectable concept: any type that provides a static constexpr describe() returning a tuple of Property / OptionalProperty descriptors is handled recursively. Types without a describe() fall through to nlohmann's native converters (arithmetic, std::string, bool) or to the built-in dispatchers for std::vector, std::optional, std::unordered_map / std::map, and glm vec/mat types.

Callers never touch nlohmann::json directly. JSONSerializer owns all knowledge of the wire format.

File operations are built on top of the in-memory encode / decode pair. last_error() is set on any failure; it is cleared at the start of every fallible call.

Definition at line 31 of file JSONSerializer.hpp.


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