git.iko.soy/rowtype-yoga/purescript-yoga-json
on v3.0.1 help sign in
61 lines 2.0 kB history raw
1# ๐Ÿ๐Ÿ‘‘ purescript-yoga-json
3**Note**: This is a fork of [simple-json](https://github.com/justinwoo/purescript-simple-json) ([MIT Licence](./LICENSE/simple-json.LICENSE)).
5## Table of Contents
6* [usage](#usage)
7* [migrate from `simple-json`](#migrate-from-purescript-simple-json)
9## Usage
11```purescript
12import Yoga.JSON as JSON
14serialised :: String
15serialised =
16 JSON.writeJSON { first_name: "Lola", last_name: "Flores" }
17```
19Check out the tests for how to encode/decode increasingly complex types.
21## Migrate from `purescript-simple-json`
23`purescript-yoga-json` is almost (read below if you use variants) a drop-in replacement for `purescript-simple-json`. Just change the imports from `Simple.JSON` to `Yoga.JSON`.
25## Additions over `simple-json`
27### Tuples
28There is an inbuilt codec for `Tuple`s thanks to @ursi
29`yoga-json` represents tuples as arrays in JSON.
31### Eithers
32There is an inbuilt codec for `Either`s.
33`yoga-json` represents eithers as objects with a `type` and a `value` tag in JSON.
35### Generics
36It includes @justinwoo's codecs for en- and decoding generics inspired by
37[simple-json-generics](https://github.com/justinwoo/purescript-simple-json-generics)
39It is possible to customise the representation of enums, tagged sum types, and untagged sum types via options.
41### BigInts
42It can *read* bigints (if you install `big-integer` as a JS dependency).
44### ๐Ÿ’ฃ Cannot write bigints as bigints but only strings
45It seems that there is no way to write bigints in JavaScript except for writing your own `JSON.stringify`.
47## Differences to `simple-json`
49### ๐Ÿ’ฃ Variant codec
50If you want to emulate `simple-json`'s format you may use the newtype `TaggedVariant`
52```purescript
53type YourVariantRow = ( a :: Int, b :: String )
54type YourVariant = Variant YourVariantRow
55x :: YourVariant
56x = inj (Proxy :: Proxy "a") 5
57-- encoded = writeJSON x
58-- ^ Let's say you had this before
59-- You can now do:
60encoded = writeJSON (TaggedVariant "type" "value" x)
61```