Typescript: specified object with fromJson method,

2019-08-02 08:33发布

问题:

i have a object that contains a fromJson method. this method is not working because private property of the class can not be accessed? what is wrong and how to handle it? Code is written in TypeScript.

class Example {
    private Foo: string; // does not matter if private or public, same effect, and normaly has to be private

    constructor(input?: string) {
        if (!!input) {
            this.foo = input;    
        }
    }

    set foo(value: string) {
        this.Foo = value;
    }
    get foo(): string {
        return this.Foo;
    }

    public static fromJson(obj: Object) {
        let result: Example = new Example();
        for (let index in obj) {
            if (Example.hasOwnProperty(index)) { // never runs because false
                result[index] = obj[index];
            }

            /* allready tried this -> same result */
            // if (result.hasOwnProperty(index)) {
            //    result[index] = obj[index];
            //}

            // let descriptor = Object.getOwnPropertyDescriptor(Example, index); // = undefined
            // let descriptor = Object.getOwnPropertyDescriptor(result, index); // = undefined
        }
        return result;
    }

    public toJsonString() {
        return JSON.stringify(this);
    }

    public toJsonObject() {
        return JSON.parse(this.toJsonString());
    }
}

let a = new Example('one');
let json = a.toJsonObject();  // this looks exactly like my api response (type json)
let obj = Example.fromJson(json);
console.log(json);
console.log(obj);

but the console.log(obj) has to be <Example> {"Foo": "one", foo(...)}

EDIT: generated JavaScript:

var Example = (function () {
    function Example(input) {
        if (!!input) {
            this.foo = input;
        }
    }
    Object.defineProperty(Example.prototype, "foo", {
        get: function () {
            return this.Foo;
        },
        set: function (value) {
            this.Foo = value;
        },
        enumerable: true,
        configurable: true
    });
    Example.fromJson = function (obj) {
        var result = new Example();
        for (var index in obj) {
            if (Example.hasOwnProperty(index)) {
                result[index] = obj[index];
            }
        }
        return result;
    };
    Example.prototype.toJsonString = function () {
        return JSON.stringify(this);
    };
    Example.prototype.toJsonObject = function () {
        return JSON.parse(this.toJsonString());
    };
    return Example;
}());
var a = new Example('one');
var json = a.toJsonObject(); // this looks exactly like my api response (type json)
var obj = Example.fromJson(json);
console.log(json);
console.log(obj);

回答1:

class Example {
    private Foo: string = undefined;
    private Foo2: number = undefined;

    constructor(input?: string) {
        if (!!input) {
            this.foo = input;    
        }
    }

    set foo(value: string) {
        this.Foo = value;
    }
    get foo(): string {
        return this.Foo;
    }

    set numeric(value: number) {
        this.Foo2 = value;
    }
    get numeric(): number {
        return this.Foo2;
    }

    public static fromJson(obj: Object) {
        let result: Example = new Example();
        for (let index in obj) {
            if (result.hasOwnProperty(index)) {
                result[index] = obj[index]; // care, has to be result
            }
        }
        return result;
    }

    public toJsonString() {
        return JSON.stringify(this);
    }

    public toJsonObject() {
        return JSON.parse(this.toJsonString());
    }
}

let a = new Example('one');
let json = a.toJsonObject();
let obj = Example.fromJson(json);
console.log(json);
console.log(obj);

i think this is the solution you are looking for. positive effect, if you initialize your properties with undefined, your toJson methods don't list the arguments. so your request traffic is not so big.