serialazy icon indicating copy to clipboard operation
serialazy copied to clipboard

example for TypeSerializer<TSerialized, TOriginal> optional properties?

Open janseng1970 opened this issue 4 years ago • 3 comments

Hello,

is there any example for using @Serialize.Custom with this optinal properties in the TypeSerializer<TSerialized, TOriginal> interface?

/**
 * _Optional._ Original type constructor function.
 * Default: Value of `design:type` for given property.
 */
type?: Constructable.Default<TOriginal>;
/**
 * _Optional._ Property type descriminator function.
 * Used to narrow type constructor function (e.g. for union types)
 * @param serialized Serialized value
 * @returns Original type constructor function
 */
discriminate?(this: void, serialized: TSerialized): Constructable.Default<TOriginal>;

I google the code go in deep for the inflate and deflate function and can not even find, that this two properties are used. I want to use the discriminate function to map the name of a type, stored in a serialized string property to a type via a "typemapper".

Thanks for help!

Georg

janseng1970 avatar Dec 10 '19 11:12 janseng1970

Hey Georg,

This discriminate functionality was never fully implemented. Later I decided that it adds unnecessary complexity to the library and the same effect can be achieved by providing custom up/down functions. So, I removed it completely in v3 (develop branch, can be installed with npm i serialazy@next). Also, you can check a CHANGELOG for more info on changes.

teq avatar Dec 10 '19 13:12 teq

Btw, can you provide a complete example of what you're trying to do?

teq avatar Dec 10 '19 13:12 teq

Hello, thanks for the interesst! May be you find some inspiration or you just shake your head.

I want to compine polymorphism with generics and want to define the serialisation in the generic. To do this I defined an interface "NamedType", an interface that yust defined the the property "public typeName: string" witch will be serialised (and will be transported over axios or some like that). So I will serialize some thing like an Array<T extends NamedType> (or List-, Tree- or Collection- Generic) of for example a "Person" - Class with can in detail be instance of "Teacher" or "Student" witch extends "Person". So up/inflate the generic Array<Person> has to translate the array elements with typeName="Teacher" to a instance of Teacher - Class and typeName="Student" to an Student - Class. I try to define this mapping in the Array<T extends NamedType> generic (or Tree<T extends NamedType> generic) and only want to define the mapper like (typeName: string) => T later on the level I want to use the generic.

I hope I make it clear, I dont expect a solution. I am still working on my challange.

Thanks,

Georg

janseng1970 avatar Dec 11 '19 08:12 janseng1970