Module
Data.Codec.Argonaut.Variant
- Package
- purescript-codec-argonaut
- Repository
- garyb/purescript-codec-argonaut
#variant Source
variant :: JsonCodec (Variant ())
Allows building codecs for variants in combination with variantCase.
Commonly used to write decoders for sum-types, by providing a mapping from
and to a Variant from that type and then using dimap
.
codecMaybe ∷ ∀ a. JA.JsonCodec a → JA.JsonCodec (Maybe a)
codecMaybe codecA =
dimap toVariant fromVariant
(JAV.variant
# JAV.variantCase _Just (Right codecA)
# JAV.variantCase _Nothing (Left unit))
where
toVariant = case _ of
Just a → V.inj _Just a
Nothing → V.inj _Nothing unit
fromVariant = V.case_
# V.on _Just Just
# V.on _Nothing (const Nothing)
_Just = SProxy ∷ SProxy "just"
_Nothing = SProxy ∷ SProxy "nothing"
#VariantCodec Source
class VariantCodec (rl :: RowList) (ri :: Row Type) (ro :: Row Type) | rl -> ri ro where
Members
variantCodec :: RLProxy rl -> Record ri -> JsonCodec (Variant ro)
Instances
VariantCodec Nil () ()
(VariantCodec rs ri' ro', Cons sym (Either a (JsonCodec a)) ri' ri, Cons sym a ro' ro, IsSymbol sym, TypeEquals co (Either a (JsonCodec a))) => VariantCodec (Cons sym co rs) ri ro
#variantMatch Source
variantMatch :: forall ro ri rl. RowToList ri rl => VariantCodec rl ri ro => Record ri -> JsonCodec (Variant ro)