diff options
author | mrkutly <mark.sauer.utley@gmail.com> | 2022-05-23 17:43:51 -0400 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-06-11 18:39:10 +0100 |
commit | 46612fb5d02f6b1fa4f97686fe6eed6eb13a0321 (patch) | |
tree | 0f767d98c7141fc1304efe667396cc56c9ec500c /src/gleam_json_ffi.mjs | |
parent | c6e5c8b748401766ba0592fecc872fadff18bed4 (diff) | |
download | gleam_json-46612fb5d02f6b1fa4f97686fe6eed6eb13a0321.tar.gz gleam_json-46612fb5d02f6b1fa4f97686fe6eed6eb13a0321.zip |
use identity and direct string value in javascript ffi
Diffstat (limited to 'src/gleam_json_ffi.mjs')
-rw-r--r-- | src/gleam_json_ffi.mjs | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/src/gleam_json_ffi.mjs b/src/gleam_json_ffi.mjs index 0d9021c..64d90b0 100644 --- a/src/gleam_json_ffi.mjs +++ b/src/gleam_json_ffi.mjs @@ -6,10 +6,14 @@ export function json_to_string(json) { return JSON.stringify(json) } -export function object_from(entries) { +export function object(entries) { return Object.fromEntries(entries) } +export function identity(x) { + return x +} + export function array(list) { return list.toArray() } @@ -18,23 +22,6 @@ export function do_null() { return null } -export function string(x) { - return json_to_string(x) -} - -export function int(x) { - return parseInt(json_to_string(x), 10) -} - -export function float(x) { - return parseFloat(json_to_string(x), 10) -} - -export function bool(x) { - return Boolean(x) -} - - export function decode(bit_string) { const stringResult = bit_string_to_string(bit_string) if (!stringResult.isOk()) return stringResult @@ -46,6 +33,15 @@ export function decode(bit_string) { } } +export function decode_string_to_dynamic(string) { + try { + const result = JSON.parse(string) + return new Ok(result) + } catch (err) { + return new Error(getJsonDecodeError(err)) + } +} + function getJsonDecodeError(stdErr) { if (isUnexpectedByte(stdErr)) return new toUnexpectedByteError(stdErr) if (isUnexpectedEndOfInput(stdErr)) return new UnexpectedEndOfInput() |