TypeScript:使用装饰器进行自定义JSON序列化 [英] TypeScript : Using decorators for custom JSON serialization

查看:721
本文介绍了TypeScript:使用装饰器进行自定义JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现TypeScript装饰器,如所示, 链接.我的项目正在运行,但是从Decorator检索一些值时遇到了问题.

I'm trying to implement TypeScript decorators as seen in this, this and this link. I got my project running but I've got an issue when it comes to retrieve some values from the Decorator.

首先,相关的类:

SerializeDecorator.ts

let toSerialize = new WeakMap();

// for each target (Class object), add a map of property names
export function serialize(target: any, key: string) {
    console.log("Target = " + target + " / Key = " + key);

    let map = toSerialize.get(target);

    if (!map) {
        map = [];
        toSerialize.set(target, map);
    }

    map.push(key);

    console.log(toSerialize);
}

export function print() {
    console.log(toSerialize);
}

/* not used for now
export function getArray() {
    return toSerialize;
}

export function value(key: string) {
    return this[key];
}
*/

DeviceClass.ts

import * as serializer from "./SerializeDecorator";

export class Device {
    @serializer.serialize
    private deviceid: number;
    private indication: number;
    private serialnumber: number;

    private type: string = "MEMS6";
    private value: string;

    public constructor(id: number = 1, indic: number = 1, serial: number = 200001) {
        this.deviceid = id;
        this.indication = indic;
        this.serialnumber = serial;
    }

    public getDeviceid(): number {
        return this.deviceid;
    }

    public setDeviceid(i: number) {
        this.deviceid = i;
    }

    public getIndication(): number {
        return this.indication;
    }

    public setIndication(i: number) {
        this.indication = i;
    }

    public getSerialnumber(): number {
        return this.serialnumber;
    }

    public setSerialnumber(i: number) {
        this.serialnumber = i;
    }

    public getType(): string {
        return this.type;
    }

    public setType(s: string) {
        this.type = s;
    }

    public getValue(): string {
        return this.value;
    }

    public setValue(s: string) {
        this.value = s;
    }

    public toJSON(): any {
        serializer.print();

        // just to return some value ...
        return {
            "test": "test"
        };
    }
}

我的目标

我想将所有要序列化的属性放在由Decorator保存的(Weak)Map中.然后,将所有属性放置在Map中之后,我想在调用覆盖的toJSON函数时检索属性的名称及其值. (一个for循环,在每个key上循环以找到所需的target,并检索value)

I want to put all the properties to be serialized in a (Weak)Map, kept by the Decorator. Then, once all the properties have been placed in the Map, I would like to retrieve the name of the property and its value when calling the overridden toJSON function. (A for loop, looping on each key for a desired target and retrieving the value)

我的问题

当我如图所示致电serializer.print()时,我希望看到类似的内容:

When I call serializer.print() as shown, I am expecting to see something like :

WeakMap:{[Object object]:["deviceid"]}

WeakMap : { [Object object] : ["deviceid"] }

但是我得到了这个:

WeakMap:{}

WeakMap : {}

我的问题

当然,我想知道我想要实现的目标是否可行.如果有可能,什么原因导致我的WeakMap为空?我对Decorator语法不是很熟悉(例如,没有class),所以它可能不是它确实"是一个对象而使其无法保留引用的事实吗?

Of course, I want to know if what I want to achieve is possible or not. And if it is possible, what are the causes that make my WeakMap empty ? I am not very familiar with the Decorator syntax (no class for example), so could it be that the fact it is not "really" an object makes it unable to keep references ?

推荐答案

您的代码确实有效,但是您却被console.log

Your code works indeed but you are misled by the console.log

弱映射的键不可枚举,因此显示为空.但是调用gethas方法,您会发现情况并非如此.

Keys of a weakmap are not enumerable so it appears empty. But call the get or has method and you will see it's not the case.

这篇关于TypeScript:使用装饰器进行自定义JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆