qmuntal / gltf

Go library for encoding glTF 2.0 files
https://www.khronos.org/gltf/
BSD 2-Clause "Simplified" License
242 stars 32 forks source link

Fix failed to Encode() some gltf.Document when document has certain value in extras. #42

Closed neguse closed 2 years ago

neguse commented 2 years ago

Hi. I had a problem with Encode() failing when the document contains certain value in extras. For example, the following code reproduces the problem. https://play.golang.org/p/CiO4tz0Zwpy

MarshalJSON() of type Node does bytes.Replace() to remove default and empty values from the JSONized data, but this may remove the data in the extras as well, so it may generate invalid JSON. https://github.com/qmuntal/gltf/blob/6c59e8324d6bed73c8c875ebb608a6e3fba72f8b/encode.go#L176-L199

I think it would be better to delete the field using omitempty instead of using bytes.Replace().

Thanks.

qmuntal commented 2 years ago

I like this approach!