AFLOW
 
Loading...
Searching...
No Matches
aurostd_xserialization.h
Go to the documentation of this file.
1// ***************************************************************************
2// * *
3// * Aflow STEFANO CURTAROLO - Duke University 2003-2025 *
4// * *
5// ***************************************************************************
6
7#ifndef AFLOW_XSERIALIZATION_H
8#define AFLOW_XSERIALIZATION_H
9
10#include <filesystem>
11#include <string>
12
13#include "aurostd_xfile.h"
14
15namespace aurostd::JSON {
16 struct object;
17}
18
47template <typename T /* <: */> class JsonSerializable {
49
50public:
51 virtual ~JsonSerializable() = default;
52
53 [[nodiscard]] aurostd::JSON::object dumpToJson() const;
54 [[nodiscard]] std::string dumpToString() const;
55 void dumpToFile(const std::filesystem::path &path, aurostd::compression_type compression = aurostd::compression_type::None) const;
56
58 static T loadFromFile(const std::filesystem::path &path);
59 static T loadFromString(const std::string &content);
60
61protected:
63 virtual T deserialize(const aurostd::JSON::object &jo) = 0;
65 [[nodiscard]] virtual aurostd::JSON::object serialize() const = 0;
67 [[nodiscard]] virtual std::string getJsonID() const = 0;
68
69private:
70 [[nodiscard]] aurostd::JSON::object dumpJsonMeta() const;
71 void checkClassID(const aurostd::JSON::object &jo) const;
72};
73
74#include "aurostd_xserialization.tpp" // NOLINT / template implementation
75
76#endif // AFLOW_XSERIALIZATION_H
Interface for Json serialization functionality using CRTP.
void checkClassID(const aurostd::JSON::object &jo) const
static T loadFromString(const std::string &content)
static T loadFromJson(const aurostd::JSON::object &jo)
static T loadFromFile(const std::filesystem::path &path)
virtual ~JsonSerializable()=default
void dumpToFile(const std::filesystem::path &path, aurostd::compression_type compression=aurostd::compression_type::None) const
aurostd::JSON::object dumpToJson() const
aurostd::JSON::object dumpJsonMeta() const
std::string dumpToString() const
virtual T deserialize(const aurostd::JSON::object &jo)=0
Performs the actual deserialization of members.
virtual std::string getJsonID() const =0
Returns the string ID of the class.
virtual aurostd::JSON::object serialize() const =0
Performs the actual serialization of members.
unified namespace to read and write JSON
storge container for a JSON object