TypeScript : Using decorators for custom JSON serialization

独自空忆成欢 提交于 2019-12-12 01:57:10

问题


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.

First, the related classes :

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"
        };
    }
}

My goal

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)

My issue

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

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

But I got this instead :

WeakMap : {}

My question

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 ?


回答1:


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

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.



来源:https://stackoverflow.com/questions/41164054/typescript-using-decorators-for-custom-json-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!