godotjs / godotjs/javascript

Issues with JSON.stringify for godot objects

Đang mở
#72 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
C++
Star
1.1k
Fork
80
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.