JohnWeisz/TypedJSON

[Feature request] Registering types to avoid repeating serializers

Closed this issue · 1 comments

I find myself repeating serializers in the jsonMember decorator. Example:

    @jsonMember(decimalSerializer)
    minimumPriceBlackWhite: Decimal;

    @jsonMember(decimalSerializer)
    minimumPriceColor: Decimal;

    @jsonArrayMember(PrinterOption)
    options: Array<PrinterOption>;

    @jsonMember(decimalSerializer)
    purchasePrice: Decimal;

Decimal, in this case, is a object provided by a third party library. It would be nice if I could to the following:

TypedJSON.setGlobalConfig({
    mappedTypes: [
        [Decimal, decimalSerializer],
    ],
});

This way I can avoid repeating the serializer. It would also allow us to make our own serializers for native types such as Date.

This structure could be stored as a Map. Eager to know your opinion.

Hi @MatthiasKunnen, sounds cool. If you look into the implementation, currently the serlization/deserialization process is basically an inlined strategy. I was thinking about changing that into a proper strategy for other purposes but IMO it would enable this feature nicely. Although, frankly speaking, I am not sure how long it would take me as my dev time is limited at the moment. But, we will gladly accept any pull requests :D