ECMAScript
ECMAScript copied to clipboard
Issues with JSON.stringify for godot objects
Hi, I found a number of issues with JSON.stringify having some unexpected behavior when trying to write to a file from a tool script for godot objects.
- There are a number of properties (
__class__
,__ptr__
,__ctx__
,__id__
) that are added to every instance of a class that extends godot.Node / seem to be properties to interact with the binding layer - The "name" property is completely omitted
- toJSON doesn't affect JSON.stringify
- When stringifying arrays of these objects, the results balloon in size due to the additional properties
Here's some code as an example to repro the issue:
class Item extends godot.Node {
stuff: string;
name: string;
constructor(stuff: string, newName: string) {
super();
this.stuff = stuff;
this.name = newName;
}
}
// Helper function to save to a file
function save(path: string, contents: string) {
const file = new godot.File();
file.open(path, godot.File.WRITE);
file.store_string(contents);
file.close();
}
// Code to write to file in a tool script
const item1 = new Item('foo', 'bar');
console.log(item1.stuff);
save('res://src/test.json', JSON.stringify(item1));
test.json is as follows:
{"__class__":"Node","__ptr__":"0x-3A837B60","__ctx__":"0x-23DD8CF0","__id__":34393,"stuff":"foo"}
Specifically, the "name" property is missing from the object and the extra underscore properties are added. This isn't too much of a problem for single objects, but when you have arrays of these objects, it becomes a problem since the size of the serialized data is significantly larger than it needs to be.
Temporary fix
Note that it is possible to get around the issue by extracting the properties into plain javascript objects, but this seems like a very manual fix for each object that needs to be serialized (particularly since toJSON
doesn't seem to work).
const item1 = new Item('foo', 'bar');
console.log(item1.stuff);
const serializableItem = {
stuff: item1.stuff,
name: item1.name,
};
save('res://src/test.json', JSON.stringify(serializableItem));
Which produces the following result:
{"stuff":"foo","name":"bar"}
Potential fix: Using non-enumerable properties?
I'm not sure how these properties are used, but perhaps making them non-enumerable would solve this issue? Not sure if it would require other changes in the binding layer though since it's unclear how these properties are used.
There are a number of properties (class,ptr, ctx, id) that are added to every instance of a class that extends godot.Node / seem to be properties to interact with the binding layer
These internal properties only exist under debug build for debug usage. It is better to add an option in project settings to disable it.
The "name" property is completely omitted
I think the name
is a property of godot.Node
with getter and setter so it is omitted. And we should make them enumerable.