godotjs / godotjs/javascript

Issues with JSON.stringify for godot objects

Ouverte
#72 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
C++
Étoiles
1.1k
Forks
80
Métriques de merge des PR
Aucune PR mergée en 30 j

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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.