
常见的,业务对象中的的某些属性需要导出为 JSON 对象,或者相反,将 Json 对象的值赋值(导入)给业务对象. 然而 Json 对象的属性名称并不一定是就是业务对象中的属性名称,也不是所有的 Json 对象的值都该被赋值(导入).
针对属性默认有如下的约定:
undefined)的不要导出$"开头的可枚举属性不要导出,但可以导入(赋值)例如,假如能够用修饰器定义属性的话:
@Properties export class ContactWay extends AbstractModel { @Property({alias:'启', type: 'Boolean', default: true}) enabled?: boolean; @Property({alias:'私', type: 'Boolean', default: true}) private?: boolean; @Property({alias:'类', type: 'String'}) category?: string; @Property({alias:'型', type: 'String'}) type?: string; @Property({alias:'值', type: 'String', encrypted: true}) value?: string | null; @Property({alias:'起日', type: 'Date'}) startTime?: string | null; @Property({alias:'止日', type: 'Date'}) endTime?: string | null; constructor(options?) { if (typeof optiOns=== 'string') optiOns= {'值': options}; super(options); } } @Properties export class ContactPhone extends ContactWay { constructor(options?) { super(options); this['type'] = '电话'; } } @Properties export class ContactEmail extends ContactWay { constructor(options?) { super(options); this['type'] = '邮件'; } } @Properties export class ContactAddress extends ContactWay { @Property({alias:'国', type: 'String', encrypted: true}) country!: string; @Property({alias:'省', type: 'String', encrypted: true}) province!: string; @Property({alias:'市', type: 'String', encrypted: true}) city!: string; @Property({alias:'区', type: 'String', encrypted: true}) district!: string; @Property({alias:'镇', type: 'String', encrypted: true}) town!: string; @Property({alias:'邮编', type: 'String', encrypted: true}) zip!: string; constructor(options?) { super(options); this['type'] = '地址'; } } @Properties export class Contacts extends AbstractModel { @Property({alias:'电话', type: arrayOf(ContactPhone), encrypted: true}) phones!: ContactPhone[]; @Property({alias:'地址', type: arrayOf(ContactAddress), encrypted: true}) addresses!: ContactAddress[]; @Property({alias:'邮件', type: arrayOf(ContactEmail), encrypted: true}) emails!: ContactEmail[]; } // 导入 const cOntacts= new Contacts({ '电话': ['1234567890', {'私': false, '值': '010-1111111', '类': '单位'}], '地址': [{'国': '中国', '省': '福建', '市': '...'}], '邮件': ['[email protected]'] }) // 导出 JSON.stringify(contacts) { "电话": [{"值": "1234567890", "型": "电话"}, {"私": false, "值": "010-1111111", "类": "单位", "型": "电话"}], "地址": [{"国": "中国", "省": "福建", "市": "...", "型": "地址"}], "邮件": [{"值": "[email protected]", "型": "邮件"}] } 1 riceball OP |