elm-street-0.2.2.0: Crossing the road between Haskell and Elm
Safe HaskellSafe-Inferred
LanguageHaskell2010

Elm.Aeson

Description

Options used to derive FromJSON/ToJSON instance. These options generally comply to elm-street rules regarding names.

Synopsis

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 typeParsed 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.

newtype ElmStreet a Source #

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

Constructors

ElmStreet 

Fields

Instances

Instances details
(Typeable a, Generic a, GFromJSON Zero (Rep a)) => FromJSON (ElmStreet a) Source # 
Instance details

Defined in Elm.Aeson

(Typeable a, Generic a, GToJSON Zero (Rep a)) => ToJSON (ElmStreet a) Source # 
Instance details

Defined in Elm.Aeson

(ElmStreetGenericConstraints a, Typeable a) => Elm (ElmStreet a) Source # 
Instance details

Defined in Elm.Aeson