Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/minibill/elm-codec
Build JSON encoders and decoders with minimal boilerplate
https://github.com/minibill/elm-codec
Last synced: about 7 hours ago
JSON representation
Build JSON encoders and decoders with minimal boilerplate
- Host: GitHub
- URL: https://github.com/minibill/elm-codec
- Owner: miniBill
- License: mit
- Created: 2019-05-29T19:42:57.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T15:01:10.000Z (10 days ago)
- Last Synced: 2024-10-28T17:34:34.122Z (10 days ago)
- Language: Elm
- Homepage: https://package.elm-lang.org/packages/miniBill/elm-codec/latest
- Size: 117 KB
- Stars: 44
- Watchers: 5
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# elm-codecs
This package allows you to build pairs of JSON encoders (`a -> Value`) and decoders (`Decoder a`), collectively called a `Codec a`.
It supports all of the basic types, collections, records and even custom types! See an example at the bottom of this document.
## Design Goals
The design goal is to be as type safe as possible while keeping a nice API.
Using this package will greatly reduce the risk of unmatched encoders and decoders.The packages re-exposes the `Value` and `Decoder` types from `elm/json`, so you don't need to import them too.
## Learning Resources
Ask for help on the [Elm Slack](https://elmlang.herokuapp.com/).
You can also have a look at the `FAQ.md` file.
## Examples
See the `examples` folder for more examples.
### Basic usage
```elm
import Codec exposing (Codec, Value)codec : Codec (List Int)
codec =
Codec.list Codec.intencode : List Int -> Value
encode list =
Codec.encoder codec listdecodeString : String -> Result Codec.Error (List Int)
decodeString s =
Codec.decodeString codec s
```### Custom types
```elm
type Semaphore
= Red Int String
| Yellow
| Green FloatsemaphoreCodec : Codec Semaphore
semaphoreCodec =
Codec.custom
(\red yellow green value ->
case value of
Red i s ->
red i sYellow ->
yellowGreen f ->
green f
)
|> Codec.variant2 "Red" Red Codec.int Codec.string
|> Codec.variant0 "Yellow" Yellow
|> Codec.variant1 "Green" Green Codec.float
|> Codec.buildCustom
```