Skip to content

Commit

Permalink
Merge pull request #462 from ptc-tgamper/bug/issue457
Browse files Browse the repository at this point in the history
Fix Empty nodes are wrongly serialized as null Issue #457
  • Loading branch information
syoyo authored Nov 23, 2023
2 parents f32475c + d4ea67c commit b6e2398
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,52 @@ TEST_CASE("default-material", "[issue-459]") {
CHECK(mat.occlusionTexture.index == -1);
CHECK(mat.emissiveTexture.index == -1);
}

TEST_CASE("serialize-empty-node", "[issue-457]") {
tinygltf::Model m;
// Add default constructed node to model
m.nodes.push_back({});
// Add scene to model
m.scenes.push_back({});
// The scene's only node is the empty node
m.scenes.front().nodes.push_back(0);

// Serialize model to output stream
std::stringstream os;
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);

// Parse serialized model
nlohmann::json j = nlohmann::json::parse(os.str());

// Serialized nodes shall hold an empty object that
// represents the default constructed node
REQUIRE(j.find("nodes") != j.end());
REQUIRE(j["nodes"].is_array());
REQUIRE(1 == j["nodes"].size());
CHECK(j["nodes"][0].is_object());
CHECK(j["nodes"][0].empty());

// We also want to make sure that the serialized scene
// is referencing the empty node.

// There shall be a single serialized scene
auto scenes = j.find("scenes");
REQUIRE(scenes != j.end());
REQUIRE(scenes->is_array());
REQUIRE(1 == scenes->size());
auto scene = scenes->at(0);
REQUIRE(scene.is_object());
// The scene's nodes array shall hold a reference
// to the single node
auto nodes = scene.find("nodes");
REQUIRE(nodes != scene.end());
REQUIRE(nodes->is_array());
REQUIRE(1 == nodes->size());
auto node = nodes->at(0);
CHECK(node.is_number_integer());
int idx = -1;
node.get_to(idx);
CHECK(0 == idx);
}
10 changes: 10 additions & 0 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -8036,6 +8036,16 @@ static void SerializeGltfModel(const Model *model, detail::json &o) {
for (unsigned int i = 0; i < model->nodes.size(); ++i) {
detail::json node;
SerializeGltfNode(model->nodes[i], node);

if (detail::JsonIsNull(node)) {
// Issue 457.
// `node` does not have any required parameters,
// so the result may be null(unmodified) when all node parameters
// have default value.
//
// null is not allowed thus we create an empty JSON object.
detail::JsonSetObject(node);
}
detail::JsonPushBack(nodes, std::move(node));
}
detail::JsonAddMember(o, "nodes", std::move(nodes));
Expand Down

0 comments on commit b6e2398

Please sign in to comment.