Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Options used to derive FromJSON/ToJSON instance. These options generally
comply to elm-street
rules regarding names.
Synopsis
- elmStreetParseJson :: forall a. (Typeable a, Generic a, GFromJSON Zero (Rep a)) => Value -> Parser a
- elmStreetParseJsonWith :: forall a. (Generic a, GFromJSON Zero (Rep a)) => CodeGenOptions -> Value -> Parser a
- elmStreetToJson :: forall a. (Typeable a, Generic a, GToJSON Zero (Rep a)) => a -> Value
- elmStreetToJsonWith :: forall a. (Generic a, GToJSON Zero (Rep a)) => CodeGenOptions -> a -> Value
- elmStreetJsonOptions :: CodeGenOptions -> Options
- newtype ElmStreet a = ElmStreet {
- unElmStreet :: a
Documentation
elmStreetParseJson :: forall a. (Typeable a, Generic a, GFromJSON Zero (Rep a)) => Value -> Parser a Source #
Allows to create FromJSON
instance for data types supported by
elm-street
. Strips data type name prefix from every field.
Example:
The following JSON
{ "name": "John" , "age": 42 }
is decoded in the following way for each of the specified types:
Haskell data type | Parsed type |
---|---|
data User = User
{ userName :: String
, userAge :: Int
}
|
User
{ userName = "John"
, userAge = 42
}
|
data LongUser = LongUser
{ luName :: String
, luAge :: Int
}
|
LongUser
{ luName = "John"
, luAge = 42
}
|
data SimpleUser = SimpleUser
{ name :: String
, age :: Int
}
|
SimpleUser
{ name = "John"
, age = 42
}
|
>>>
data User = User { userName :: String, userAge :: Int } deriving (Generic, Show)
>>>
instance FromJSON User where parseJSON = elmStreetParseJson
>>>
decode @User "{\"age\":42,\"name\":\"John\",\"tag\":\"User\"}"
Just (User {userName = "John", userAge = 42})
>>>
data VeryLongType = VeryLongType { vltName :: String, vltAge :: Int } deriving (Generic, Show)
>>>
instance FromJSON VeryLongType where parseJSON = elmStreetParseJson
>>>
decode @VeryLongType "{\"age\":42,\"name\":\"John\",\"tag\":\"VeryLongType\"}"
Just (VeryLongType {vltName = "John", vltAge = 42})
elmStreetParseJsonWith :: forall a. (Generic a, GFromJSON Zero (Rep a)) => CodeGenOptions -> Value -> Parser a Source #
Use custom CodeGenOptions
to customize the behavior of derived FromJSON instance.
elmStreetToJson :: forall a. (Typeable a, Generic a, GToJSON Zero (Rep a)) => a -> Value Source #
Allows to create ToJSON
instance for types supported by elm-street
.
Strips type name prefix from every record field.
>>>
data User = User { userName :: String, userAge :: Int } deriving (Generic, Show)
>>>
instance ToJSON User where toJSON = elmStreetToJson
>>>
encode $ User { userName = "John", userAge = 42 }
"{\"age\":42,\"name\":\"John\",\"tag\":\"User\"}"
>>>
data VeryLongType = VeryLongType { vltName :: String, vltAge :: Int } deriving (Generic, Show)
>>>
instance ToJSON VeryLongType where toJSON = elmStreetToJson
>>>
encode $ VeryLongType {vltName = "John", vltAge = 42}
"{\"age\":42,\"name\":\"John\",\"tag\":\"VeryLongType\"}"
>>>
data User = User { name :: String, age :: Int } deriving (Generic, Show)
>>>
instance ToJSON User where toJSON = elmStreetToJson
>>>
encode $ User { name = "John", age = 42 }
"{\"age\":42,\"name\":\"John\",\"tag\":\"User\"}"
elmStreetToJsonWith :: forall a. (Generic a, GToJSON Zero (Rep a)) => CodeGenOptions -> a -> Value Source #
Use custom CodeGenOptions
to customize the behavior of derived ToJSON instance.
elmStreetJsonOptions :: CodeGenOptions -> Options Source #
Build elm-street
compatible Options
from CodeGenOptions
.
Newtype for reusing in DerivingVia
.
In order to use it with your type MyType
add the following deriving to your type:
deriving (Elm, ToJSON, FromJSON) via ElmStreet MyType
ElmStreet | |
|
Instances
(Typeable a, Generic a, GFromJSON Zero (Rep a)) => FromJSON (ElmStreet a) Source # | |
(Typeable a, Generic a, GToJSON Zero (Rep a)) => ToJSON (ElmStreet a) Source # | |
(ElmStreetGenericConstraints a, Typeable a) => Elm (ElmStreet a) Source # | |
Defined in Elm.Aeson toElmDefinition :: Proxy (ElmStreet a) -> ElmDefinition Source # |