Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module offers generic conversions to and from JSON Value
s
for data types with a Generic
instance.
The structure of the generated JSON is meant to be close to idiomatic JSON. This means:
- Enumerations are converted to JSON strings.
- Record fields become JSON keys.
- Data types with one unlabeled field convert to just that field.
- Data types with multiple unlabeled fields become arrays.
- Multiple constructors are represented by keys.
Maybe
values are either an absent key, or the value.
See 'tests/Main.hs' for more examples.
Synopsis
- gtoJson :: forall a. (Generic a, GtoJson (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => a -> Value
- gparseJson :: forall a. (Generic a, GfromJson (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Value -> Parser a
- class GtoJson f where
- class GfromJson f where
- formatLabel :: Settings -> Text -> Text
- data Settings = Settings {}
- defaultSettings :: Settings
- gtoJsonWithSettings :: forall a. (Generic a, GtoJson (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Settings -> a -> Value
- gparseJsonWithSettings :: forall a. (Generic a, GfromJson (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Settings -> Value -> Parser a
Documentation
gtoJson :: forall a. (Generic a, GtoJson (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => a -> Value Source #
gparseJson :: forall a. (Generic a, GfromJson (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Value -> Parser a Source #
class GtoJson f where Source #
Class for converting the functors from GHC.Generics to JSON.
You generally don't need to give any custom instances. Just add
'deriving Generic' and call gToJson
.
gtoJSONf :: Settings -> Bool -> Bool -> f a -> Either [Value] [(Text, Value)] Source #
Generically show a functor as a JSON value. The first argument tells us if there are multiple constructors in the data type. The second indicates if this data type is an enumeration (only empty constructors). A functor is then converted to either a list of values (for non-labeled fields) or a list of String/value pairs (for labeled fields).
Instances
GtoJson (U1 :: Type -> Type) Source # | |
ToJSON c => GtoJson (K1 a c :: Type -> Type) Source # | |
(GtoJson f, GtoJson g) => GtoJson (f :+: g) Source # | |
(GtoJson f, GtoJson g) => GtoJson (f :*: g) Source # | |
GtoJson f => GtoJson (M1 D c f) Source # | |
(Constructor c, GtoJson f) => GtoJson (M1 C c f) Source # | |
(Selector c, ToJSON a) => GtoJson (M1 S c (K1 i (Maybe a) :: Type -> Type)) Source # | |
(Selector c, GtoJson f) => GtoJson (M1 S c f) Source # | |
class GfromJson f where Source #
Class for parsing the functors from GHC.Generics from JSON.
You generally don't need to give any custom instances. Just add
'deriving Generic' and call gFromJson
.
gparseJSONf :: Settings -> Bool -> Bool -> Bool -> StateT [Value] Parser (f a) Source #
Generically read a functor from a JSON value. The first argument tells us if there are multiple constructors in the data type. The second indicates if we've already detected that this data type has multiple constructors. When this is False, the (:*:) puts the fields in the state. The third indicates if this data type is an enumeration (only empty constructors). The third is a function for parsing the recursive positions. A JSON value is then parsed to either a functor, or a failure.
Instances
GfromJson (U1 :: Type -> Type) Source # | |
FromJSON c => GfromJson (K1 a c :: Type -> Type) Source # | |
(GfromJson f, GfromJson g) => GfromJson (f :+: g) Source # | |
(GfromJson f, GfromJson g) => GfromJson (f :*: g) Source # | |
GfromJson f => GfromJson (M1 D c f) Source # | |
(Constructor c, GfromJson f) => GfromJson (M1 C c f) Source # | |
(Selector c, FromJSON a) => GfromJson (M1 S c (K1 i (Maybe a) :: Type -> Type)) Source # | |
(Selector c, GfromJson f) => GfromJson (M1 S c f) Source # | |
formatLabel :: Settings -> Text -> Text Source #
Lowercases the first letter and strips leading and trailing underscores.