Skip to content

Commit

Permalink
Add FFI for json (#11)
Browse files Browse the repository at this point in the history
Adds FFI definitions for core/json along with a JSON parser and printer. Printer doesn't support pretty printing yet.
  • Loading branch information
anttih authored May 21, 2024
1 parent 88a40ff commit 48119e8
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 2 deletions.
18 changes: 18 additions & 0 deletions json/spago.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package:
name: json
dependencies:
- prelude
- arrays
- functions
- integers
- maybe
- either
- tuples
- foldable-traversable
- gen
- strings
- unfoldable
test:
main: Test.JSON.Main
dependencies:
- assert
2 changes: 1 addition & 1 deletion json/src/JSON.purs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Data.Function.Uncurried (runFn2, runFn3, runFn7)
import Data.Int as Int
import Data.Maybe (Maybe(..))
import JSON.Internal (JArray, JObject, JSON)
import JSON.Internal (JArray, JObject, JSON) as Exports
import JSON.Internal (JArray, JObject, JSON, isNull) as Exports
import JSON.Internal as Internal

-- | Attempts to parse a string as a JSON value. If parsing fails, an error message detailing the
Expand Down
31 changes: 31 additions & 0 deletions json/src/JSON.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(library (JSON foreign)
(export _null
fromBoolean
fromInt
fromString
fromJArray
fromJObject
print
printIndented)
(import (chezscheme)
(only (JSON.Internal foreign) json-stringify))

(define (coerce x) x)

(define _null 'null)

(define fromBoolean coerce)

(define fromInt inexact)

(define fromString coerce)

(define fromJArray coerce)

(define fromJObject coerce)

(define print json-stringify)

(define printIndented json-stringify)

)
9 changes: 9 additions & 0 deletions json/src/JSON/Array.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(library (JSON.Array foreign)
(export singleton)
(import (chezscheme)
(prefix (purs runtime srfi :214) srfi:214:))

(define (singleton x) (srfi:214:flexvector x))

)

2 changes: 2 additions & 0 deletions json/src/JSON/Internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ export const _index = (nothing, just, ix, arr) =>
ix >= 0 && ix < arr.length ? just(arr[ix]) : nothing;

export const _append = (xs, ys) => xs.concat(ys);

export const isNull = (json) => json == null;
2 changes: 2 additions & 0 deletions json/src/JSON/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ foreign import _append
JArray
JArray
JArray

foreign import isNull :: JSON -> Boolean
Loading

0 comments on commit 48119e8

Please sign in to comment.