V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
riceball
V2EX  ›  问与答

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

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

    常见的,业务对象中的的某些属性需要导出为 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   ·   我们的愿景   ·   实用小工具   ·   2240 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:11 · PVG 12:11 · LAX 21:11 · JFK 00:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.