Skip to content

Commit 269b005

Browse files
committed
Simplify JSON.Decode implementation
1 parent 0b61e51 commit 269b005

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

lib/es6/Stdlib_JSON.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function float(json) {
6969
}
7070

7171
function object(json) {
72-
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
72+
if (typeof json === "object" && json !== null && !Array.isArray(json)) {
7373
return json;
7474
}
7575

lib/js/Stdlib_JSON.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function float(json) {
6969
}
7070

7171
function object(json) {
72-
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
72+
if (typeof json === "object" && json !== null && !Array.isArray(json)) {
7373
return json;
7474
}
7575

runtime/Stdlib_JSON.res

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,39 @@ module Encode = {
8080
}
8181

8282
module Decode = {
83-
let bool = (json: t) =>
84-
Stdlib_Type.typeof(json) === #boolean ? Some((Obj.magic(json): bool)) : None
85-
let null = (json: t) => Obj.magic(json) === Stdlib_Null.null ? Some(Stdlib_Null.null) : None
86-
let string = (json: t) =>
87-
Stdlib_Type.typeof(json) === #string ? Some((Obj.magic(json): string)) : None
88-
let float = (json: t) =>
89-
Stdlib_Type.typeof(json) === #number ? Some((Obj.magic(json): float)) : None
90-
let object = (json: t) =>
91-
if (
92-
Stdlib_Type.typeof(json) === #object &&
93-
!Stdlib_Array.isArray(json) &&
94-
!(Obj.magic(json) === Stdlib_Null.null)
95-
) {
96-
Some((Obj.magic(json): dict<t>))
97-
} else {
98-
None
83+
let bool = json =>
84+
switch json {
85+
| Boolean(b) => Some(b)
86+
| _ => None
87+
}
88+
89+
let null = json =>
90+
switch json {
91+
| Null => Some(Stdlib_Null.null)
92+
| _ => None
93+
}
94+
95+
let string = json =>
96+
switch json {
97+
| String(s) => Some(s)
98+
| _ => None
99+
}
100+
101+
let float = json =>
102+
switch json {
103+
| Number(f) => Some(f)
104+
| _ => None
105+
}
106+
107+
let object = json =>
108+
switch json {
109+
| Object(o) => Some(o)
110+
| _ => None
111+
}
112+
113+
let array = (json: t) =>
114+
switch json {
115+
| Array(a) => Some(a)
116+
| _ => None
99117
}
100-
let array = (json: t) => Stdlib_Array.isArray(json) ? Some((Obj.magic(json): array<t>)) : None
101118
}

0 commit comments

Comments
 (0)