Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

- Remove ignore in res_scanner.ml . https://github.com/rescript-lang/rescript/pull/7280
- Use the new stdlib modules in the analysis tests. https://github.com/rescript-lang/rescript/pull/7295
- Build with OCaml 5.3.0. https://github.com/rescript-lang/rescript-compiler/pull/7294
- Build with OCaml 5.3.0. https://github.com/rescript-lang/rescript/pull/7294
- Simplify JSON.Decode implementation. https://github.com/rescript-lang/rescript/pull/7304

# 12.0.0-alpha.8

Expand Down
2 changes: 1 addition & 1 deletion lib/es6/Stdlib_JSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function float(json) {
}

function object(json) {
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
if (typeof json === "object" && json !== null && !Array.isArray(json)) {
return json;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/js/Stdlib_JSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function float(json) {
}

function object(json) {
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
if (typeof json === "object" && json !== null && !Array.isArray(json)) {
return json;
}

Expand Down
51 changes: 34 additions & 17 deletions runtime/Stdlib_JSON.res
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,39 @@ module Encode = {
}

module Decode = {
let bool = (json: t) =>
Stdlib_Type.typeof(json) === #boolean ? Some((Obj.magic(json): bool)) : None
let null = (json: t) => Obj.magic(json) === Stdlib_Null.null ? Some(Stdlib_Null.null) : None
let string = (json: t) =>
Stdlib_Type.typeof(json) === #string ? Some((Obj.magic(json): string)) : None
let float = (json: t) =>
Stdlib_Type.typeof(json) === #number ? Some((Obj.magic(json): float)) : None
let object = (json: t) =>
if (
Stdlib_Type.typeof(json) === #object &&
!Stdlib_Array.isArray(json) &&
!(Obj.magic(json) === Stdlib_Null.null)
) {
Some((Obj.magic(json): dict<t>))
} else {
None
let bool = json =>
switch json {
| Boolean(b) => Some(b)
| _ => None
}

let null = json =>
switch json {
| Null => Some(Stdlib_Null.null)
| _ => None
}

let string = json =>
switch json {
| String(s) => Some(s)
| _ => None
}

let float = json =>
switch json {
| Number(f) => Some(f)
| _ => None
}

let object = json =>
switch json {
| Object(o) => Some(o)
| _ => None
}

let array = (json: t) =>
switch json {
| Array(a) => Some(a)
| _ => None
}
let array = (json: t) => Stdlib_Array.isArray(json) ? Some((Obj.magic(json): array<t>)) : None
}