not really known
Go to file
2022-08-30 11:40:22 +01:00
.github/workflows Remove coverage script 2022-08-30 11:40:22 +01:00
.vscode Improve generics and add tests (#3) 2022-06-11 21:12:24 +02:00
LICENCE Initial commit 2022-05-10 10:28:15 +02:00
src/Yoga Fix allow int for number 2022-08-30 11:18:34 +01:00
test Fix allow int for number 2022-08-30 11:18:34 +01:00
.gitignore Add option for enums and more instances (#9) 2022-07-21 14:45:22 +02:00
.nycrc.json Add option for enums and more instances (#9) 2022-07-21 14:45:22 +02:00
.tidyrc.json Improve generics and add tests (#3) 2022-06-11 21:12:24 +02:00
bower.json Bower shit 2022-08-22 10:37:46 +02:00
CHANGELOG.md Fix allow int for number 2022-08-30 11:18:34 +01:00
LICENSE Initial commit 2022-05-10 10:28:15 +02:00
package-lock.json Add option for enums and more instances (#9) 2022-07-21 14:45:22 +02:00
package.json Add option for enums and more instances (#9) 2022-07-21 14:45:22 +02:00
packages.dhall Improve generics and add tests (#3) 2022-06-11 21:12:24 +02:00
README.md Update README.md 2022-08-22 11:04:04 +02:00
spago.dhall Add DateTimeCodec 2022-07-21 15:29:57 +02:00
test.dhall Add DateTimeCodec 2022-07-21 15:29:57 +02:00

purescript-yoga-json

Note: This is a fork of simple-json (MIT Licence).

Table of Contents

Usage

import Yoga.JSON as JSON

serialised :: String
serialised =
  JSON.writeJSON { first_name: "Lola", last_name: "Flores" }

Check out the tests for how to encode/decode increasingly complex types.

Migrate from purescript-simple-json

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.

Additions over simple-json

Errors

yoga-json will actually report multiple errors at once in the case of records, objects, and arrays. There's also a way to make errors more readable with renderHumanError: (e.g. Must provide a value of type 'Int' instead of 'Object' at $.a.b.c.d[1])

Tuples

There is an inbuilt codec for Tuples thanks to @ursi yoga-json represents tuples as arrays in JSON.

Eithers

There is an inbuilt codec for Eithers. yoga-json represents eithers as objects with a type and a value tag in JSON.

JSDate and DateTime

Both are encoded as ISO8601 strings as spat out by JSDate.toISOString.

Generics

It includes @justinwoo's codecs for en- and decoding generics inspired by simple-json-generics

It is possible to customise the representation of enums, tagged sum types, and untagged sum types via options.

BigInts

It can read bigints (if you install big-integer as a JS dependency).

💣 Cannot write bigints as bigints but only strings

It seems that there is no way to write bigints in JavaScript except for writing your own JSON.stringify.

Differences to simple-json

💣 Variant codec

If you want to emulate simple-json's format you may use the newtype TaggedVariant

type YourVariantRow = ( a :: Int, b :: String )
type YourVariant = Variant YourVariantRow
x :: YourVariant
x = inj (Proxy :: Proxy "a") 5
-- encoded = writeJSON x
-- ^ Let's say you had this before
-- You can now do:
encoded = writeJSON (TaggedVariant "type" "value" x)