AFLOW
 
Loading...
Searching...
No Matches
JsonSerializable< T > Class Template Referenceabstract

Interface for Json serialization functionality using CRTP. More...

#include <aurostd_xserialization.h>

Public Member Functions

virtual ~JsonSerializable ()=default
 
aurostd::JSON::object dumpToJson () const
 
std::string dumpToString () const
 
void dumpToFile (const std::filesystem::path &path, aurostd::compression_type compression=aurostd::compression_type::None) const
 

Static Public Member Functions

static T loadFromJson (const aurostd::JSON::object &jo)
 
static T loadFromFile (const std::filesystem::path &path)
 
static T loadFromString (const std::string &content)
 

Protected Member Functions

virtual T deserialize (const aurostd::JSON::object &jo)=0
 Performs the actual deserialization of members.
 
virtual aurostd::JSON::object serialize () const =0
 Performs the actual serialization of members.
 
virtual std::string getJsonID () const =0
 Returns the string ID of the class.
 

Private Member Functions

aurostd::JSON::object dumpJsonMeta () const
 
void checkClassID (const aurostd::JSON::object &jo) const
 

Detailed Description

template<typename T>
class JsonSerializable< T >

Interface for Json serialization functionality using CRTP.

Template Parameters
TThe serializable type and inheriting class

Inheriting this class provides json serialization capabilities for reading/writing json objects/strings/files. This class should be inherited with public inheritance in order for it to be of use. This class uses CRTP (Curiously Recurring Template Pattern) https://en.cppreference.com/w/cpp/language/crtp} in order to have static methods which act as constructors for the derived (inheriting) class. Inheriting this class will require the override implementation of three protected methods to serialize, deserialize, and check identity.

Requirements on the templated class T:

  1. Must be default constructible.
  2. Must inherit this class.

The implemntation of serialize/deserialize should write/read the members you wish to serialize to/from aurostd::JSON:object. Implementation of the third ID method should return a string ID for the class, typically the class name. See MockSerializableMember and MockSerializableMain for example implementations.

Note: It may be useful to use a macro to define a comma separated list of the members the class will serialize. If defined, this macro should have the name JSON_<class ID/class name>_MEMBERS. This macro would then be passed to the AST_JSON_SETTER and AST_JSON_GETTER macros to generate assignments for deserialization and a dictionary/map for serialization, respectively. Again, see MockSerializableMain for an example. These macros will only work if the type is supported for cast conversion by aurostd::JSON::object class, which includes many numeric, list-like, and dictionary-like types as well as others inheriting JsonSerializable.

Authors
20250108 | Scott Thiel - created
20250315 | Scott Thiel - slightly modify interface for clearer contract on inheriters

Definition at line 47 of file aurostd_xserialization.h.

Constructor & Destructor Documentation

◆ ~JsonSerializable()

template<typename T>
virtual JsonSerializable< T >::~JsonSerializable ( )
virtualdefault

Member Function Documentation

◆ dumpToJson()

template<typename T>
aurostd::JSON::object JsonSerializable< T >::dumpToJson ( ) const
nodiscard

◆ dumpToString()

template<typename T>
std::string JsonSerializable< T >::dumpToString ( ) const
nodiscard

◆ dumpToFile()

template<typename T>
void JsonSerializable< T >::dumpToFile ( const std::filesystem::path & path,
aurostd::compression_type compression = aurostd::compression_type::None ) const

◆ loadFromJson()

template<typename T>
static T JsonSerializable< T >::loadFromJson ( const aurostd::JSON::object & jo)
static

◆ loadFromFile()

template<typename T>
static T JsonSerializable< T >::loadFromFile ( const std::filesystem::path & path)
static

◆ loadFromString()

template<typename T>
static T JsonSerializable< T >::loadFromString ( const std::string & content)
static

◆ deserialize()

template<typename T>
virtual T JsonSerializable< T >::deserialize ( const aurostd::JSON::object & jo)
protectedpure virtual

Performs the actual deserialization of members.

Implemented in xDOSCAR, xEIGENVAL, xIBZKPT, xKPOINTS, xOUTCAR, xPLASMONICS, xPOTCAR, xQMVASP, and xVASPRUNXML.

◆ serialize()

template<typename T>
virtual aurostd::JSON::object JsonSerializable< T >::serialize ( ) const
nodiscardprotectedpure virtual

Performs the actual serialization of members.

Implemented in xDOSCAR, xEIGENVAL, xIBZKPT, xKPOINTS, xOUTCAR, xPLASMONICS, xPOTCAR, xQMVASP, and xVASPRUNXML.

◆ getJsonID()

template<typename T>
virtual std::string JsonSerializable< T >::getJsonID ( ) const
nodiscardprotectedpure virtual

Returns the string ID of the class.

Implemented in xDOSCAR, xEIGENVAL, xIBZKPT, xKPOINTS, xOUTCAR, xPLASMONICS, xPOTCAR, xQMVASP, and xVASPRUNXML.

◆ dumpJsonMeta()

template<typename T>
aurostd::JSON::object JsonSerializable< T >::dumpJsonMeta ( ) const
nodiscardprivate

◆ checkClassID()

template<typename T>
void JsonSerializable< T >::checkClassID ( const aurostd::JSON::object & jo) const
private

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