Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nkrkv/jzon
ReScript library to encode and decode JSON data with type safety.
https://github.com/nkrkv/jzon
json rescript
Last synced: about 1 month ago
JSON representation
ReScript library to encode and decode JSON data with type safety.
- Host: GitHub
- URL: https://github.com/nkrkv/jzon
- Owner: nkrkv
- License: other
- Created: 2021-02-08T16:18:51.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T18:34:04.000Z (8 months ago)
- Last Synced: 2024-12-14T17:05:20.093Z (about 1 month ago)
- Topics: json, rescript
- Language: ReScript
- Homepage: https://nkrkv.github.io/jzon/
- Size: 802 KB
- Stars: 76
- Watchers: 4
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Jzon
Jzon is a library for ReScript to encode and decode JSON data with type safety.
- 🎷 [Documentation](https://nkrkv.github.io/jzon/) 🎷
- [Changelog](./CHANGELOG.md): MIT
- [License](./LICENSE.md): MIT## Installation
1. `yarn add rescript-jzon`
2. Add `"rescript-jzon"` item to the `dependencies` key of `bsconfig.json`## Quick start
Imagine you have the following ReScript types to encode and decode:
```rescript
type style = {
size: float,
color: string,
}type point = {
x: float,
y: float,
z: float,
style: option,
}
```First, define their _codecs_:
```rescript
module Codecs = {
let style = Jzon.object2(
// Function to encode original object to linear tuple
({size, color}) => (size, color),// Function to decode linear tuple back to object
((size, color)) => {size, color}->Ok,// Field names and codecs for the tuple elements
Jzon.field("size", Jzon.float),
Jzon.field("color", Jzon.string),
);// Similar codec for another record type
let point = Jzon.object4(
({x, y, z, style}) => (x, y, z, style),
((x, y, z, style)) => {x, y, z, style}->Ok,
Jzon.field("x", Jzon.float),
Jzon.field("y", Jzon.float),
// ... supports default values
Jzon.field("z", Jzon.float)->Jzon.default(0.0),
// ... may refer your other codecs
Jzon.field("style", style)->Jzon.optional,
)
}
```Next, convert between the ReScript types and `Js.Json.t` with:
```rescript
let myPoint = {
x: 1.0,
y: 2.0,
z: 3.0,
style: Some({size: 4.0, color: "#fd0"}),
}let json = myPoint->Jzon.encodeWith(Codecs.point)
```and back with:
```rescript
let myPoint = json->Jzon.decodeWith(Codecs.point)
```