syoyo/tinygltf

Generate duplicated "extensions" json entry when add custom extension to model's extensions.

ginn-gu opened this issue · 2 comments

Describe the issue
Generate duplicated "extensions" json entry when adding lights to model and adding custom extension to model's extensions.

This issue is only in RapidJSON version. Because of detail::JsonAssign use Copy operation when RapidJSON defined.
see line 7939 in tiny_gltf.h.
when saving "KHR_lights_punctual" extension (see tiny_gltf.h:7945: detail::JsonAddMember(ext_j, "KHR_lights_punctual", std::move(khr_lights_cmn));), the duplicated "extensions" is added.

To Reproduce
#define TINYGLTF_USE_RAPIDJSON
gltf_model.lights.emplace_back(...); // add some lights
gltf_model.extensions.emplace("xxx", custom_extesion); // add custom extension
WriteGltfSceneToFile(gltm_model, ...) // save gltf scene

  • OS
  • Compiler, compiler version, compile options
  • Please attach minimal and reproducible .glTF file if you got an issue related to .glTF data

Expected behaviour

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

syoyo commented

Good catch!

Confirmed the issue by adding some lines to examples/build-gltf

    // lights
    tinygltf::Light dummyLight;
    dummyLight.name = "bora";
    m.lights.push_back(dummyLight);
    tinygltf::Value dummyVal(3.14);
    m.extensions.emplace("ext1", dummyVal);
  "extensions": {
    "ext1": 3.14
  },
  "extensions": {
    "ext1": 3.14,
    "KHR_lights_punctual": {
      "lights": [
        {
          "name": "bora",
          "intensity": 1.0,
          "type": ""
        }
      ]
    }
  },

It is a bit surprise for me that JSON sometimes allows duplicated key name and RapidJSON supports duplicated keys.

https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object
https://stackoverflow.com/questions/35222230/cpp-rapidjson-resolve-key-conflicts-without-information-loss

Need to find a way to overwrite extensions key in RapidJSON API.

syoyo commented

Add a fix to prevent duplicated extensions key generation in RapidJson backend.

The fix now should work well on your side. If you still encounter the issue, please let me know.