godotjs / godotjs/javascript

Issues with JSON.stringify for godot objects

Open
#72 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
1.1k
Forks
80
PR merge metrics
No merged PRs in 30d

Description

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior) 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:

```typescript
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:
```json
{"__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).

```typescript
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:
```json
{"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](https://itnext.io/hidden-properties-in-javascript-73b52def1589) 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.