Skip to content

Add JSON decode functions from Js.Json #16

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 3 commits into from
Feb 9, 2023
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
57 changes: 55 additions & 2 deletions src/Core__JSON.mjs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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 */
23 changes: 22 additions & 1 deletion src/Core__JSON.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ external stringifyAnyWithReplacer: ('a, jsonReplacer) => option<string> = "JSON.
external stringifyAnyWithReplacerAndIndent: ('a, jsonReplacer, int) => option<string> =
"JSON.stringify"

module Decode = {
module Classify = {
type t =
| Bool(bool)
| Null
Expand Down Expand Up @@ -60,3 +60,24 @@ module Encode = {
external object: Core__Dict.t<t> => t = "%identity"
external array: array<t> => 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<t>))
} else {
None
}
let array = (json: t) => Core__Array.isArray(json) ? Some((Obj.magic(json): array<t>)) : None
}
55 changes: 55 additions & 0 deletions src/Core__JSON.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
type t = Js.Json.t

type jsonReviver
external asJsonReviver: 'a => jsonReviver = "%identity"
type jsonReplacer
external asJsonReplacer: 'a => jsonReplacer = "%identity"
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of these? They look unsafe, but are not marked as such. And if they can really be anything, why not just allow anything to be passed directly?

Also, as I understand, jsonReviver can really only be a function. So why isn't it a function?


@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<string> = "JSON.stringify"
@val
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
@val
external stringifyAnyWithReplacer: ('a, jsonReplacer) => option<string> = "JSON.stringify"
@val
external stringifyAnyWithReplacerAndIndent: ('a, jsonReplacer, int) => option<string> =
"JSON.stringify"

module Classify: {
type t =
| Bool(bool)
| Null
| String(string)
| Number(float)
| Object(Core__Dict.t<t>)
| Array(array<t>)

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> => t = "%identity"
external array: array<t> => t = "%identity"
}

module Decode: {
let bool: t => option<bool>
let null: t => option<Core__Null.t<'a>>
let string: t => option<string>
let float: t => option<float>
let object: t => option<Core__Dict.t<t>>
let array: t => option<array<t>>
}
4 changes: 2 additions & 2 deletions src/Core__Null.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}
4 changes: 1 addition & 3 deletions src/Core__Nullable.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ 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"

let fromOption: option<'a> => t<'a> = option =>
switch option {
| Some(x) => make(x)
| None => empty
| None => undefined
}
3 changes: 0 additions & 3 deletions src/RescriptCore.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ var $$Error;

var Float;

var Global;

var Int;

var $$BigInt;
Expand Down Expand Up @@ -107,7 +105,6 @@ export {
Dict ,
$$Error ,
Float ,
Global ,
Int ,
$$BigInt ,
$$Math ,
Expand Down
4 changes: 2 additions & 2 deletions test/TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions test/TempTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down