Skip to content

rowtype-yoga/purescript-yoga-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f9fb9d7 Β· Jul 21, 2022

History

26 Commits
Jul 21, 2022
Jun 11, 2022
May 10, 2022
Jul 21, 2022
Jul 21, 2022
Jul 21, 2022
Jul 21, 2022
Jun 11, 2022
May 10, 2022
Jul 21, 2022
Jun 11, 2022
Jul 21, 2022
Jul 21, 2022
Jun 11, 2022
Jul 21, 2022
Jun 16, 2022

Repository files navigation

πŸπŸ‘‘ 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

Tuples

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

Tuples

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

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)