Module

Data.Codec.JSON.Generic

Package
purescript-codec-json
Repository
garyb/purescript-codec-json

#nullarySum Source

nullarySum :: forall a r. Generic a r => NullarySumCodec r => String -> Codec a

Encodes nullary sums with a Generic instance as strings that match the constructor names.

import JSON as J
import Data.Codec.JSON as CJ
import Data.Codec.JSON.Generic as CJG

data MySum = Ctor1 | Ctor2 | Ctor3
derive instance Generic MySum _

CJ.encode (CJG.nullarySum "MySum") Ctor1 == J.fromString "Ctor1"
CJ.decode (CJG.nullarySum "MySum") (J.fromString "Ctor3") == Right Ctor3