TypedJSON icon indicating copy to clipboard operation
TypedJSON copied to clipboard

JsonMember's reviver and replacer

Open afshin-hoseini opened this issue 6 years ago • 4 comments

First of all thank you for this awesome library.

I have added following feature and would be glad if you accept:

  1. Implemented reviver and replacer functions for JsonMemberOptions:

Sometimes it's needed to let a member to revive or replace its own value on serialization and/or deserialization process. As like as currency or metric values which should convert their value.

Definition

/** Will be invoked, with the deserialized value in order to let the member, revive it. */
reviver? : (deserializedValue : any)=>any
/** Will be invoked, with the serialized value in order to let the member, replace it. */
replacer? : (serializedValue : any)=>any

Usage

@JsonMember({
      type : Number, 
      reviver : value=> value + 5, 
      replacer : value=> value * 10 
})
level : number

afshin-hoseini avatar Apr 28 '18 08:04 afshin-hoseini

OMG this would be awesome

Jimmy-Hernandez avatar Mar 05 '19 14:03 Jimmy-Hernandez

You can use serializer and deserializer options in the latest version. Or, is it maybe something different?

Neos3452 avatar Mar 05 '19 15:03 Neos3452

@Neos3452 Actually it is an old PR, when serializer and deserializer weren't available. 🙂

@Jimmy-Hernandez 🥺You're the only one who told me something like this in my whole life... I'll appreciate it 😀

afshin-hoseini avatar Mar 06 '19 13:03 afshin-hoseini

@afshin-hoseini I know, but if it is different and serves a purpose to someone I would consider rebasing your change. :)

Neos3452 avatar Mar 06 '19 14:03 Neos3452