aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrkutly <mark.sauer.utley@gmail.com>2022-05-23 17:43:51 -0400
committerLouis Pilfold <louis@lpil.uk>2022-06-11 18:39:10 +0100
commit46612fb5d02f6b1fa4f97686fe6eed6eb13a0321 (patch)
tree0f767d98c7141fc1304efe667396cc56c9ec500c
parentc6e5c8b748401766ba0592fecc872fadff18bed4 (diff)
downloadgleam_json-46612fb5d02f6b1fa4f97686fe6eed6eb13a0321.tar.gz
gleam_json-46612fb5d02f6b1fa4f97686fe6eed6eb13a0321.zip
use identity and direct string value in javascript ffi
-rw-r--r--src/gleam/json.gleam37
-rw-r--r--src/gleam_json_ffi.mjs32
2 files changed, 44 insertions, 25 deletions
diff --git a/src/gleam/json.gleam b/src/gleam/json.gleam
index 5670949..b8c49f6 100644
--- a/src/gleam/json.gleam
+++ b/src/gleam/json.gleam
@@ -38,8 +38,31 @@ pub fn decode(
from json: String,
using decoder: dynamic.Decoder(t),
) -> Result(t, DecodeError) {
- let bits = bit_string.from_string(json)
- decode_bits(bits, decoder)
+ do_decode(from: json, using: decoder)
+}
+
+if erlang {
+ fn do_decode(
+ from json: String,
+ using decoder: dynamic.Decoder(t),
+ ) -> Result(t, DecodeError) {
+ let bits = bit_string.from_string(json)
+ decode_bits(bits, decoder)
+ }
+}
+
+if javascript {
+ fn do_decode(
+ from json: String,
+ using decoder: dynamic.Decoder(t),
+ ) -> Result(t, DecodeError) {
+ try dynamic_value = decode_string(json)
+ decoder(dynamic_value)
+ |> result.map_error(UnexpectedFormat)
+ }
+
+ external fn decode_string(String) -> Result(Dynamic, DecodeError) =
+ "../gleam_json_ffi.mjs" "decode_string"
}
/// Decode a JSON bit string into dynamically typed data which can be decoded
@@ -158,7 +181,7 @@ if erlang {
if javascript {
external fn do_string(String) -> Json =
- "../gleam_json_ffi.mjs" "string"
+ "../gleam_json_ffi.mjs" "identity"
}
/// Encode a bool into JSON.
@@ -181,7 +204,7 @@ if erlang {
if javascript {
external fn do_bool(Bool) -> Json =
- "../gleam_json_ffi.mjs" "bool"
+ "../gleam_json_ffi.mjs" "identity"
}
/// Encode an int into JSON.
@@ -204,7 +227,7 @@ if erlang {
if javascript {
external fn do_int(Int) -> Json =
- "../gleam_json_ffi.mjs" "int"
+ "../gleam_json_ffi.mjs" "identity"
}
/// Encode an float into JSON.
@@ -227,7 +250,7 @@ if erlang {
if javascript {
external fn do_float(input: Float) -> Json =
- "../gleam_json_ffi.mjs" "float"
+ "../gleam_json_ffi.mjs" "identity"
}
/// The JSON value null.
@@ -297,7 +320,7 @@ if erlang {
if javascript {
external fn do_object(entries: List(#(String, Json))) -> Json =
- "../gleam_json_ffi.mjs" "object_from"
+ "../gleam_json_ffi.mjs" "object"
}
/// Encode a list into a JSON array.
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()