Skip to content

Simplify JSON.Decode implementation #7304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
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
}