Skip to content

Commit

Permalink
Simplify JSON.Decode implementation (#7304)
Browse files Browse the repository at this point in the history
* Simplify JSON.Decode implementation

* CHANGELOG
  • Loading branch information
cknitt authored Feb 18, 2025
1 parent 0b61e51 commit 0fda872
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
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
}

0 comments on commit 0fda872

Please sign in to comment.