Skip to content

Commit 83a9bc8

Browse files
authored
Temporarily vendor bs-json (#324)
Master has some module resolution trouble (deps...)
1 parent db785db commit 83a9bc8

16 files changed

+1584
-17
lines changed

bsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
},
77
"bs-dependencies": [
88
"@rescript/react",
9-
"@glennsl/bs-json",
109
"@ryyppy/rescript-promise"
1110
],
1211
"ppx-flags": [],

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"node": ">=12.x"
99
},
1010
"dependencies": {
11-
"@glennsl/bs-json": "^3.0.0",
1211
"@mdx-js/loader": "^1.5.5",
1312
"@next/mdx": "^8.1.0",
1413
"@rescript/react": "^0.10.1",

src/bindings/RescriptCompilerApi.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as Js_dict from "rescript/lib/es6/js_dict.js";
55
import * as Belt_Int from "rescript/lib/es6/belt_Int.js";
66
import * as Belt_List from "rescript/lib/es6/belt_List.js";
77
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
8-
import * as Json_decode from "@glennsl/bs-json/src/Json_decode.js";
8+
import * as Json_decode from "../vendor/Json_decode.mjs";
99
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
1010

1111
function toString(t) {

src/common/BlogFrontmatter.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as Js_null from "rescript/lib/es6/js_null.js";
44
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
55
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
6-
import * as Json_decode from "@glennsl/bs-json/src/Json_decode.js";
6+
import * as Json_decode from "../vendor/Json_decode.mjs";
77
import * as Caml_exceptions from "rescript/lib/es6/caml_exceptions.js";
88
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
99
import Blog_authorsMjs from "../../data/blog_authors.mjs";

src/common/DocFrontmatter.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Js_null from "rescript/lib/es6/js_null.js";
4-
import * as Json_decode from "@glennsl/bs-json/src/Json_decode.js";
4+
import * as Json_decode from "../vendor/Json_decode.mjs";
55
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
66

77
function decode(json) {

src/components/Navigation.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as Constants from "../common/Constants.mjs";
1111
import * as DocSearch from "./DocSearch.mjs";
1212
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
1313
import * as Caml_option from "rescript/lib/es6/caml_option.js";
14-
import * as ReactDOMStyle from "@rescript/react/src/ReactDOMStyle.js";
14+
import * as ReactDOMStyle from "@rescript/react/src/ReactDOMStyle.mjs";
1515
import * as VersionSelect from "./VersionSelect.mjs";
1616

1717
var link = "no-underline block text-inherit hover:cursor-pointer hover:text-fire-30 text-gray-40 mb-px";

src/vendor/Json.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Js_exn from "rescript/lib/es6/js_exn.js";
4+
import * as Caml_option from "rescript/lib/es6/caml_option.js";
5+
import * as Caml_exceptions from "rescript/lib/es6/caml_exceptions.js";
6+
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
7+
8+
var ParseError = /* @__PURE__ */Caml_exceptions.create("Json.ParseError");
9+
10+
function parse(s) {
11+
try {
12+
return Caml_option.some(JSON.parse(s));
13+
}
14+
catch (exn){
15+
return ;
16+
}
17+
}
18+
19+
function parseOrRaise(s) {
20+
try {
21+
return JSON.parse(s);
22+
}
23+
catch (raw_e){
24+
var e = Caml_js_exceptions.internalToOCamlException(raw_e);
25+
if (e.RE_EXN_ID === Js_exn.$$Error) {
26+
var m = e._1.message;
27+
var message = m !== undefined ? m : "Unknown error";
28+
throw {
29+
RE_EXN_ID: ParseError,
30+
_1: message,
31+
Error: new Error()
32+
};
33+
}
34+
throw e;
35+
}
36+
}
37+
38+
var Decode;
39+
40+
var Encode;
41+
42+
function stringify(prim) {
43+
return JSON.stringify(prim);
44+
}
45+
46+
export {
47+
Decode ,
48+
Encode ,
49+
ParseError ,
50+
parse ,
51+
parseOrRaise ,
52+
stringify ,
53+
54+
}
55+
/* No side effect */

src/vendor/Json.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Decode = Json_decode
2+
module Encode = Json_encode
3+
4+
exception ParseError of string
5+
6+
let parse s =
7+
try Some (Js.Json.parseExn s) with
8+
| _ -> None
9+
10+
let parseOrRaise s =
11+
try Js.Json.parseExn s with
12+
| Js.Exn.Error e ->
13+
let message =
14+
match Js.Exn.message e with
15+
| Some m -> m
16+
| None -> "Unknown error"
17+
in raise @@ ParseError message
18+
19+
external stringify : Js.Json.t -> string = "JSON.stringify" [@@bs.val]

src/vendor/Json.mli

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
(** Efficient JSON handling
2+
This module has four aspects to it:
3+
- Parsing, which turns a JSON string into an encoded JSON data structure
4+
- Stringificaiton, which produces a JSON string from an encoded JSON data structure
5+
- Encoding, which is the process of construction a JSON data structure
6+
- Decoding, which is the process of deconstructing a JSON data structure
7+
{3 Parsing}
8+
{! parse} and {! parseOrRaise} will both (try to) parse a JSON string into a JSON
9+
data structure ({! Js.Json.t}), but behaves differently when encountering a
10+
parse error. [parseOrRaise] will raise a [ParseError], while [parse] will return
11+
a [Js.Json.t result] indicating whether or not the parsing succeeded. There's
12+
not much more to it: [string] in, [Js.Json.t] out.
13+
The parsed result, and encoded JSON data structure, then needs to be decoded to
14+
actually be usable. See {!section:Decoding} below.
15+
{3 Stringification}
16+
Stringification is the exact reverse of parsing. {! stringify} and {! stringifyAny}
17+
both technically do the same thing, but where [stringifyAny] will take any value
18+
and try to do its best with it, retuning a [string option], [stringify] on the
19+
other hand uses the type system to guarantee success, but requires that the data
20+
has been encoded in a JSON data structure first. See {!section:Encoding} below.
21+
{3 Encoding}
22+
Encoding creates a JSON data structure which can stringified directly with
23+
{! stringify} or passed to other APIs requiring a typed JSON data structure. Or
24+
you could just go straight to decoding it again, if that's your thing. Encoding
25+
functions are in the {! Encode} module.
26+
{3 Decoding}
27+
Decoding is a more complex process, due to the highly dynamic nature of JSON
28+
data structures. The {! Decode} module provides decoder combinators that can
29+
be combined to create complex composite decoders for any _known_ JSON data
30+
structure. It allows for custom decoders to produce user-defined types.
31+
32+
@example {[
33+
(* Parsing a JSON string using Json.parse *)
34+
let arrayOfInts str
35+
match Json.parse str with
36+
| Some value ->
37+
match Json.Decode.(array int value)
38+
| Ok arr -> arr
39+
| Error _ -> []
40+
| None -> failWith "Unable to parse JSON"
41+
42+
(* prints `[3, 2, 1]` *)
43+
let _ = Js.log (arrayOfInts "[1, 2, 3]" |> Js.Array.reverse)
44+
]}
45+
46+
@example {[
47+
(* Stringifying a value using Json.stringify *)
48+
49+
(* prints `null` *)
50+
let _ =
51+
Json.stringify (Encode.int 42)
52+
|> Js.log
53+
]}
54+
55+
@example {[
56+
(* Encoding a JSON data structure using Json.Encode *)
57+
58+
(* prints ["foo", "bar"] *)
59+
let _ =
60+
[| "foo", "bar" |]
61+
|> Json.Encode.stringArray
62+
|> Json.stringify
63+
|> Js.log
64+
65+
(* prints ["foo", "bar"] *)
66+
let _ =
67+
[| "foo", "bar" |]
68+
|> Js.Array.map Encode.int
69+
|> Json.Encode.jsonArray
70+
|> Json.stringify
71+
|> Js.log
72+
]}
73+
74+
@example {[
75+
(* Decoding a fixed JSON data structure using Json.Decode *)
76+
let mapJsonObjectString f decoder encoder str =
77+
match Json.parse str with
78+
| Ok json ->
79+
match Json.Decode.(dict decoder json) with
80+
| Ok dict ->
81+
dict |> Js.Dict.map f
82+
|> Js.Dict.map encoder
83+
|> Json.Encode.dict
84+
|> Json.stringify
85+
| Error _ -> []
86+
| Error _ -> []
87+
88+
let sum ns =
89+
Array.fold_left (+) 0
90+
91+
(* prints `{ "foo": 6, "bar": 24 }` *)
92+
let _ =
93+
Js.log (
94+
mapJsonObjectString sun Json.Decode.(array int) Json.Encode.int {|
95+
{
96+
"foo": [1, 2, 3],
97+
"bar": [9, 8, 7]
98+
}
99+
|}
100+
)
101+
]}
102+
*)
103+
104+
module Decode = Json_decode
105+
module Encode = Json_encode
106+
107+
exception ParseError of string
108+
109+
val parse: string -> Js.Json.t option
110+
(** [parse s] returns [Some json] if s is a valid json string, [None] otherwise *)
111+
112+
val parseOrRaise: string -> Js.Json.t
113+
(** [parse s] returns a [Js.Json.t] if s is a valid json string, raises [ParseError] otherwise *)
114+
115+
val stringify: Js.Json.t -> string
116+
(** [stringify json] returns the [string] representation of the given [Js.Json.t] value *)

0 commit comments

Comments
 (0)