Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shamrock::solvergraph::JsonSerializable Struct Referenceabstract

Base interface for types that can be serialized to and from JSON. More...

#include <shamsolvergraph/include/shamsolvergraph/JsonSerializable.hpp>

Inheritance diagram for shamrock::solvergraph::JsonSerializable:

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< JsonSerializablefrom_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.

Detailed Description

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.

int value;
void _impl_to_json(nlohmann::json &j) const override { j["value"] = value; }
std::string type_name() const override { return "MyType"; }
static MyType from_json(const nlohmann::json &j) {
return MyType{j.at("value").get<int>()};
}
};
// Register once (name must match type_name() / JSON "type")
// Serialize
MyType obj{42};
nlohmann::json j;
obj.to_json(j); // also sets j["type"] = "MyType"
// Deserialize polymorphically
auto *restored = dynamic_cast<MyType *>(ptr.get());
void from_json(const nlohmann::json &j, PatchSchedulerConfig &p)
Deserializes a PatchSchedulerConfig object from a JSON object.
static JsonSerializable_registry & instance()
Access the process-wide registry instance.
void register_type(const std::string &name, std::source_location loc=std::source_location::current())
Register a type under a string name for polymorphic deserialization.
Base interface for types that can be serialized to and from JSON.
virtual void _impl_to_json(nlohmann::json &j) const =0
Write derived-class fields into a JSON object.
static std::unique_ptr< JsonSerializable > from_json(const nlohmann::json &j)
Reconstruct a registered type from JSON.
virtual std::string type_name() const =0
Return the type discriminator string for this class.

Definition at line 72 of file JsonSerializable.hpp.

Member Function Documentation

◆ _impl_to_json()

virtual void shamrock::solvergraph::JsonSerializable::_impl_to_json ( nlohmann::json & j) const
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.

Parameters
jJSON object to fill

Implemented in shamrock::solvergraph::IDataEdgeSerializable< T >.

◆ from_json()

std::unique_ptr< JsonSerializable > shamrock::solvergraph::JsonSerializable::from_json ( const nlohmann::json & j)
inlinestatic

Reconstruct a registered type from JSON.

Reads j.at("type") and dispatches to the matching factory in JsonSerializable_registry.

Parameters
jJSON object containing at least a string "type" field
Returns
Owned instance of the matching registered type
Exceptions
std::runtime_errorif 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.

Here is the call graph for this function:

◆ to_json()

void shamrock::solvergraph::JsonSerializable::to_json ( nlohmann::json & j) const
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.

Parameters
jJSON object to fill

Definition at line 84 of file JsonSerializable.hpp.

Here is the call graph for this function:

◆ type_name()

virtual std::string shamrock::solvergraph::JsonSerializable::type_name ( ) const
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 >.


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