npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mapper-json-c

v1.2.0

Published

mapper for json

Downloads

80

Readme

mapper-json-c

用于将json数据根据指定实例格式化 npm i mapper-json-c

装饰器

@JsonProperty()

赋值装饰器,将json中的属性赋值给class中定义的对应属性

class TestEntity {
    // str => str
    @JsonProperty()
    str: string = undefined;

    // str1 => str1
    @JsonProperty('str1')
    str1: string = undefined;
    
    // str1 => str2
    @JsonProperty('str1')
    str2: string = undefined;

    // obj = TestEntity => obj
    @JsonProperty({ name: 'obj', clazz: TestEntity })
    obj: TestEntity = void 0;
}

@ObjectEntriesProperty()

转义装饰器,将json中的对象属性拆分key-value后赋值给class中定义的对应属性

class TestEntity {
    // {key1:value1,key2:value2} => [{label:key1,value:value1},{label:key2,value:value2}]
    @ObjectEntriesProperty()
    objArr1: {label:string,value:string}[] = [];

    // {key1:value1,key2:value2} => [{myLabel:key1,myValue:value1},{myLabel:key2,myValue:value2}]
    @ObjectEntriesProperty(['myLabel','myValue'])
    objArr2: {label:string,value:string}[] = [];
}

@LockString()

String类型锁装饰器,锁定属性类型为String

class TestEntity {
    // 'number', 'boolean', 'bigint' 会被转为string
    // 其他的设置为空字符串
    @LockString()
    str: string = undefined;

    // 'number', 'boolean', 'bigint' 会被转为string
    // object,array 会被设置为JSON字符串
    // 其他的设置为空字符串
    @LockString('json')
    str1: string = undefined;
    

    // undefined, null 转为空字符串
    // 其他数据强制执行String()方法
    @LockString('force')
    str2: string = undefined;

    // 所有数据强制执行String()方法
    @LockString('forceAll')
    str3: string = void 0;
}

@LockNumber()

Number类型锁装饰器,锁定属性类型为Number

class TestEntity {
    // 'number', 'boolean'类型会转为数字,其他类型的会被设置为undefined
    @LockNumber()
    num: number = undefined;

    // 'number', 'boolean'类型会转为bigint,其他类型的会被设置为undefined
    @LockNumber('bigint')
    str2: string = undefined;
}

使用示例

创建自己的实例对象

class TestEntity {
    @JsonProperty('num')
        num: number = undefined;
    
        @JsonProperty()
        str: string = undefined;
    
        @JsonProperty()
        bool: boolean = false;
        
        @JsonProperty()
        arr: [] = undefined;
        
        @JsonProperty()
        obj: {} = undefined;
        
        @JsonProperty({ name: 'me', clazz: TestEntity })
        me: TestEntity = void 0;
    
        @JsonProperty({ clazz: TestEntity })
        me1: TestEntity = void 0;
        
        @JsonProperty({ name: 'me', clazz: TestEntity })
        me2: TestEntity = void 0;

        @LockNumber()
        num1: number = void 0;

        @LockString()
        str1: string = void 0;
        
        @JsonProperty({ name: 'meArr', clazz: TestEntity })
        meArr: TestEntity[] = [];

        @ObjectEntriesProperty()
        objArr1: {label:string,value:string}[] = [];

        @ObjectEntriesProperty(['label','value'])
        objArr2: {label:string,value:string}[] = [];

        // 先转换属性名称,再转格式
        @ObjectEntriesProperty(['label','value'])
        @JsonProperty('objArr0')
        objArr3: {label:string,value:string}[] = [];
}

input :

const json = {
    num: 1,
    str: 'strin',
    bool: false,
    arr: [{test: 1}],
    obj: { a: 1 },
    me: { num: 0 },
    me1: { num: 1 },
    me2: { num: 2 },
    me3: { num: 3 },
    num1: '1',
    str1: 2,
    meArr: [{}],
    objArr1: {1:'v11',2:'v22'},
    objArr2: {1:'v11',2:'v22'},
    objArr0: {1:'v11',2:'v22'},
    undefindKey: '1',
}

use :

mapperJsonC(json, TestEntity)

output :

TestEntity {
    num: 1,
    str: 'strin',
    bool: false,
    arr: [{test: 1}],
    obj: { a: 1 },
    me: {
        num: 0,
        str: undefined,
        bool: undefined,
        arr: undefined,
        obj: undefined,
        me: undefined,
        me1: undefined,
        me2: undefined,
        num1: undefined,
        str1: '',
        meArr: [],
        objArr1: [],
        objArr2: [],
        objArr3: [],
    },
    me1: {
        num: 1,
        str: undefined,
        bool: undefined,
        arr: undefined,
        obj: undefined,
        me: undefined,
        me1: undefined,
        me2: undefined,
        num1: undefined,
        str1: '',
        meArr: [],
        objArr1: [],
        objArr2: [],
        objArr3: [],
    },
    me2: {
        num: 0,
        str: undefined,
        bool: undefined,
        arr: undefined,
        obj: undefined,
        me: undefined,
        me1: undefined,
        me2: undefined,
        num1: undefined,
        str1: '',
        meArr: [],
        objArr1: [],
        objArr2: [],
        objArr3: [],
    },
    num1: 1,
    str1: '2',
    meArr: [{
        num: undefined,
        str: undefined,
        bool: undefined,
        arr: undefined,
        obj: undefined,
        me: undefined,
        me1: undefined,
        me2: undefined,
        num1: undefined,
        str1: '',
        meArr: [],
        objArr1: [],
        objArr2: [],
        objArr3: [],
    }],
    objArr1: [['1','v11'],['2','v22']],
    objArr2: [{label:'1',value:'v11'},{label:'2',value:'v22'}],
    objArr3: [{label:'1',value:'v11'},{label:'2',value:'v22'}],
}

可参考单元测试用例