Issues with JSON.stringify for godot objects
- 主要语言
- C++
- 星标
- 1.1k
- 派生
- 80
- PR 合并指标
- 30 天内没有已合并 PR
描述
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.
贡献指南
调研方向
从提供的 TypeScript 复现开始,检查 JSON.stringify 处理 Godot Node 实例时所使用的绑定层行为。检查在序列化过程中 name、toJSON 和 __class__/__ptr__/__ctx__/__id__ 属性是如何暴露的。完成的标准是:序列化后的对象包含预期的 name 和 toJSON 行为,且不包含不必要的绑定属性,包括在数组中时也是如此。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp, javascript, typescript
- 领域
- game-dev
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100