Skip to content

Commit

Permalink
feat(engine): add missing error handling in json bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacops committed Jan 30, 2024
1 parent 38a68d1 commit eabe308
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions engine/include/cubos/engine/assets/bridges/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,27 @@ namespace cubos::engine
stream.readUntil(jsonStr, nullptr);
core::data::JSONDeserializer deserializer{};

// New JSONDeserializer() receives a JSON object to deserialize from
nlohmann::json json = nlohmann::json::parse(jsonStr);
// JSONDeserializer() receives a JSON object to deserialize from
nlohmann::json json{};

try
{
json = nlohmann::json::parse(jsonStr);
}
catch (nlohmann::json::parse_error& e)
{
CUBOS_ERROR("{}", e.what());
return false;
}
deserializer.feed(json);

// Deserialize the asset and store it in the asset manager.
T data{};
deserializer.read(data);
if (deserializer.read(data))
{
CUBOS_ERROR("Could not deserialize asset from JSON file");
return false;
}

assets.store(handle, std::move(data));
return true;
Expand All @@ -58,7 +72,11 @@ namespace cubos::engine

// Read the asset from the asset manager and serialize it to the file stream.
auto data = assets.read<T>(handle);
serializer.write(*data);
if (serializer.write(*data))
{
CUBOS_ERROR("Could not serialize asset to JSON file");
return false;
}

// new JSONSerializer() does not receive a stream to write to, need to write to it manually
auto jsonStr = serializer.output().dump();
Expand Down

0 comments on commit eabe308

Please sign in to comment.