| Safe Haskell | None |
|---|
Data.Reflection.Extras
- using :: forall p a. ReifiableConstraint p => Def p a -> (p a => a) -> a
- usingT :: forall p f a. ReifiableConstraint p => Def p a -> (p a => f a) -> f a
- reifyInstance :: Def p a -> (forall s. Reifies s (Def p a) => Proxy s -> r) -> r
- with :: forall p a. Def p a -> (forall s. Reifies s (Def p a) => Lift p s a) -> a
- data Lift p s a
- class ReifiableConstraint p where
- class Reifies s a | s -> a where
- reflect :: proxy s -> a
- class FromJSON a where
- class ToJSON a where
Documentation
using :: forall p a. ReifiableConstraint p => Def p a -> (p a => a) -> aSource
Choose a dictionary for a local type class instance.
>>>using (Monoid (+) 0) $ mempty <> 10 <> 12> 12
usingT :: forall p f a. ReifiableConstraint p => Def p a -> (p a => f a) -> f aSource
Instances
| Functor (Lift p s) | |
| Applicative (Lift p s) | |
| Reifies * s (Def Bounded a) => Bounded (Lift Bounded s a) | |
| Reifies * s (Def Enum a) => Enum (Lift Enum s a) | |
| Reifies * s (Def Eq a) => Eq (Lift Eq s a) | |
| Reifies * s (Def Ord a) => Eq (Lift Ord s a) | |
| Reifies * s (Def Real a) => Eq (Lift Real s a) | |
| Reifies * s (Def Num a) => Num (Lift Num s a) | |
| Reifies * s (Def Real a) => Num (Lift Real s a) | |
| Reifies * s (Def Ord a) => Ord (Lift Ord s a) | |
| Reifies * s (Def Real a) => Ord (Lift Real s a) | |
| Reifies * s (Def Read a) => Read (Lift Read s a) | |
| Reifies * s (Def Real a) => Real (Lift Real s a) | |
| Reifies * s (Def Show a) => Show (Lift Show s a) | |
| Reifies * s (Def ToJSON a) => ToJSON (Lift ToJSON s a) | |
| Reifies * s (Def FromJSON a) => FromJSON (Lift FromJSON s a) | |
| Reifies * s (Def Monoid a) => Monoid (Lift Monoid s a) |
class ReifiableConstraint p whereSource
class Reifies s a | s -> a where
class FromJSON a where
A type that can be converted from JSON, with the possibility of failure.
When writing an instance, use empty, mzero, or fail to make a
conversion fail, e.g. if an Object is missing a required key, or
the value is of the wrong type.
An example type and instance:
{-# LANGUAGE OverloadedStrings #-}
data Coord { x :: Double, y :: Double }
instance FromJSON Coord where
parseJSON (Object v) = Coord <$>
v .: "x" <*>
v .: "y"
-- A non-Object value is of the wrong type, so use mzero to fail.
parseJSON _ = mzero
Note the use of the OverloadedStrings language extension which enables
Text values to be written as string literals.
Instead of manually writing your FromJSON instance, there are three options
to do it automatically:
- Data.Aeson.TH provides template-haskell functions which will derive an instance at compile-time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
fromJSONfunction that parses to any type which is an instance ofData. - If your compiler has support for the
DeriveGenericandDefaultSignatureslanguage extensions,parseJSONwill have a default generic implementation.
To use this, simply add a deriving clause to your datatype and
declare a GenericFromJSON instance for your datatype without giving a definition
for parseJSON.
For example the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord { x :: Double, y :: Double } deriving Generic
instance FromJSON Coord
Note that, instead of using DefaultSignatures, it's also possible
to parameterize the generic decoding using genericParseJSON applied
to your encoding/decoding Options:
instance FromJSON Coord where
parseJSON = genericParseJSON defaultOptions
Instances
class ToJSON a where
A type that can be converted to JSON.
An example type and instance:
{-# LANGUAGE OverloadedStrings #-}
data Coord { x :: Double, y :: Double }
instance ToJSON Coord where
toJSON (Coord x y) = object ["x" .= x, "y" .= y]
Note the use of the OverloadedStrings language extension which enables
Text values to be written as string literals.
Instead of manually writing your ToJSON instance, there are three options
to do it automatically:
- Data.Aeson.TH provides template-haskell functions which will derive an instance at compile-time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
toJSONfunction that accepts any type which is an instance ofData. - If your compiler has support for the
DeriveGenericandDefaultSignatureslanguage extensions (GHC 7.2 and newer),toJSONwill have a default generic implementation.
To use the latter option, simply add a deriving clause to your
datatype and declare a GenericToJSON instance for your datatype without giving a
definition for toJSON.
For example the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord { x :: Double, y :: Double } deriving Generic
instance ToJSON Coord
Note that, instead of using DefaultSignatures, it's also possible
to parameterize the generic encoding using genericToJSON applied
to your encoding/decoding Options:
instance ToJSON Coord where
toJSON = genericToJSON defaultOptions
Instances