elm-street-0.1.0.4: Crossing the road between Haskell and Elm
Safe HaskellNone
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 that strips the supported by elm-street data type name prefix from every field..

Example:

With the following JSON

{ "name": "John"
, "age": 42
}

it is decoded it 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 "{ \"name\": \"John\", \"age\": 42 }"
Just (User {userName = "John", userAge = 42})
>>> data VeryLongType = VeryLongType { vltName :: String, vltAge :: Int } deriving (Generic, Show)
>>> instance FromJSON VeryLongType where parseJSON = elmStreetParseJson
>>> decode @VeryLongType "{ \"name\": \"John\", \"age\": 42 }"
Just (VeryLongType {vltName = "John", vltAge = 42})

elmStreetToJson :: forall a. (Typeable a, Generic a, GToJSON Zero (Rep a)) => a -> Value Source #

Allows to create ToJSON instance that strips the supported by elm-street data type name prefix from every 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\"}"
>>> 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\"}"
>>> 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\"}"

elmStreetJsonOptions :: forall a. Typeable a => Options Source #

Options to strip type name from the field names.

Data type nameField nameStripped field name
UseruserNamename
AaaBbbCccabcFieldNamefieldName
Foofieldfield
Fieldfieldfield

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, GToJSON Zero (Rep a)) => ToJSON (ElmStreet a) Source # 
Instance details

Defined in Elm.Aeson

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

Defined in Elm.Aeson

(HasNoTypeVars a, HasLessThanEightUnnamedFields a, HasNoNamedSum a, Generic a, GenericElmDefinition (Rep a)) => Elm (ElmStreet a) Source # 
Instance details

Defined in Elm.Aeson