aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-05-23 19:24:29 +0100
committerLouis Pilfold <louis@lpil.uk>2024-05-23 19:32:48 +0100
commit9753cfad7d937c42db64baee5b9cc818effe984b (patch)
tree62f4aa9582a2c3d60ca832bbccb1160d5c124822
parent1979919b792910e6c75f3e28596f017d980b45ee (diff)
downloadgleam_json-9753cfad7d937c42db64baee5b9cc818effe984b.tar.gz
gleam_json-9753cfad7d937c42db64baee5b9cc818effe984b.zip
Encoding also!
-rw-r--r--gleam.toml2
-rw-r--r--manifest.toml2
-rw-r--r--src/gleam_json_ffi.erl22
-rw-r--r--test/gleam_json_test.gleam16
4 files changed, 31 insertions, 11 deletions
diff --git a/gleam.toml b/gleam.toml
index e9304d8..e19fa60 100644
--- a/gleam.toml
+++ b/gleam.toml
@@ -2,7 +2,6 @@ name = "gleam_json"
version = "1.0.1"
gleam = ">= 0.32.0"
-target = "javascript"
licences = ["Apache-2.0"]
description = "Work with JSON in Gleam"
@@ -14,7 +13,6 @@ links = [
[dependencies]
gleam_stdlib = ">= 0.19.0 and < 2.0.0"
-thoas = ">= 0.2.0 and < 2.0.0"
[dev-dependencies]
gleeunit = "~> 1.0"
diff --git a/manifest.toml b/manifest.toml
index 18592fb..1a17cd5 100644
--- a/manifest.toml
+++ b/manifest.toml
@@ -4,10 +4,8 @@
packages = [
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
- { name = "thoas", version = "1.2.0", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "540C8CB7D9257F2AD0A14145DC23560F91ACDCA995F0CCBA779EB33AF5D859D1" },
]
[requirements]
gleam_stdlib = { version = ">= 0.19.0 and < 2.0.0" }
gleeunit = { version = "~> 1.0" }
-thoas = { version = ">= 0.2.0 and < 2.0.0" }
diff --git a/src/gleam_json_ffi.erl b/src/gleam_json_ffi.erl
index 2bfd6c6..888ae51 100644
--- a/src/gleam_json_ffi.erl
+++ b/src/gleam_json_ffi.erl
@@ -26,10 +26,18 @@ json_to_string(Json) when is_binary(Json) ->
json_to_string(Json) when is_list(Json) ->
list_to_binary(Json).
-null() -> thoas_encode:null().
-int(X) -> thoas_encode:integer(X).
-bool(X) -> thoas_encode:boolean(X).
-float(X) -> thoas_encode:float(X).
-string(X) -> thoas_encode:string(X).
-object(X) -> thoas_encode:non_recursive_object(X).
-array(X) -> thoas_encode:non_recursive_array(X).
+null() -> <<"null">>.
+bool(true) -> <<"true">>;
+bool(false) -> <<"false">>.
+int(X) -> json:encode_integer(X).
+float(X) -> json:encode_float(X).
+string(X) -> json:encode_binary(X).
+
+array([]) -> <<"[]">>;
+array([First | Rest]) -> [$[, First | array_loop(Rest)].
+array_loop([]) -> "]";
+array_loop([Elem | Rest]) -> [$,, Elem | array_loop(Rest)].
+
+object(List) -> encode_object([[$,, string(Key), $: | Value] || {Key, Value} <- List]).
+encode_object([]) -> <<"{}">>;
+encode_object([[_Comma | Entry] | Rest]) -> ["{", Entry, Rest, "}"].
diff --git a/test/gleam_json_test.gleam b/test/gleam_json_test.gleam
index fcc4a62..e2f3542 100644
--- a/test/gleam_json_test.gleam
+++ b/test/gleam_json_test.gleam
@@ -93,6 +93,17 @@ pub fn encode_object_test() {
|> should_encode("{\"foo\":5}")
}
+pub fn encode_empty_object_test() {
+ json.object([])
+ |> should_encode("{}")
+}
+
+pub fn encode_empty_array_test() {
+ []
+ |> json.array(of: json.int)
+ |> should_encode("[]")
+}
+
pub fn encode_array_test() {
[5, 6, 1, 4]
|> json.array(of: json.int)
@@ -104,6 +115,11 @@ pub fn encode_preprocessed_array_test() {
|> should_encode("[5,6]")
}
+pub fn encode_empty_preprocessed_array_test() {
+ json.preprocessed_array([])
+ |> should_encode("[]")
+}
+
pub fn encode_nullable_some_test() {
json.nullable(Some(5), of: json.int)
|> should_encode("5")