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

Feature: Ability to specify how a class is serialized and deserialized #3

Open
sdalexandre opened this issue Aug 19, 2021 · 1 comment

Comments

@sdalexandre
Copy link

Because I want to specify a class that behave like a type.

For example:

@Model()
class MyCounterClass {
     @Member()
    counter: { [key: string]: number; }

    // Some Methods
}

will be serialized this way:

{counter: { [key: string]: number; }}

but I would like it to be this way directly:

{ [key: string]: number; }

I'm currently declaring:

type MyCounterType = { [key: string]: number; }

with the drawback that a type doesn't have methods and I have to create another class for this:

class MyCounterTypeMethods {
   // Some static methods to manipulate MyCounterType.
}
@uon-io
Copy link
Contributor

uon-io commented Dec 9, 2021

It would be possible to do by declaring a toJSON() method in the class, if present it gets called from JSON.stringify(). However, @model overrides toJSON() on the class prototype. I could make it that if toJSON is already declared it wont get overridden (with a warning).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants