https://github.com/norpan/elm-json-patch
JSON Patch (RFC 6902) for Elm
https://github.com/norpan/elm-json-patch
Last synced: 5 months ago
JSON representation
JSON Patch (RFC 6902) for Elm
- Host: GitHub
- URL: https://github.com/norpan/elm-json-patch
- Owner: norpan
- License: bsd-3-clause
- Created: 2017-03-21T22:56:58.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-06-14T12:06:02.000Z (almost 4 years ago)
- Last Synced: 2024-05-09T13:37:22.152Z (about 1 year ago)
- Language: Elm
- Size: 11.7 KB
- Stars: 9
- Watchers: 2
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON Patch
This library implements [RFC 6902](https://tools.ietf.org/html/rfc6902 RFC6902), JSON Patch (and [RFC 6901](https://tools.ietf.org/html/rfc6901), JSON Pointer) for Elm.
## Usage
Let's say you previously have gotten a JSON document as a `Value` from the server or via a port. Now you get a `Value` that is a JSON Patch for that document.```elm
newDocument =
Json.Decoder.decodeValue Json.Patch.decoder patch
|> Result.andThen (\p -> Json.Patch.apply p document)
```Patching needs to be done on the `Value` type, due to the type system (records can't be accessed by field name in Elm).
However, if you have an encoder/decoder pair for your Elm type, you can patch the Elm type like this:
```elm
newElmData =
Json.Decoder.decodeValue Json.Patch.decoder patch
|> Result.andThen
(\p ->
dataEncoder elmData
|> Json.Patch.apply p
|> Result.andThen (Json.Decoder.decodeValue dataDecoder)
)
```