Angular4 - 为什么自定义 toJSON() 只在新对象上被调用?
Angular4 - why does custom toJSON() only get called on new objects?
我有这个代码。请注意,序列化只是将 template_items 属性 重命名为 template_items_attributes:
export class Template {
constructor(
) {}
public id: string
public account_id: string
public name: string
public title: string
public info: string
public template_items: Array<TemplateItem>
toJSON(): ITemplateSerialized {
return {
id: this.id,
account_id: this.account_id,
name: this.name,
title: this.title,
info: this.info,
template_items_attributes: this.template_items
}
}
}
export interface ITemplateSerialized {
id: string,
account_id: string,
name: string,
title: string,
info: string,
template_items_attributes: Array<TemplateItem>
}
在本地创建对象工作正常,stringify 调用 toJSON() 方法。
但是,一旦我将该对象发送到 API:
private newTemplate(name: string): Template {
let template = new Template();
template.name = name;
template.account_id = this._userService.user.account_id;
// next 5 lines are for testing that toJSON() is called on new obj
let item = new TemplateItem();
item.content = "Test"
template.template_items.push(item);
let result = JSON.stringify(template);
console.log('ready', result); // SHOWS the property changes
return template;
}
postTemplate(name: string): Observable<any> {
return this._authService.post('templates', JSON.stringify(this.newTemplate(name)))
.map((response) => {
return response.json();
});
}
它被保存并返回,但从那时起,当我再次进行字符串化和保存时,它不会调用 toJSON()。
patchTemplate(template: Template): Observable<any> {
console.log('patching', JSON.stringify(template)); // DOES NOT CHANGE!
return this._authService.patch('templates' + `/${template.id}`, JSON.stringify(template))
.map((response) => {
return response.json();
});
}
为什么 toJSON() 只对新对象起作用?
事实上,您的问题与 Angular 或 Typescript 无关,只是一些 JavaScript 以及序列化工作的逻辑和 为什么 我们序列化对象吗?
I send that object to the API, save and return it
当您 return 来自 API 的 "object" 时,您正在 return 解析一个字符串作为 JSON 序列化对象。然后你得到一个 plain JavaScript 对象,not 你的 class 的一个实例。
Object
JavaScript 中的原型没有 toJSON
方法,即使有,也不是您在 Template
中编写的方法 class,所以不会调用.
您甚至不需要服务器调用来复制它,只需这样做
const obj = JSON.parse(JSON.stringify(new Template()))
obj.toJSON // undefined
并且您会看到 obj
不是 Template
的实例。它只是一个对象,恰好将所有字段作为原始对象制作为 Template
实例,但它 不是 class 的实例。
我有这个代码。请注意,序列化只是将 template_items 属性 重命名为 template_items_attributes:
export class Template {
constructor(
) {}
public id: string
public account_id: string
public name: string
public title: string
public info: string
public template_items: Array<TemplateItem>
toJSON(): ITemplateSerialized {
return {
id: this.id,
account_id: this.account_id,
name: this.name,
title: this.title,
info: this.info,
template_items_attributes: this.template_items
}
}
}
export interface ITemplateSerialized {
id: string,
account_id: string,
name: string,
title: string,
info: string,
template_items_attributes: Array<TemplateItem>
}
在本地创建对象工作正常,stringify 调用 toJSON() 方法。
但是,一旦我将该对象发送到 API:
private newTemplate(name: string): Template {
let template = new Template();
template.name = name;
template.account_id = this._userService.user.account_id;
// next 5 lines are for testing that toJSON() is called on new obj
let item = new TemplateItem();
item.content = "Test"
template.template_items.push(item);
let result = JSON.stringify(template);
console.log('ready', result); // SHOWS the property changes
return template;
}
postTemplate(name: string): Observable<any> {
return this._authService.post('templates', JSON.stringify(this.newTemplate(name)))
.map((response) => {
return response.json();
});
}
它被保存并返回,但从那时起,当我再次进行字符串化和保存时,它不会调用 toJSON()。
patchTemplate(template: Template): Observable<any> {
console.log('patching', JSON.stringify(template)); // DOES NOT CHANGE!
return this._authService.patch('templates' + `/${template.id}`, JSON.stringify(template))
.map((response) => {
return response.json();
});
}
为什么 toJSON() 只对新对象起作用?
事实上,您的问题与 Angular 或 Typescript 无关,只是一些 JavaScript 以及序列化工作的逻辑和 为什么 我们序列化对象吗?
I send that object to the API, save and return it
当您 return 来自 API 的 "object" 时,您正在 return 解析一个字符串作为 JSON 序列化对象。然后你得到一个 plain JavaScript 对象,not 你的 class 的一个实例。
Object
JavaScript 中的原型没有 toJSON
方法,即使有,也不是您在 Template
中编写的方法 class,所以不会调用.
您甚至不需要服务器调用来复制它,只需这样做
const obj = JSON.parse(JSON.stringify(new Template()))
obj.toJSON // undefined
并且您会看到 obj
不是 Template
的实例。它只是一个对象,恰好将所有字段作为原始对象制作为 Template
实例,但它 不是 class 的实例。