JS 有哪些现成的库能够方便的对业务对象导出必要的属性(JSON)或导入 Json 对象? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
riceball
V2EX    问与答

JS 有哪些现成的库能够方便的对业务对象导出必要的属性(JSON)或导入 Json 对象?

  •  
  •   riceball 2023-04-14 20:54:40 +08:00 1392 次点击
    这是一个创建于 986 天前的主题,其中的信息可能已经有所发展或是发生改变。

    常见的,业务对象中的的某些属性需要导出为 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]", "型": "邮件"}] } 
    riceball
        1
    riceball  
    OP
       2023-04-15 17:13:46 +08:00
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5281 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 08:55 PVG 16:55 LAX 00:55 JFK 03:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86