aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-01-01 15:50:49 +0000
committerLouis Pilfold <louis@lpil.uk>2022-01-01 15:50:49 +0000
commit30248f52a042fa5de08b08e79d76aa3d6a9c4eaf (patch)
tree2292f01cfe136bbccf7223c7ed199c371aa02734
parent87c49c3fc248ceca5557c4f873a468884a9ca14a (diff)
downloadgleam_json-30248f52a042fa5de08b08e79d76aa3d6a9c4eaf.tar.gz
gleam_json-30248f52a042fa5de08b08e79d76aa3d6a9c4eaf.zip
Convert to Thoas
-rw-r--r--CHANGELOG.md4
-rw-r--r--gleam.toml2
-rw-r--r--manifest.toml4
-rw-r--r--src/gleam/json.gleam111
-rw-r--r--src/gleam/json/types.gleam12
-rw-r--r--src/gleam_json_ffi.erl11
-rw-r--r--test/gleam_json_test.gleam22
7 files changed, 87 insertions, 79 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c2c03ea..2f615d7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,10 @@
## Unreleased
- Converted to use the Gleam build tool.
+- The underlying `jsone` Erlang JSON libary has been replaced with the new
+ `thoas` Erlang JSON library.
+- The `encode` function has been replaced by `to_string` and `to_string_builder`
+ functions.
## v0.1.0 - 2020-07-30
diff --git a/gleam.toml b/gleam.toml
index c5f59c3..18f4631 100644
--- a/gleam.toml
+++ b/gleam.toml
@@ -11,7 +11,7 @@ links = [
[dependencies]
gleam_stdlib = "~> 0.18"
-jsone = "~> 1.7"
+thoas = "~> 0.2"
[dev-dependencies]
gleeunit = "~> 0.5"
diff --git a/manifest.toml b/manifest.toml
index 5fc0b3b..d71063c 100644
--- a/manifest.toml
+++ b/manifest.toml
@@ -4,10 +4,10 @@
packages = [
{ name = "gleam_stdlib", version = "0.18.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "763ECD87D54D08755EE4C8551720D122FDCA47F61D1CA8AF23B19A90395A7468" },
{ name = "gleeunit", version = "0.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7FA7477D930178C1E59519DBDB5E086BE3A6B65F015B67DA94D30A323062154" },
- { name = "jsone", version = "1.7.0", build_tools = ["rebar3"], requirements = [], otp_app = "jsone", source = "hex", outer_checksum = "A3A33712EE6BC8BE10CFA21C7C425A299DE4C5A8533F9F931E577A6D0E8F5DBD" },
+ { name = "thoas", version = "0.2.0", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "630AAEE57FB3FDE201578E787259E15E788A27733D49DE8DCCE1354DB1885B8D" },
]
[requirements]
gleam_stdlib = "~> 0.18"
gleeunit = "~> 0.5"
-jsone = "~> 1.7"
+thoas = "~> 0.2"
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).
diff --git a/test/gleam_json_test.gleam b/test/gleam_json_test.gleam
index 162fa6e..768a994 100644
--- a/test/gleam_json_test.gleam
+++ b/test/gleam_json_test.gleam
@@ -1,9 +1,9 @@
import gleam/dynamic
+import gleam/json
import gleam/option.{None, Some}
import gleam/result
-import gleam/json
-import gleeunit/should
import gleeunit
+import gleeunit/should
pub fn main() {
gleeunit.main()
@@ -19,24 +19,30 @@ pub fn decode_test() {
|> should.equal(Error(Nil))
}
-pub fn encode_test() {
+pub fn encode_string_test() {
json.string("hello")
- |> json.encode()
+ |> json.to_string()
|> should.equal("\"hello\"")
+}
+pub fn encode_null_test() {
json.null()
- |> json.encode()
+ |> json.to_string()
|> should.equal("null")
+}
+pub fn encode_object_test() {
json.object([#("foo", json.int(5))])
- |> json.encode()
+ |> json.to_string()
|> should.equal("{\"foo\":5}")
+}
+pub fn encode_nullable_test() {
json.nullable(Some(5), json.int)
- |> json.encode()
+ |> json.to_string()
|> should.equal("5")
json.nullable(None, json.int)
- |> json.encode()
+ |> json.to_string()
|> should.equal("null")
}