Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize a method function or get (Dynamic serialization) #59

Open
ahaverty opened this issue Mar 14, 2018 · 1 comment
Open

Serialize a method function or get (Dynamic serialization) #59

ahaverty opened this issue Mar 14, 2018 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@ahaverty
Copy link

ahaverty commented Mar 14, 2018

Probably a feature request as I can't find any examples

Would it be possible to serialize a function or typescript get function with @JsonProperty()?
Example:

    @JsonProperty("tally", Number, true)
    public get tally(): number {
        return this._currentTally;
    }

I know could keep the variable up to date as I call on model functions, but I think it would be beneficial to be able to serialize dynamically, and less remembering!
If the above is a language limitation, maybe an interface would be useful?

@JsonObject
export class Order implements JsonConvertHooks {

    before(): void {
        this._total = this.netTotal;
    }
    
    @JsonProperty("lineItems", LineItemConvert, true)
    private  _lineItems: LineItem[] = [];

    @JsonProperty("total", Number, true)
    total: number: number = undefined;

    public addLineItem(lineItem: LineItem) {
        this._lineItems.add(lineItem);
    }
    
    get netTotal(): number {
        let netTotal = 0;
        this.lineItems.forEach((lineItem) => {
            netTotal += lineItem.netPrice;
        });
        return netTotal;
    }
}

The before() function is called just before serialising each property.

(The only issue I can think of is the function naming. If someone added a function before() even if they didn't implement, the serializer would be called, as typescript isn't able to check if an instance inherits after compiling, so it's really just checking if the function exists, and calling)

I've got the above example working in my fork ahaverty@7282f2c

What do ye think?

@andreas-aeschlimann
Copy link
Member

Thank you for your input. We will look into it for sure and come back to you when we discussed it (it might take a while).

@andreas-aeschlimann andreas-aeschlimann self-assigned this Apr 24, 2018
@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Apr 24, 2018
@andreas-aeschlimann andreas-aeschlimann added this to the version 2.x milestone Feb 27, 2019
@andreas-aeschlimann andreas-aeschlimann removed this from the version 2.x milestone May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants