cerialize icon indicating copy to clipboard operation
cerialize copied to clipboard

Missing in documentation: Serialization on getters / setters

Open briosheje opened this issue 6 years ago • 0 comments

This is quite angular specific, but whenever you bind a getter to angular, it looks like that if it isn't binded in autoserialize it won't work.

Example:

    @autoserialize public Quantity: number;
     public set _Quantity(value: number) {
        this.QuantitySpecified = true;
        this.Quantity= value;
    }
    public get _Quantity(): number {
        return this.Quantity;
    }

    @autoserialize public QuantitySpecified: boolean;

By binding _Quantity to the angular's NgModel (or anything similar, really), this won't work if the instance is coming from a Deserialization.

To make it work, you need to bind the setter with serialize / deserialize / autoserialize.

So, to make it work, you need to do:

    @autoserialize public Quantity: number;
    @autoserialize public set _Quantity(value: number) {
        this.QuantitySpecified = true;
        this.Quantity= value;
    }
    public get _Quantity(): number {
        return this.Quantity;
    }

    @autoserialize public QuantitySpecified: boolean;

Not really relevant with the package itself, but it's worth mentioning that it's always better to serialize the setter.

briosheje avatar Jun 15 '18 13:06 briosheje