Skip to content

Commit b8d01ad

Browse files
cknittzth
authored andcommitted
Add JSON decode functions from Js.Json and move classify into separate submodule
1 parent 88d055e commit b8d01ad

File tree

5 files changed

+136
-7
lines changed

5 files changed

+136
-7
lines changed

src/Core__JSON.mjs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Caml_option from "rescript/lib/es6/caml_option.js";
34

45
function classify(value) {
56
var match = Object.prototype.toString.call(value);
@@ -34,14 +35,66 @@ function classify(value) {
3435
}
3536
}
3637

37-
var Decode = {
38+
var Classify = {
3839
classify: classify
3940
};
4041

4142
var Encode = {};
4243

44+
function bool(json) {
45+
if (typeof json === "boolean") {
46+
return json;
47+
}
48+
49+
}
50+
51+
function $$null(json) {
52+
if (json === null) {
53+
return null;
54+
}
55+
56+
}
57+
58+
function string(json) {
59+
if (typeof json === "string") {
60+
return json;
61+
}
62+
63+
}
64+
65+
function $$float(json) {
66+
if (typeof json === "number") {
67+
return json;
68+
}
69+
70+
}
71+
72+
function object(json) {
73+
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
74+
return Caml_option.some(json);
75+
}
76+
77+
}
78+
79+
function array(json) {
80+
if (Array.isArray(json)) {
81+
return json;
82+
}
83+
84+
}
85+
86+
var Decode = {
87+
bool: bool,
88+
$$null: $$null,
89+
string: string,
90+
$$float: $$float,
91+
object: object,
92+
array: array
93+
};
94+
4395
export {
44-
Decode ,
96+
Classify ,
4597
Encode ,
98+
Decode ,
4699
}
47100
/* No side effect */

src/Core__JSON.res

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ external stringifyAnyWithReplacer: ('a, jsonReplacer) => option<string> = "JSON.
2323
external stringifyAnyWithReplacerAndIndent: ('a, jsonReplacer, int) => option<string> =
2424
"JSON.stringify"
2525

26-
module Decode = {
26+
module Classify = {
2727
type t =
2828
| Bool(bool)
2929
| Null
@@ -60,3 +60,24 @@ module Encode = {
6060
external object: Core__Dict.t<t> => t = "%identity"
6161
external array: array<t> => t = "%identity"
6262
}
63+
64+
module Decode = {
65+
let bool = (json: t) =>
66+
Core__Type.typeof(json) === #boolean ? Some((Obj.magic(json): bool)) : None
67+
let null = (json: t) => Obj.magic(json) === Core__Null.null ? Some(Core__Null.null) : None
68+
let string = (json: t) =>
69+
Core__Type.typeof(json) === #string ? Some((Obj.magic(json): string)) : None
70+
let float = (json: t) =>
71+
Core__Type.typeof(json) === #number ? Some((Obj.magic(json): float)) : None
72+
let object = (json: t) =>
73+
if (
74+
Core__Type.typeof(json) === #object &&
75+
!Core__Array.isArray(json) &&
76+
!(Obj.magic(json) === Core__Null.null)
77+
) {
78+
Some((Obj.magic(json): Core__Dict.t<t>))
79+
} else {
80+
None
81+
}
82+
let array = (json: t) => Core__Array.isArray(json) ? Some((Obj.magic(json): array<t>)) : None
83+
}

src/Core__JSON.resi

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
type t = Js.Json.t
2+
3+
type jsonReviver
4+
external asJsonReviver: 'a => jsonReviver = "%identity"
5+
type jsonReplacer
6+
external asJsonReplacer: 'a => jsonReplacer = "%identity"
7+
8+
@val external parseExn: string => t = "JSON.parse"
9+
@val external parseExnWithReviver: (string, jsonReviver) => t = "JSON.parse"
10+
@val external stringify: t => string = "JSON.stringify"
11+
@val external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"
12+
@val external stringifyWithReplacer: (t, jsonReplacer) => string = "JSON.stringify"
13+
@val external stringifyWithReplacerAndIndent: (t, jsonReplacer, int) => string = "JSON.stringify"
14+
15+
@val external parseToAnyExn: string => 'a = "JSON.parse"
16+
@val external parseToAnyExnWithReviver: (string, jsonReviver) => 'a = "JSON.parse"
17+
@val external stringifyAny: 'a => option<string> = "JSON.stringify"
18+
@val
19+
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
20+
@val
21+
external stringifyAnyWithReplacer: ('a, jsonReplacer) => option<string> = "JSON.stringify"
22+
@val
23+
external stringifyAnyWithReplacerAndIndent: ('a, jsonReplacer, int) => option<string> =
24+
"JSON.stringify"
25+
26+
module Classify: {
27+
type t =
28+
| Bool(bool)
29+
| Null
30+
| String(string)
31+
| Number(float)
32+
| Object(Core__Dict.t<t>)
33+
| Array(array<t>)
34+
35+
let classify: 'a => t
36+
}
37+
38+
module Encode: {
39+
external bool: bool => t = "%identity"
40+
external null: t = "#null"
41+
external string: string => t = "%identity"
42+
external int: int => t = "%identity"
43+
external float: float => t = "%identity"
44+
external object: Core__Dict.t<t> => t = "%identity"
45+
external array: array<t> => t = "%identity"
46+
}
47+
48+
module Decode: {
49+
let bool: t => option<bool>
50+
let null: t => option<Core__Null.t<'a>>
51+
let string: t => option<string>
52+
let float: t => option<float>
53+
let object: t => option<Core__Dict.t<t>>
54+
let array: t => option<array<t>>
55+
}

test/TempTests.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ console.info("---");
118118

119119
var json = JSON.parse("{\"foo\": \"bar\"}");
120120

121-
var json$1 = Core__JSON.Decode.classify(json);
121+
var json$1 = Core__JSON.Classify.classify(json);
122122

123123
var tmp;
124124

125125
if (typeof json$1 === "number" || json$1.TAG !== /* Object */3) {
126126
tmp = undefined;
127127
} else {
128-
var value = Core__JSON.Decode.classify(json$1._0["foo"]);
128+
var value = Core__JSON.Classify.classify(json$1._0["foo"]);
129129
tmp = typeof value === "number" || value.TAG !== /* String */1 ? undefined : value._0;
130130
}
131131

test/TempTests.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ Console.info("JSON")
5757
Console.info("---")
5858
let json = JSON.parseExn(`{"foo": "bar"}`)
5959
Console.log(
60-
switch JSON.Decode.classify(json) {
60+
switch JSON.Classify.classify(json) {
6161
| Object(json) =>
62-
switch JSON.Decode.classify(json->Dict.get("foo")) {
62+
switch JSON.Classify.classify(json->Dict.get("foo")) {
6363
| String(value) => Some(value)
6464
| _ => None
6565
}

0 commit comments

Comments
 (0)