1

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);
4
  • What does the generated javascript look like? Commented Apr 5, 2017 at 13:41
  • @Arg0n put it to question via edit Commented Apr 5, 2017 at 13:45
  • 2
    What happens with the generated JS if you assign a value to Foo? private Foo: string = null; Commented Apr 5, 2017 at 13:50
  • 1
    ArgOn is right. Every property has to be initialized to access via hasOwnProperty function. i post a working sample Commented Apr 5, 2017 at 13:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this is exactly what i was looking for. now it's a stupid handwork to initialize all properties in all my models ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.