on master help sign in
elm-bridge/README.md
94 lines 2.8 kB history raw
1Elm Bridge
2=====
4[![Build Status](https://travis-ci.org/agrafix/elm-bridge.svg)](https://travis-ci.org/agrafix/elm-bridge)
6[![Hackage Deps](https://img.shields.io/hackage-deps/v/elm-bridge.svg)](http://packdeps.haskellers.com/reverse/elm-bridge)
8## Intro
10Hackage: [elm-bridge](http://hackage.haskell.org/package/elm-bridge)
12Building the bridge from [Haskell](http://haskell.org) to [Elm](http://elm-lang.org) and back. Define types once, use on both sides and enjoy easy (de)serialisation. Cheers!
14This version of the package only supports Elm 0.19. Version 0.5.2 supports Elm 0.18, and Version 0.3.0.2 supports Elm 0.16 and Elm 0.17.
16Note that the [bartavelle/json-helpers](http://package.elm-lang.org/packages/bartavelle/json-helpers/latest/) package, with version >= 1.2.0, is expected by the generated Elm modules.
18## Usage
20```haskell
21{-# LANGUAGE TemplateHaskell #-}
22import Elm.Derive
23import Elm.Module
25import Data.Proxy
27data Foo
28 = Foo
29 { f_name :: String
30 , f_blablub :: Int
31 } deriving (Show, Eq)
33deriveBoth defaultOptions ''Foo
35main :: IO ()
36main =
37 putStrLn $ makeElmModule "Foo"
38 [ DefineElm (Proxy :: Proxy Foo)
39 ]
41```
43Output will be:
45```elm
46module Foo where
48import Json.Decode
49import Json.Decode exposing ((:=))
50import Json.Encode
51import Json.Helpers exposing (..)
54type alias Foo =
55 { f_name: String
56 , f_blablub: Int
57 }
59jsonDecFoo : Json.Decode.Decoder ( Foo )
60jsonDecFoo =
61 ("f_name" := Json.Decode.string) `Json.Decode.andThen` \pf_name ->
62 ("f_blablub" := Json.Decode.int) `Json.Decode.andThen` \pf_blablub ->
63 Json.Decode.succeed {f_name = pf_name, f_blablub = pf_blablub}
65jsonEncFoo : Foo -> Value
66jsonEncFoo val =
67 Json.Encode.object
68 [ ("f_name", Json.Encode.string val.f_name)
69 , ("f_blablub", Json.Encode.int val.f_blablub)
70 ]
71```
73Also, there are functions `Elm.Json.stringSerForSimpleAdt` and `Elm.Json.stringParserForSimpleAdt` to generate functions for your non-JSON ADT types.
75For more usage examples check the tests or the examples dir.
77## Install
79### Haskell
81* Using cabal: `cabal install elm-bridge`
82* From Source: `git clone https://github.com/agrafix/elm-bridge.git && cd elm-bridge && cabal install`
84### Elm
86* `elm package install bartavelle/json-helpers`
88or, for Elm 0.19:
90* `elm install bartavelle/json-helpers`
92## Contribute
94Pull 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.