uon-team / model

A decorator-based schema/model library with json and binary (de)serialization
MIT License
0 stars 0 forks source link

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

Open sdalexandre opened 3 years ago

sdalexandre commented 3 years ago

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 commented 2 years ago

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).