diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-01 15:50:49 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-01 15:50:49 +0000 |
commit | 30248f52a042fa5de08b08e79d76aa3d6a9c4eaf (patch) | |
tree | 2292f01cfe136bbccf7223c7ed199c371aa02734 /src | |
parent | 87c49c3fc248ceca5557c4f873a468884a9ca14a (diff) | |
download | gleam_json-30248f52a042fa5de08b08e79d76aa3d6a9c4eaf.tar.gz gleam_json-30248f52a042fa5de08b08e79d76aa3d6a9c4eaf.zip |
Convert to Thoas
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/json.gleam | 111 | ||||
-rw-r--r-- | src/gleam/json/types.gleam | 12 | ||||
-rw-r--r-- | src/gleam_json_ffi.erl | 11 |
3 files changed, 66 insertions, 68 deletions
diff --git a/src/gleam/json.gleam b/src/gleam/json.gleam index 74a9746..43c8dda 100644 --- a/src/gleam/json.gleam +++ b/src/gleam/json.gleam @@ -1,75 +1,74 @@ -import gleam/dynamic.{Dynamic} import gleam/map -import gleam/option.{Option} import gleam/result -import gleam/json/types - -pub type Json = - types.Json +import gleam/option.{None, Option, Some} +import gleam/dynamic.{Dynamic} +import gleam/string_builder.{StringBuilder} -external fn jsone_decode(String) -> types.DecodeResult = - "jsone_decode" "decode" +pub external type Json -pub fn decode(encoded: String) -> Result(Json, Dynamic) { - case jsone_decode(encoded) { - types.Ok(json, _rest) -> Ok(json) - types.Error(types.Badarg(reason)) -> Error(reason) - } +pub type DecodeError { + UnexpectedEndOfInput + UnexpectedByte(byte: String, position: Int) + UnexpectedSequence(byte: String, position: Int) } -external fn jsone_encode(Json) -> Result(String, types.DecodeResult) = - "jsone_encode" "encode" +// TODO: document +// TODO: test +pub external fn decode(String) -> Result(Dynamic, DecodeError) = + "thoas" "decode" -pub fn encode(json: Json) -> String { - // The encoder only error if input is invalid, i.e. a PID. - // This cannot happen when passing in the the Json type. - assert Ok(encoded) = jsone_encode(json) - encoded -} +// TODO: document +// TODO: test +pub external fn to_string(Json) -> String = + "gleam_json_ffi" "json_to_string" -pub fn string(input: String) -> Json { - input - |> dynamic.from() - |> dynamic.unsafe_coerce() -} +// TODO: document +// TODO: test +external fn to_string_builder(Json) -> String = + "thoas_encode" "encode" -pub fn bool(input: Bool) -> Json { - input - |> dynamic.from() - |> dynamic.unsafe_coerce() -} +// TODO: document +// TODO: test +pub external fn string(input: String) -> Json = + "thoas_encode" "string" -pub fn int(input: Int) -> Json { - input - |> dynamic.from() - |> dynamic.unsafe_coerce() -} +// TODO: document +// TODO: test +pub external fn bool(input: Bool) -> Json = + "thoas_encode" "boolean" + +// TODO: document +// TODO: test +pub external fn int(input: Int) -> Json = + "thoas_encode" "integer" type Null { Null } -pub fn null() -> Json { - Null - |> dynamic.from() - |> dynamic.unsafe_coerce() -} +// TODO: document +// TODO: test +pub external fn null() -> Json = + "thoas_encode" "null" -pub fn nullable(input: Option(a), mapper: fn(a) -> Json) -> Json { - input - |> option.map(mapper) - |> option.unwrap(null()) +// TODO: document +// TODO: test +// TODO: change argument order +pub fn nullable(input: Option(a), inner: fn(a) -> Json) -> Json { + case input { + Some(value) -> inner(value) + None -> null() + } } -pub fn object(entries: List(#(String, Json))) -> Json { - entries - |> map.from_list() - |> dynamic.from() - |> dynamic.unsafe_coerce() -} +// TODO: document +// TODO: test +pub external fn object(entries: List(#(String, Json))) -> Json = + "thoas_encode" "non_recursive_object" -pub fn list(input: List(Json)) -> Json { - input - |> dynamic.from() - |> dynamic.unsafe_coerce() -} +// TODO: document +// TODO: test +// TODO: rename to array +// TODO: make into a mapping function? +pub external fn list(entries: List(Json)) -> Json = + "thoas_encode" "non_recursive_array" diff --git a/src/gleam/json/types.gleam b/src/gleam/json/types.gleam deleted file mode 100644 index ad341d0..0000000 --- a/src/gleam/json/types.gleam +++ /dev/null @@ -1,12 +0,0 @@ -import gleam/dynamic.{Dynamic} - -pub external type Json - -pub type Failure { - Badarg(Dynamic) -} - -pub type DecodeResult { - Ok(Json, String) - Error(Failure) -} diff --git a/src/gleam_json_ffi.erl b/src/gleam_json_ffi.erl new file mode 100644 index 0000000..94c3cc5 --- /dev/null +++ b/src/gleam_json_ffi.erl @@ -0,0 +1,11 @@ +-module(gleam_json_ffi). + +-export([json_to_iodata/1, json_to_string/1]). + +json_to_iodata(Json) -> + Json. + +json_to_string(Json) when is_binary(Json) -> + Json; +json_to_string(Json) when is_list(Json) -> + list_to_binary(Json). |