9 struct convert<std::any> {
10 static Node encode(
const std::any& rhs) {
12 if (rhs.type() ==
typeid(
int)) {
13 node = std::any_cast<int>(rhs);
14 }
else if (rhs.type() ==
typeid(
float)) {
15 node = std::any_cast<float>(rhs);
16 }
else if (rhs.type() ==
typeid(std::string)) {
17 node = std::any_cast<std::string>(rhs);
19 throw std::runtime_error(
"Unsupported type for YAML serialization");
24 static bool decode(
const Node& node, std::any& rhs) {
26 if (node.IsScalar()) {
27 std::string value = node.as<std::string>();
45 std::unordered_map<std::string, std::any>
Data;
47 void Write(
const std::string& key,
const std::any& value)
override {
51 std::any
Read(
const std::string& key)
const override {
52 auto it =
Data.find(key);
53 if (it ==
Data.end()) {
54 throw std::runtime_error(
"Key not found: " + key);
60 for (
const auto& [key, value] :
Data) {
77 void Write(
const std::string& key,
const std::any& value)
override {
78 if (value.type() ==
typeid(
int)) {
79 JsonData[key] = std::any_cast<int>(value);
80 }
else if (value.type() ==
typeid(
float)) {
81 JsonData[key] = std::any_cast<float>(value);
82 }
else if (value.type() ==
typeid(std::string)) {
83 JsonData[key] = std::any_cast<std::string>(value);
88 std::any
Read(
const std::string& key)
const override {
90 throw std::runtime_error(
"Key not found: " + key);
109 void Write(
const std::string& key,
const std::any& value)
override {
110 if (value.type() ==
typeid(
int)) {
111 YamlData[key] = std::any_cast<int>(value);
112 }
else if (value.type() ==
typeid(
float)) {
113 YamlData[key] = std::any_cast<float>(value);
114 }
else if (value.type() ==
typeid(std::string)) {
115 YamlData[key] = std::any_cast<std::string>(value);
120 std::any
Read(
const std::string& key)
const override {
122 throw std::runtime_error(
"Key not found: " + key);
124 return YamlData[key].as<std::any>();
141 std::ofstream file(filePath);
142 if (!file.is_open()) {
143 throw std::runtime_error(
"Failed to open file for writing: " + filePath);
147 context->WriteToStream(file);
154 std::ifstream file(filePath);
155 if (!file.is_open()) {
156 throw std::runtime_error(
"Failed to open file for reading: " + filePath);
159 context->ReadFromStream(file);
165 if (filePath.ends_with(
".bin")) {
167 }
else if (filePath.ends_with(
".json")) {
169 }
else if (filePath.ends_with(
".yaml") || filePath.ends_with(
".yml")) {
170 return SerializationFormat::YAML;
178 return std::make_unique<BinarySerializationContext>();
180 return std::make_unique<JsonSerializationContext>();
181 case SerializationFormat::YAML:
182 return std::make_unique<YamlSerializationContext>();
184 throw std::runtime_error(
"Unsupported serialization format.");
void Write(const std::string &key, const std::any &value) override
std::any Read(const std::string &key) const override
void WriteToStream(std::ostream &stream) const override
void ReadFromStream(std::istream &stream) override
std::unordered_map< std::string, std::any > Data
Interface for serialization contexts.
ISerializationContext backed by nlohmann::json.
void ReadFromStream(std::istream &stream) override
void Write(const std::string &key, const std::any &value) override
void WriteToStream(std::ostream &stream) const override
std::any Read(const std::string &key) const override
void DeserializeFromFile(const std::string &filePath)
Deserializes the object from a file.
void SerializeToFile(const std::string &filePath) const
Serializes the object to a file.
virtual void Serialize(std::unique_ptr< ISerializationContext > context) const =0
Serializes the object using the specified context.
virtual void Deserialize(std::unique_ptr< ISerializationContext > context)=0
Deserializes the object using the specified context.
static std::unique_ptr< ISerializationContext > CreateContext(SerializationFormat format)
Builds the context implementation matching format.
static SerializationFormat DetectFormat(const std::string &filePath)
Picks a format from filePath's extension (.bin/.json/.yaml/.yml).
ISerializationContext backed by a YAML::Node.
void Write(const std::string &key, const std::any &value) override
std::any Read(const std::string &key) const override
void ReadFromStream(std::istream &stream) override
void WriteToStream(std::ostream &stream) const override
Root namespace for everything the engine exposes.