序列化后如何使用 getters 恢复 TypeScript 私有属性?
How to revive TypeScript private properties with getters after serialization?
我 运行 遇到了 classes 的序列化问题,我不知道如何处理。
我从 REST 或数据库请求创建对象,如下所示:
export interface ILockerModel {
id: string
lockerId: string
ownerId: string
modules: IModuleModel[]
}
export class LockerModel implements ILockerModel {
private _id: string
private _lockerId: string
private _ownerId: string
private _modules: ModuleModel[]
constructor(document: ILockerModel) {
this._id = document.id
this._lockerId = document.lockerId
this._ownerId = document.ownerId
this._modules = document.modules.map(m => new ModuleModel(m))
}
// Utility methods
}
然后我有多个实用方法,可以更轻松地使用模型,从列表中添加和删除内容等等。
完成后,我想将对象保存到文档数据库或 return REST 响应中,因此我调用 JSON.stringify(objectInstance)
。但是,这给了我 class 但所有属性都带有下划线 (_
),而不是我的 getter 值。这会破坏我应用程序其他部分的反序列化。
序列化接口给了我想要的东西,但我还没有找到从 class 到接口表示的直接方法。这个问题变得更加棘手,因为我在层次结构中反序列化数据(请参阅构造函数中的模块映射)。
你一般是怎么解决这个问题的?
据我所知,您并没有真正实现 ILockerModel
。这不应该引发错误吗?
当我 运行 它时,我得到以下信息:
Type 'LockerModel' is missing the following properties from type 'ILockerModel': id, lockerId, ownerId, modules
另一件事是 JSON.strigify()
只获取您的对象并制作其所有属性的字符串表示形式。它不关心你的吸气剂。如果你想让它把它转换成正确的格式,你应该给它一个正确格式的对象。
一种解决方案是通过使用 map
和 reduce
:
的组合,从所有键中删除“_”
const input = {
_test: 123,
_hello: 'world'
};
console.log(input);
console.log(JSON.stringify(input));
const convertToJson = (obj) => {
return Object.entries(obj) // Create array from object
.map(([key, value]) => [ // change key to remove '_'
key.startsWith('_') ? key.substring(1) : key,
value
])
.reduce((acc, [key, value]) => { // Transform back to object
acc[key] = value;
return acc;
}, {});
}
const output = convertToJson(input);
console.log(output);
console.log(JSON.stringify(output));
或者如果你被允许使用 ES10:
const input = {
_test: 123,
_hello: 'world'
};
console.log(input);
console.log(JSON.stringify(input));
const convertToJson = (obj) => {
return Object.fromEntries( // Create Object from array
Object.entries(obj) // Create array from object
.map(([key, value]) => [ // change key to remove '_'
key.startsWith('_') ? key.substring(1) : key,
value
])
);
}
const output = convertToJson(input);
console.log(output);
console.log(JSON.stringify(output));
我 运行 遇到了 classes 的序列化问题,我不知道如何处理。
我从 REST 或数据库请求创建对象,如下所示:
export interface ILockerModel {
id: string
lockerId: string
ownerId: string
modules: IModuleModel[]
}
export class LockerModel implements ILockerModel {
private _id: string
private _lockerId: string
private _ownerId: string
private _modules: ModuleModel[]
constructor(document: ILockerModel) {
this._id = document.id
this._lockerId = document.lockerId
this._ownerId = document.ownerId
this._modules = document.modules.map(m => new ModuleModel(m))
}
// Utility methods
}
然后我有多个实用方法,可以更轻松地使用模型,从列表中添加和删除内容等等。
完成后,我想将对象保存到文档数据库或 return REST 响应中,因此我调用 JSON.stringify(objectInstance)
。但是,这给了我 class 但所有属性都带有下划线 (_
),而不是我的 getter 值。这会破坏我应用程序其他部分的反序列化。
序列化接口给了我想要的东西,但我还没有找到从 class 到接口表示的直接方法。这个问题变得更加棘手,因为我在层次结构中反序列化数据(请参阅构造函数中的模块映射)。
你一般是怎么解决这个问题的?
据我所知,您并没有真正实现 ILockerModel
。这不应该引发错误吗?
当我 运行 它时,我得到以下信息:
Type 'LockerModel' is missing the following properties from type 'ILockerModel': id, lockerId, ownerId, modules
另一件事是 JSON.strigify()
只获取您的对象并制作其所有属性的字符串表示形式。它不关心你的吸气剂。如果你想让它把它转换成正确的格式,你应该给它一个正确格式的对象。
一种解决方案是通过使用 map
和 reduce
:
const input = {
_test: 123,
_hello: 'world'
};
console.log(input);
console.log(JSON.stringify(input));
const convertToJson = (obj) => {
return Object.entries(obj) // Create array from object
.map(([key, value]) => [ // change key to remove '_'
key.startsWith('_') ? key.substring(1) : key,
value
])
.reduce((acc, [key, value]) => { // Transform back to object
acc[key] = value;
return acc;
}, {});
}
const output = convertToJson(input);
console.log(output);
console.log(JSON.stringify(output));
或者如果你被允许使用 ES10:
const input = {
_test: 123,
_hello: 'world'
};
console.log(input);
console.log(JSON.stringify(input));
const convertToJson = (obj) => {
return Object.fromEntries( // Create Object from array
Object.entries(obj) // Create array from object
.map(([key, value]) => [ // change key to remove '_'
key.startsWith('_') ? key.substring(1) : key,
value
])
);
}
const output = convertToJson(input);
console.log(output);
console.log(JSON.stringify(output));