Skip to content

Commit

Permalink
Add docs about DerivingVia and aeson options (#58)
Browse files Browse the repository at this point in the history
* Add docs about DerivingVia and aeson options

* Update docs
  • Loading branch information
chshersh authored and vrom911 committed Feb 26, 2019
1 parent 1eaafff commit 493ab8f
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,33 @@ applications.
In order to use `elm-street` features, you need to perform the following steps:

1. Add `elm-street` to the dependencies of your Haskell package.
2. Derive the `Elm` typeclass for relevant data types. This can be done like this:
2. Derive the `Elm` typeclass for relevant data types. You also need to derive
JSON instances according to `elm-street` naming scheme.
This can be done like this:
```haskell
import Elm (Elm)
import Elm (Elm, elmStreetParseJson, elmStreetToJson)

data User = User
{ userName :: Text
, userAge :: Int
} deriving (Generic)
deriving anyclass (Elm)

instance ToJSON User where toJSON = elmStreetToJson
instance FromJSON User where parseJSON = elmStreetParseJson
```
> **NOTE:** This requires extensions `-XDerivingStrategies`, `-XDeriveGeneric`, `-XDeriveAnyClass`.
Alternatively you can use `-XDerivingVia` to remove some boilerplate (available since GHC 8.6.1):
```haskell
import Elm (Elm, ElmStreet (..))

data User = User
{ userName :: Text
, userAge :: Int
} deriving (Generic)
deriving (Elm, ToJSON, FromJSON) via ElmStreet User
```
3. Create list of all types you want to expose to Elm:
```haskell
type Types =
Expand Down Expand Up @@ -124,9 +140,9 @@ limitations, specifically:
```
6. Generated JSON encoders and decoders are consistent with default behavior of
derived `ToJSON/FromJSON` instances from the `aeson` library except you need
to strip record field prefixes (fortunately, this also can be done generically).

TODO: link to generic options
to strip record field prefixes. Fortunately, this also can be done
generically. You can use functions from `Elm.Aeson` module to derive `JSON`
instances from the `aeson` package.
7. Only `UTCTime` Haskell data type is supported and it's translated to `Posix`
type in Elm.

Expand Down

0 comments on commit 493ab8f

Please sign in to comment.