elm-bridge
Derive Elm types and Json code from Haskell types, using aeson's options
https://github.com/agrafix/elm-bridge
| LTS Haskell 24.63: | 0.8.5 |
| Stackage Nightly 2026-10-15: | 0.8.6 |
| Latest on Hackage: | 0.8.6 |
elm-bridge-0.8.6@sha256:af4377aa9701db946f5be2bfb2569d0278f7acdb08a102fa65fb677160e97d4d,2444Module documentation for 0.8.6
Elm Bridge
Intro
Hackage: elm-bridge
Building the bridge from Haskell to Elm and back. Define types once, use on both sides and enjoy easy (de)serialisation. Cheers!
This version of the package only supports Elm 0.19. The generated code works with Elm 0.19.1 and Elm 0.19.2, and the end-to-end tests run on Elm 0.19.2. Version 0.5.2 supports Elm 0.18, and Version 0.3.0.2 supports Elm 0.16 and Elm 0.17.
Note that the bartavelle/json-helpers package, with version >= 1.2.0, is expected by the generated Elm modules.
Usage
{-# LANGUAGE TemplateHaskell #-}
import Elm.Derive
import Elm.Module
import Data.Proxy
data Foo
= Foo
{ f_name :: String
, f_blablub :: Int
} deriving (Show, Eq)
deriveBoth defaultOptions ''Foo
main :: IO ()
main =
putStrLn $ makeElmModule "Foo"
[ DefineElm (Proxy :: Proxy Foo)
]
Output will be:
module Foo where
import Json.Decode
import Json.Decode exposing ((:=))
import Json.Encode
import Json.Helpers exposing (..)
type alias Foo =
{ f_name: String
, f_blablub: Int
}
jsonDecFoo : Json.Decode.Decoder ( Foo )
jsonDecFoo =
("f_name" := Json.Decode.string) `Json.Decode.andThen` \pf_name ->
("f_blablub" := Json.Decode.int) `Json.Decode.andThen` \pf_blablub ->
Json.Decode.succeed {f_name = pf_name, f_blablub = pf_blablub}
jsonEncFoo : Foo -> Value
jsonEncFoo val =
Json.Encode.object
[ ("f_name", Json.Encode.string val.f_name)
, ("f_blablub", Json.Encode.int val.f_blablub)
]
The default type conversion rules (see Elm.Module.defaultAlterations) convert Haskell types that do not exist in Elm. E.g. Text becomes String, Map String v becomes Dict String v and Either a b becomes Result a b.
Also, there are functions Elm.Json.stringSerForSimpleAdt and Elm.Json.stringParserForSimpleAdt to generate functions for your non-JSON ADT types.
For more usage examples check the tests or the examples dir.
Install
Haskell
- Using cabal:
cabal install elm-bridge - From Source:
git clone https://github.com/agrafix/elm-bridge.git && cd elm-bridge && cabal install
Elm
elm package install bartavelle/json-helpers
or, for Elm 0.19:
elm install bartavelle/json-helpers
Contribute
To run the tests, you need stack, node, Elm 0.19.2 and elm-test 0.19.2:
stack test
$(stack path --dist-dir)/build/end-to-end-tests/end-to-end-tests test/current-end-to-end/tests/MyTests.elm
cd test/current-end-to-end && npx [email protected]
The second command generates an Elm module with tests that decode and encode values that aeson produced. The third command compiles and runs these tests.
Pull requests are welcome! Please consider creating an issue beforehand, so we can discuss what you would like to do. Code should be written in a consistent style throughout the project. Avoid whitespace that is sensible to conflicts. (E.g. alignment of = signs in functions definitions) Note that by sending a pull request you agree that your contribution can be released under the BSD3 License as part of the elm-bridge package or related packages.
Changes
Unreleased
Either a bis now generated as Elm’sResult a bbydefaultAlterations, with JSON decoders and encoders that match aeson’s{"Left": ..}/{"Right": ..}encoding (#63).- Fix decoders for sum type constructors with named fields. Since v0.8.5 they referenced the type name as a constructor, which does not compile in Elm.
- Fix decoders for newtypes and sum type constructors with fields whose names are Elm reserved words (e.g.
type). - Support Elm 0.19.2. The end-to-end tests now run on Elm 0.19.2 in GitHub Actions, which replaces Travis CI.
v0.8.3
- Support Int32 and Int64, from domenkozar
v0.8.0
- Directly support integer keys in dictionnaries, thanks to odanoboru
v0.7.0
- Support for GHC 9
v0.6.0
- Support for Elm 0.19
v0.5.2
- Fix a bug about tuples.
v0.5.0
- Large change for sum types that used
constructorTagModifier. The generated types are now unaffected! This is a breaking change for those who used this feature.
v0.4.2
Drop support for aeson < 1.
Add support for aeson == 1.2.*
v0.4.1
Bugfixes
- Fixed support for Elm 0.18 (see issue #17)
v0.4.0
New features
- Support for Elm 0.18
- Dropped support for Elm 0.17 and Elm 0.16
v0.3.0
New features
- Support for Elm 0.17
v0.2.2
New features
- The Elm JSON encoders and decoders now match
aesonmore closely. In partlicular, single constructor sum types are now encoded without the constructor. Also, theaeson0.11 optionunwrapUnaryRecordsis now supported.
Bugfixes
- Fixed Elm type error in encoders for types like
[Map String v](0.2.1.2).
v0.2.1
New features
- The template Haskell derivation functions now take
aesonOptiontype instead of a custom type. This change makes it easier to synchronize the Haskell and Elm code. - The generated Elm code can be personalized. Helpers functions assist in converting type names, and defining which type will be newtyped.
Notes
- The generated Elm code depends on the bartavelle/json-helpers package.