Foreign.Generic
- Package
- purescript-foreign-generic
- Repository
- paf31/purescript-foreign-generic
#genericDecode Source
genericDecode :: forall rep a. Generic a rep => GenericDecode rep => Options -> Foreign -> F aRead a value which has a Generic type.
#genericEncode Source
genericEncode :: forall rep a. Generic a rep => GenericEncode rep => Options -> a -> ForeignGenerate a Foreign value compatible with the genericDecode function.
#decodeJSON Source
decodeJSON :: forall a. Decode a => String -> F aDecode a JSON string using a Decode instance.
#encodeJSON Source
encodeJSON :: forall a. Encode a => a -> StringEncode a JSON string using an Encode instance.
#genericDecodeJSON Source
genericDecodeJSON :: forall rep a. Generic a rep => GenericDecode rep => Options -> String -> F aRead a value which has a Generic type from a JSON String
#genericEncodeJSON Source
genericEncodeJSON :: forall rep a. Generic a rep => GenericEncode rep => Options -> a -> StringWrite a value which has a Generic type as a JSON String
Re-exports from Foreign
#ForeignError Source
data ForeignErrorA type for foreign type errors
Constructors
ForeignError StringTypeMismatch String StringErrorAtIndex Int ForeignErrorErrorAtProperty String ForeignError
Instances
#Foreign Source
data Foreign :: TypeA type for foreign data.
Foreign data is data from any external unknown or unreliable source, for which it cannot be guaranteed that the runtime representation conforms to that of any particular type.
Suitable applications of Foreign are
- To represent responses from web services
- To integrate with external JavaScript libraries.
#F Source
type F = Except MultipleErrorsAn error monad, used in this library to encode possible failures when dealing with foreign data.
The Alt instance for Except allows us to accumulate errors,
unlike Either, which preserves only the last error.
Re-exports from Foreign.Generic.Class
#SumEncoding Source
data SumEncodingThe 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
TaggedObject { constructorTagTransform :: String -> String, contentsFieldName :: String, tagFieldName :: String }
#Options Source
type Options = { fieldTransform :: String -> String, sumEncoding :: SumEncoding, unwrapSingleArguments :: Boolean, unwrapSingleConstructors :: Boolean }Encoding/Decoding options which can be used to customize
Decode and Encode instances which are derived via
Generic (see genericEncode and genericDecode).
#Decode Source
class Decode a whereThe 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 whereThe 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
#GenericDecode Source
class GenericDecode a Instances
GenericDecode NoConstructors(IsSymbol name, GenericDecodeArgs rep, GenericCountArgs rep) => GenericDecode (Constructor name rep)(GenericDecode a, GenericDecode b) => GenericDecode (Sum a b)
#GenericEncode Source
class GenericEncode a Instances
GenericEncode NoConstructors(IsSymbol name, GenericEncodeArgs rep) => GenericEncode (Constructor name rep)(GenericEncode a, GenericEncode b) => GenericEncode (Sum a b)
#defaultOptions Source
defaultOptions :: OptionsDefault decoding/encoding options:
- Represent sum types as records with
tagandcontentsfields - Unwrap single arguments
- Don't unwrap single constructors
- Use the constructor names as-is
- Use the field names as-is