r/purescript • u/ondrap • Apr 07 '17
Purescript generic json decoding
I'm creating an interface between haskell backend and purescript frontend (with purescript-bridge) and I came across an issue with serializing some types. The purescript-bridge generates all the structures and I can parse/encode them in purescript using genericJsonEncode; however
- if the resulting type doesn't have a Generic instance (e.g. purescript-uuid), I cannot have a Generic instance of the whole type and I cannot use generic encoding/decoding
- if I need to do some special handling of the docoding process (e.g. decode ISO formatted time into DateTime), there is no way to override the decodeJson instance and do the special handling, as the genericDecodeJson doesn't seem to use these instances
The Generics and json encoding/decoding work differently in purescript, so I guess there should be some different way to solve these problems, I just didn't find any. Did I miss some obvious solution?
6
Upvotes
View all comments
Show parent comment
1
u/berdario Apr 18 '17 edited Apr 22 '17
I happened to define the IsForeign instance for (a newtype wrapper of) DateTime twice, so last week I finally got around and published a micro-package with only that
https://github.com/berdario/purescript-foreign-datetime
Generic.Rep works amazingly well... defining IsForeign for bigger and more complex data types becomes almost effortless
(example from my other WIP library: https://github.com/berdario/purescript-optimizely-api/blob/a7605e63e6aade03ee4b30e09890d430a73ae62c/src/Data/Optimizely/Project.purs#L51-L79 )