![]() |
Shamrock 2025.10.0
Astrophysical Code
|
Base interface for types that can be serialized to and from JSON. More...
#include <shamsolvergraph/include/shamsolvergraph/JsonSerializable.hpp>
Public Member Functions | |
| void | to_json (nlohmann::json &j) const |
| Write this object to JSON, including the "type" discriminator. | |
| virtual std::string | type_name () const =0 |
| Return the type discriminator string for this class. | |
Static Public Member Functions | |
| static std::unique_ptr< JsonSerializable > | from_json (const nlohmann::json &j) |
| Reconstruct a registered type from JSON. | |
Protected Member Functions | |
| virtual void | _impl_to_json (nlohmann::json &j) const =0 |
| Write derived-class fields into a JSON object. | |
Base interface for types that can be serialized to and from JSON.
Derived types must implement _impl_to_json and type_name, and also provide a static from_json that returns an instance of the derived type (enforced by JsonDeserializable when registering).
The JSON object used for polymorphic deserialization must contain a "type" field whose value matches the name passed to JsonSerializable_registry::register_type and the string returned by type_name(). Public to_json sets this field automatically after _impl_to_json.
Definition at line 72 of file JsonSerializable.hpp.
|
protectedpure virtual |
Write derived-class fields into a JSON object.
Must not be responsible for the "type" discriminator; to_json sets it after this call.
| j | JSON object to fill |
Implemented in shamrock::solvergraph::IDataEdgeSerializable< T >.
|
inlinestatic |
Reconstruct a registered type from JSON.
Reads j.at("type") and dispatches to the matching factory in JsonSerializable_registry.
| j | JSON object containing at least a string "type" field |
| std::runtime_error | if j is not an object with a string "type" field, or if the type name is not registered |
Definition at line 208 of file JsonSerializable.hpp.
|
inline |
Write this object to JSON, including the "type" discriminator.
Calls _impl_to_json for derived fields, then sets j["type"] = type_name(). Derived types must not rely on writing "type" themselves; it is always overwritten here after _impl_to_json.
| j | JSON object to fill |
Definition at line 84 of file JsonSerializable.hpp.
|
pure virtual |
Return the type discriminator string for this class.
Must match the name used in JsonSerializable_registry::register_type and the "type" field in JSON.
Implemented in shamrock::solvergraph::IDataEdgeSerializable< T >.