Skip to content

Commit a56fff5

Browse files
committed
json
1 parent 533f98e commit a56fff5

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

docs/json.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
id: json
3+
title: JSON
4+
---
5+
6+
There are several ways to work with JSON right now, that we'll unify and polish very soon.
7+
8+
## Unsafe Conversion
9+
10+
This emulates JavaScript's JSON conversion.
11+
12+
### Parse
13+
14+
Simply use the (last resort) special [identity external](intro-to-external.md#special-identity-external):
15+
16+
```ocaml
17+
type data = < name :string > Js.t
18+
external parseIntoMyData : string -> data = "parse" [@@bs.scope "JSON"][@@bs.val]
19+
20+
let result = parseIntoMyData "{\"name\": \"Luke\"}"
21+
```
22+
23+
Reason syntax:
24+
25+
```reason
26+
type data = {. "name": string};
27+
[@bs.scope "JSON"][@bs.val] external parseIntoMyData: string => data = "parse";
28+
29+
let result = parseIntoMyData("{\"name\": \"Luke\"}");
30+
```
31+
32+
Output:
33+
34+
```js
35+
var result = JSON.parse("{\"name\": \"Luke\"}");
36+
```
37+
38+
Where `data` can be any type you assume the JSON is. As you can see, this compiles to a straightforward `JSON.parse` call. As with regular JS, this is convenient, but has no guarantee that e.g. the data is correctly shaped, or even syntactically valid.
39+
40+
### Stringify
41+
42+
Since JSON.stringify is _slightly_ safer than `JSON.parse`, we've provided it out of the box in [Js.Json](https://bucklescript.github.io/bucklescript/api/Js.Json.html#VALstringifyAny). It compiles to `JSON.stringify`.
43+
44+
## Properly Use `Js.Json`
45+
46+
Technically, the correct way to handle JSON is to recursively parse each field, and handle invalid data accordingly. [Js.Json](https://bucklescript.github.io/bucklescript/api/Js.Json.html) provides such low-level building blocks. See the examples in the API docs.
47+
48+
## Higher-level Helpers
49+
50+
We have a community-maintaned, pseudo-official JSON helper library called [bs-json](https://github.com/reasonml-community/bs-json). Those interested in combinators can also check out [JsonCodec](https://github.com/state-machine-systems/JsonCodec). Both provide more convenient JSON parsing/printing helpers that leverage the previous section's low-level API.
51+
52+
We'll provide a better, first-party solution to JSON too in the future. Stay tuned!

website/i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"interop-overview": "Overview",
3434
"interop-with-js-build-systems": "Interop with JS Build System",
3535
"intro-to-external": "Intro to External",
36+
"json": "JSON",
3637
"load-third-party-libraries": "Load Third-party Libraries",
3738
"new-project": "New Project",
3839
"nodejs-special-variables": "NodeJS Special Variables",
@@ -66,4 +67,4 @@
6667
"Edit this Doc|recruitment message asking to edit the doc source": "Edit",
6768
"Translate this Doc|recruitment message asking to translate the docs": "Translate"
6869
}
69-
}
70+
}

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"import-export",
2222
"regular-expression",
2323
"exceptions",
24+
"json",
2425
"generate-converters-accessors",
2526
"nodejs-special-variables",
2627
"interop-misc",

0 commit comments

Comments
 (0)