Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Core__JSON.res
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
type t = Js.Json.t

type jsonReviver
external asJsonReviver: 'a => jsonReviver = "%identity"
type jsonReplacer
external asJsonReplacer: 'a => jsonReplacer = "%identity"

@val external parseExn: string => t = "JSON.parse"
@val external parseExnWithReviver: (string, jsonReviver) => t = "JSON.parse"
@val external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
@val external stringify: t => string = "JSON.stringify"
@val external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"
@val external stringifyWithReplacer: (t, jsonReplacer) => string = "JSON.stringify"
@val external stringifyWithReplacerAndIndent: (t, jsonReplacer, int) => string = "JSON.stringify"

@val external parseToAnyExn: string => 'a = "JSON.parse"
@val external parseToAnyExnWithReviver: (string, jsonReviver) => 'a = "JSON.parse"
@val external parseToAnyExnWithReviver: (string, (string, t) => t) => 'a = "JSON.parse"
@val external stringifyAny: 'a => option<string> = "JSON.stringify"
@val
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
Expand Down Expand Up @@ -59,6 +57,7 @@ module Encode = {
external float: float => t = "%identity"
external object: Core__Dict.t<t> => t = "%identity"
external array: array<t> => t = "%identity"
external any: 'a => t = "%identity"
}

module Decode = {
Expand Down
29 changes: 16 additions & 13 deletions src/Core__JSON.resi
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ A type representing a JSON object.
*/
type t = Js.Json.t

/**
A type which describes how the value should be transformed while parsing.
It is a function which receives a key and a value.
*/
type jsonReviver
external asJsonReviver: 'a => jsonReviver = "%identity"

/**
A type which describes how the value should be transformed while stringification.
It is a function which receives a key and a value.
Expand Down Expand Up @@ -45,21 +38,21 @@ try {
external parseExn: string => t = "JSON.parse"

/**
`parseExnWithReviver(string, jsonReviver)`
`parseExnWithReviver(string, reviver)`

Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid.
The reviver describes how the value should be transformed. It is a function which receives a key and a value.
It returns a JSON type. If you want to get any type, use `JSON.parseToAnyExnWithReviver` instead.

## Examples
```rescript
let reviver = JSON.asJsonReviver((key, value) => {
let reviver = (key, value) => {
if key->String.toLowerCase->String.includes("date") {
Date.fromString(value)->Obj.magic
value->JSON.Decode.string->Option.map(Date.fromString)->JSON.Encode.any
} else {
value
}
})
}

let jsonString = `{"parseAsDate":"2023-02-26","parseAsString":"2023-02-26"}`

Expand All @@ -76,7 +69,7 @@ try {
*/
@raises
@val
external parseExnWithReviver: (string, jsonReviver) => t = "JSON.parse"
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"

/**
`stringify(json)`
Expand Down Expand Up @@ -230,7 +223,7 @@ try {
*/
@raises
@val
external parseToAnyExnWithReviver: (string, jsonReviver) => 'a = "JSON.parse"
external parseToAnyExnWithReviver: (string, (string, t) => t) => 'a = "JSON.parse"

/**
`stringifyAny(any)`
Expand Down Expand Up @@ -466,6 +459,16 @@ module Encode: {
```
*/
external array: array<t> => t = "%identity"

/**
Returns any as a JSON object.

## Examples
```rescript
JSON.Encode.any("test")
```
*/
external any: 'a => t = "%identity"
}

module Decode: {
Expand Down