diff --git a/CHANGELOG.md b/CHANGELOG.md index 130c4ee8a0..63357ba698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/es6/Stdlib_JSON.js b/lib/es6/Stdlib_JSON.js index bbfa0a1a65..ecd1fca530 100644 --- a/lib/es6/Stdlib_JSON.js +++ b/lib/es6/Stdlib_JSON.js @@ -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; } diff --git a/lib/js/Stdlib_JSON.js b/lib/js/Stdlib_JSON.js index 799be2bab0..81558b4625 100644 --- a/lib/js/Stdlib_JSON.js +++ b/lib/js/Stdlib_JSON.js @@ -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; } diff --git a/runtime/Stdlib_JSON.res b/runtime/Stdlib_JSON.res index 67c9213d9d..380f15e499 100644 --- a/runtime/Stdlib_JSON.res +++ b/runtime/Stdlib_JSON.res @@ -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)) - } 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)) : None }