Module

Foreign.Generic.Class

Package
purescript-foreign-generic
Repository
paf31/purescript-foreign-generic

#Options Source

type Options = { sumEncoding :: SumEncoding, unwrapSingleConstructors :: Boolean, unwrapSingleArguments :: Boolean, fieldTransform :: String -> String }

Encoding/Decoding options which can be used to customize Decode and Encode instances which are derived via Generic (see genericEncode and genericDecode).

#SumEncoding Source

data SumEncoding

The encoding of sum types for your type. TaggedObjects will be encoded in the form { [tagFieldName]: "ConstructorTag", [contentsFieldName]: "Contents"}. constructorTagTransform can be provided to transform the constructor tag to a form you use, e.g. toLower/toUpper.

Constructors

#defaultOptions Source

defaultOptions :: Options

Default decoding/encoding options:

  • Represent sum types as records with tag and contents fields
  • Unwrap single arguments
  • Don't unwrap single constructors
  • Use the constructor names as-is
  • Use the field names as-is

#Decode Source

class Decode a  where

The Decode class is used to generate decoding functions of the form Foreign -> F a using generics-rep deriving.

First, derive Generic for your data:

import Data.Generic.Rep

data MyType = MyType ...

derive instance genericMyType :: Generic MyType _

You can then use the genericDecode and genericDecodeJSON functions to decode your foreign/JSON-encoded data.

Members

Instances

#Encode Source

class Encode a  where

The Encode class is used to generate encoding functions of the form a -> Foreign using generics-rep deriving.

First, derive Generic for your data:

import Data.Generic.Rep

data MyType = MyType ...

derive instance genericMyType :: Generic MyType _

You can then use the genericEncode and genericEncodeJSON functions to encode your data as JSON.

Members

Instances

#DecodeWithOptions Source

class DecodeWithOptions a  where

When deriving En/Decode instances using Generic, we want the Options object to apply to the outermost record type(s) under the data constructors.

For this reason, we cannot use En/Decode directly when we reach an Argument during generic traversal of a type, because it might be a record type. Instead, we need to peel off any record type(s) and apply the appropriate Options before we can delegate to En/Decode, which can bake in its own Options.

Members

Instances

#EncodeWithOptions Source

class EncodeWithOptions a  where

See the comment on DecodeWithOptions.

Members

Instances

#DecodeRecord Source

class DecodeRecord r rl | rl -> r where

Members

Instances

#EncodeRecord Source

class EncodeRecord r rl | rl -> r where

Members

Instances

#GenericDecodeArgs Source