aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_json_ffi.erl
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 /src/gleam_json_ffi.erl
parent1979919b792910e6c75f3e28596f017d980b45ee (diff)
downloadgleam_json-9753cfad7d937c42db64baee5b9cc818effe984b.tar.gz
gleam_json-9753cfad7d937c42db64baee5b9cc818effe984b.zip
Encoding also!
Diffstat (limited to 'src/gleam_json_ffi.erl')
-rw-r--r--src/gleam_json_ffi.erl22
1 files changed, 15 insertions, 7 deletions
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, "}"].