|
1 | 1 | # rescript-json-combinators |
2 | 2 | Combinator library for JSON decoding and encoding. |
| 3 | + |
| 4 | +[](https://npmjs.org/@glennsl/rescript-json-combinators) |
| 5 | +[](https://github.com/glennsl/rescript-json-combinators/issues) |
| 6 | +[](https://github.com/glennsl/rescript-json-combinators/commits/master) |
| 7 | + |
| 8 | + |
| 9 | +## Example |
| 10 | + |
| 11 | +```rescript |
| 12 | +type point = { |
| 13 | + x: int, |
| 14 | + y: int, |
| 15 | +} |
| 16 | +
|
| 17 | +type polyline = { |
| 18 | + points: array<point>, |
| 19 | + thickness: option<int>, |
| 20 | +} |
| 21 | +
|
| 22 | +module Decode = { |
| 23 | + open Json.Decode |
| 24 | +
|
| 25 | + let point = object(field => { |
| 26 | + x: field.required(. "x", int), |
| 27 | + y: field.required(. "y", int), |
| 28 | + }) |
| 29 | +
|
| 30 | + let polyline = object(field => { |
| 31 | + points: field.required(. "points", array(point)), |
| 32 | + thickness: field.optional(. "thickness", int), |
| 33 | + }) |
| 34 | +} |
| 35 | +
|
| 36 | +let data = `{ |
| 37 | + "points": [ |
| 38 | + { "x": 1, "y": -4 }, |
| 39 | + { "x": 5, "y": 8 } |
| 40 | + ] |
| 41 | +}` |
| 42 | +
|
| 43 | +let _ = data->Js.Json.parseExn->Json.decode(Decode.polyline)->Js.log |
| 44 | +``` |
| 45 | + |
| 46 | +See [examples](https://github.com/glennsl/rescript-json-combinators/blob/master/examples/) for more. |
| 47 | + |
| 48 | + |
| 49 | +## Installation |
| 50 | + |
| 51 | +```sh |
| 52 | +npm install --save @glennsl/rescript-json-combinators |
| 53 | +``` |
| 54 | + |
| 55 | +Then add `@glennsl/rescript-json-combinators` to `bs-dependencies` in your `bsconfig.json`: |
| 56 | + |
| 57 | +```diff |
| 58 | + { |
| 59 | + "bs-dependencies": [ |
| 60 | ++ "@glennsl/rescript-json-combinators" |
| 61 | + ] |
| 62 | + } |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +Optionally, add this to automatically open the library in order to use `Json.Encode` and `Json.Decode` directly: |
| 67 | + |
| 68 | +```diff |
| 69 | + { |
| 70 | + "bsc-flags": [ |
| 71 | ++ "-open JsonCombinators", |
| 72 | + ] |
| 73 | + } |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +## Differences from bs-json |
| 78 | + |
| 79 | +`rescript-json-combinators` is the spriritual successor of `bs-json`. It was rewritten from scratch and not intended to be |
| 80 | +backwards compatible. And while for the most part is does share the same design principles,there are also some notable differences: |
| 81 | + |
| 82 | +* Decoders have been made abstract. While the internal representation is still the same, a function that takes a `Js.Json.t` |
| 83 | +and returns either the decoded value or raises an exception, they can no longer be called directly. Instead they have to be run |
| 84 | +via `Decode.decode`. This is in order to ensure that the `DecodeError` exceptions don't leak and returns a `result` |
| 85 | +instead. Custom decoders can still be created with `Decode.custom`. |
| 86 | + |
| 87 | +* A new `object` decoder has been added, and while it's still possible to decode fields individually in a custom decoder, this |
| 88 | +addresses a couple ergonomic problems with that approach, such as distinguishing between a field missing and having the wrong |
| 89 | +type, and the over-use and under-encapsulation of custom decoders. |
| 90 | + |
| 91 | +* Uncurrying and raw javascript is used in some places to improve performance. For the most part this isn't visible to the |
| 92 | +end user because decoders can no longer be called directly. There are a couple exceptions however, such as field decoders inside |
| 93 | +the `object` deocder, and `map`. |
| 94 | + |
| 95 | + |
| 96 | +## Documentation |
| 97 | + |
| 98 | +### API |
| 99 | + |
| 100 | +For the moment, please see the interface files: |
| 101 | + |
| 102 | +* [Json](https://github.com/glennsl/rescript-json-combinators/blob/master/src/Json.resi) |
| 103 | +* [Json.Encode](https://github.com/glennsl/rescript-json-combinators/blob/master/src/Json_Encode.resi) |
| 104 | +* [Json.Decode](https://github.com/glennsl/rescript-json-combinators/blob/master/src/Json_Decode.resi) |
| 105 | + |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +This work is dual-licensed under [LGPL 3.0](https://choosealicense.com/licenses/lgpl-3.0/) and |
| 110 | +[MPL 2.0](https://choosealicense.com/licenses/mpl-2.0/). You can choose between one of them if you use this work. |
| 111 | + |
| 112 | +Please see [LICENSE.LGPL-3.0](https://github.com/glennsl/rescript-json-combinators/blob/master/LICENSE.LGPL-3.0) and |
| 113 | +[LICENSE.MPL-2.0](https://github.com/glennsl/rescript-json-combinators/blob/master/LICENSE.MPL-2.0) for the full text of each license. |
| 114 | + |
| 115 | +`SPDX-License-Identifier: LGPL-3.0 OR MPL-2.0` |
| 116 | + |
| 117 | + |
| 118 | +## Changes |
| 119 | + |
| 120 | +### 1.0.0 |
| 121 | +Initial release |
0 commit comments