You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The superjson README provides a recipe for registering a custom transformer for a non-default data type:
import{Decimal}from"decimal.js"SuperJSON.registerCustom<Decimal,string>({isApplicable: (v): v is Decimal=>Decimal.isDecimal(v),serialize: v=>v.toJSON(),deserialize: v=>newDecimal(v),},'decimal.js');
However, the docs don't include any details about the registerClass and registerSymbol methods, nor do they include details about how a custom transformer can leverage the built-in transformers.
For example, given a CustomMap class the extends the built-in Map class and adds a customProperty and overridden get method, how can I use registerClass or registerCustom in a way that leverages the built-in serialization of the Map's entires?
classCustomMap<K,V>extendsMap<K,V>{publiccustomProperty: string;publicconstructor(customProperty: string,entries?: ReadonlyArray<readonly[K,V]>|null){super(entries);this.customProperty=customProperty;}publicoverrideget(key: K): V{constvalue=super.get(key);if(!value){thrownewError(`Value with key ${key} does not exist.`);}returnvalue;}}
Using registerClass(CustomMap) doesn't serialize the CustomMap entries:
importSuperJSONfrom"superjson";SuperJSON.registerClass(CustomMap);constmap=newCustomMap([["a",1],["b",2],]);const{ json, meta }=SuperJSON.serialize(map);/*json = { customProperty: "My Map"};meta = { values: [ ["class", "CustomMap"] ]};*/
Using registerCustom, it's unclear how you can leverage SuperJson's built-in Map transformer to serialize and deserialize the CustomMap class:
SuperJSON.registerCustom<CustomMap<any,any>,string>({isApplicable: (v): v is CustomMap<any,any>=>vinstanceofCustomMap,serialize: (v)=>"",// TODOdeserialize: (v)=>newCustomMap(/* TODO */),},"CustomMap");
The text was updated successfully, but these errors were encountered:
Hey @jasongerbes! registerClass and registerCustom aren't documented well, I agree. If you want to open a PR that adds docs for that, i'd be happy to review it!
Regarding the CustomMap case, I don't see an easy way of leveraging SuperJSON's built-in Map transformer, either. registerCustom is probably the best choice. You could try to make a Map out of your CustomMap and then recursively call SuperJSON.serialize(v) inside of serialize. Would that help?
The superjson README provides a recipe for registering a custom transformer for a non-default data type:
However, the docs don't include any details about the
registerClass
andregisterSymbol
methods, nor do they include details about how a custom transformer can leverage the built-in transformers.For example, given a
CustomMap
class the extends the built-inMap
class and adds acustomProperty
and overriddenget
method, how can I useregisterClass
orregisterCustom
in a way that leverages the built-in serialization of the Map's entires?Using
registerClass(CustomMap)
doesn't serialize theCustomMap
entries:Using
registerCustom
, it's unclear how you can leverage SuperJson's built-in Map transformer to serialize and deserialize theCustomMap
class:The text was updated successfully, but these errors were encountered: