diff --git a/src/Core__JSON.mjs b/src/Core__JSON.mjs index e6a8e3a7..e26011ca 100644 --- a/src/Core__JSON.mjs +++ b/src/Core__JSON.mjs @@ -1,5 +1,6 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +import * as Caml_option from "rescript/lib/es6/caml_option.js"; function classify(value) { var match = Object.prototype.toString.call(value); @@ -34,14 +35,66 @@ function classify(value) { } } -var Decode = { +var Classify = { classify: classify }; var Encode = {}; +function bool(json) { + if (typeof json === "boolean") { + return json; + } + +} + +function $$null(json) { + if (json === null) { + return null; + } + +} + +function string(json) { + if (typeof json === "string") { + return json; + } + +} + +function $$float(json) { + if (typeof json === "number") { + return json; + } + +} + +function object(json) { + if (typeof json === "object" && !Array.isArray(json) && json !== null) { + return Caml_option.some(json); + } + +} + +function array(json) { + if (Array.isArray(json)) { + return json; + } + +} + +var Decode = { + bool: bool, + $$null: $$null, + string: string, + $$float: $$float, + object: object, + array: array +}; + export { - Decode , + Classify , Encode , + Decode , } /* No side effect */ diff --git a/src/Core__JSON.res b/src/Core__JSON.res index 7df41e44..100ae5ee 100644 --- a/src/Core__JSON.res +++ b/src/Core__JSON.res @@ -23,7 +23,7 @@ external stringifyAnyWithReplacer: ('a, jsonReplacer) => option = "JSON. external stringifyAnyWithReplacerAndIndent: ('a, jsonReplacer, int) => option = "JSON.stringify" -module Decode = { +module Classify = { type t = | Bool(bool) | Null @@ -60,3 +60,24 @@ module Encode = { external object: Core__Dict.t => t = "%identity" external array: array => t = "%identity" } + +module Decode = { + let bool = (json: t) => + Core__Type.typeof(json) === #boolean ? Some((Obj.magic(json): bool)) : None + let null = (json: t) => Obj.magic(json) === Core__Null.null ? Some(Core__Null.null) : None + let string = (json: t) => + Core__Type.typeof(json) === #string ? Some((Obj.magic(json): string)) : None + let float = (json: t) => + Core__Type.typeof(json) === #number ? Some((Obj.magic(json): float)) : None + let object = (json: t) => + if ( + Core__Type.typeof(json) === #object && + !Core__Array.isArray(json) && + !(Obj.magic(json) === Core__Null.null) + ) { + Some((Obj.magic(json): Core__Dict.t)) + } else { + None + } + let array = (json: t) => Core__Array.isArray(json) ? Some((Obj.magic(json): array)) : None +} diff --git a/src/Core__JSON.resi b/src/Core__JSON.resi new file mode 100644 index 00000000..fc086a01 --- /dev/null +++ b/src/Core__JSON.resi @@ -0,0 +1,55 @@ +type t = Js.Json.t + +type jsonReviver +external asJsonReviver: 'a => jsonReviver = "%identity" +type jsonReplacer +external asJsonReplacer: 'a => jsonReplacer = "%identity" + +@val external parseExn: string => t = "JSON.parse" +@val external parseExnWithReviver: (string, jsonReviver) => t = "JSON.parse" +@val external stringify: t => string = "JSON.stringify" +@val external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify" +@val external stringifyWithReplacer: (t, jsonReplacer) => string = "JSON.stringify" +@val external stringifyWithReplacerAndIndent: (t, jsonReplacer, int) => string = "JSON.stringify" + +@val external parseToAnyExn: string => 'a = "JSON.parse" +@val external parseToAnyExnWithReviver: (string, jsonReviver) => 'a = "JSON.parse" +@val external stringifyAny: 'a => option = "JSON.stringify" +@val +external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option = "JSON.stringify" +@val +external stringifyAnyWithReplacer: ('a, jsonReplacer) => option = "JSON.stringify" +@val +external stringifyAnyWithReplacerAndIndent: ('a, jsonReplacer, int) => option = + "JSON.stringify" + +module Classify: { + type t = + | Bool(bool) + | Null + | String(string) + | Number(float) + | Object(Core__Dict.t) + | Array(array) + + let classify: 'a => t +} + +module Encode: { + external bool: bool => t = "%identity" + external null: t = "#null" + external string: string => t = "%identity" + external int: int => t = "%identity" + external float: float => t = "%identity" + external object: Core__Dict.t => t = "%identity" + external array: array => t = "%identity" +} + +module Decode: { + let bool: t => option + let null: t => option> + let string: t => option + let float: t => option + let object: t => option> + let array: t => option> +} diff --git a/src/Core__Null.res b/src/Core__Null.res index dc6e2fb5..2b9c0c9e 100644 --- a/src/Core__Null.res +++ b/src/Core__Null.res @@ -2,7 +2,7 @@ type t<'a> = Js.Null.t<'a> external asNullable: t<'a> => Core__Nullable.t<'a> = "%identity" -external empty: t<'a> = "#null" +external null: t<'a> = "#null" external make: 'a => t<'a> = "%identity" @@ -11,5 +11,5 @@ external toOption: t<'a> => option<'a> = "#null_to_opt" let fromOption: option<'a> => t<'a> = option => switch option { | Some(x) => make(x) - | None => empty + | None => null } diff --git a/src/Core__Nullable.res b/src/Core__Nullable.res index 2f3e9a2d..044a037b 100644 --- a/src/Core__Nullable.res +++ b/src/Core__Nullable.res @@ -4,8 +4,6 @@ external null: t<'a> = "#null" external undefined: t<'a> = "#undefined" -external empty: t<'a> = "#undefined" - external make: 'a => t<'a> = "%identity" external toOption: t<'a> => option<'a> = "#nullable_to_opt" @@ -13,5 +11,5 @@ external toOption: t<'a> => option<'a> = "#nullable_to_opt" let fromOption: option<'a> => t<'a> = option => switch option { | Some(x) => make(x) - | None => empty + | None => undefined } diff --git a/src/RescriptCore.mjs b/src/RescriptCore.mjs index dbccf7a7..281e3abb 100644 --- a/src/RescriptCore.mjs +++ b/src/RescriptCore.mjs @@ -15,8 +15,6 @@ var $$Error; var Float; -var Global; - var Int; var $$BigInt; @@ -107,7 +105,6 @@ export { Dict , $$Error , Float , - Global , Int , $$BigInt , $$Math , diff --git a/test/TempTests.mjs b/test/TempTests.mjs index 21d3ae2c..e26239c0 100644 --- a/test/TempTests.mjs +++ b/test/TempTests.mjs @@ -118,14 +118,14 @@ console.info("---"); var json = JSON.parse("{\"foo\": \"bar\"}"); -var json$1 = Core__JSON.Decode.classify(json); +var json$1 = Core__JSON.Classify.classify(json); var tmp; if (typeof json$1 === "number" || json$1.TAG !== /* Object */3) { tmp = undefined; } else { - var value = Core__JSON.Decode.classify(json$1._0["foo"]); + var value = Core__JSON.Classify.classify(json$1._0["foo"]); tmp = typeof value === "number" || value.TAG !== /* String */1 ? undefined : value._0; } diff --git a/test/TempTests.res b/test/TempTests.res index f936a283..b300526a 100644 --- a/test/TempTests.res +++ b/test/TempTests.res @@ -57,9 +57,9 @@ Console.info("JSON") Console.info("---") let json = JSON.parseExn(`{"foo": "bar"}`) Console.log( - switch JSON.Decode.classify(json) { + switch JSON.Classify.classify(json) { | Object(json) => - switch JSON.Decode.classify(json->Dict.get("foo")) { + switch JSON.Classify.classify(json->Dict.get("foo")) { | String(value) => Some(value) | _ => None }