diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-01 17:15:05 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-01 17:15:05 +0000 |
commit | e9421e168de9428c6b260e67f808f0d4db032cd5 (patch) | |
tree | 8ff47b9ff58c7336c651e740920c31a66fd7b41c | |
parent | c3d35312ca49089e9603971270a44b83c3b0527d (diff) | |
download | gleam_json-e9421e168de9428c6b260e67f808f0d4db032cd5.tar.gz gleam_json-e9421e168de9428c6b260e67f808f0d4db032cd5.zip |
float encoder
-rw-r--r-- | src/gleam/json.gleam | 12 | ||||
-rw-r--r-- | test/gleam_json_test.gleam | 16 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/gleam/json.gleam b/src/gleam/json.gleam index c649ac3..8c3652c 100644 --- a/src/gleam/json.gleam +++ b/src/gleam/json.gleam @@ -98,6 +98,18 @@ pub external fn bool(input: Bool) -> Json = pub external fn int(input: Int) -> Json = "thoas_encode" "integer" +/// Encode an float into JSON. +/// +/// ## Examples +/// +/// ```gleam +/// > to_string(float(4.7)) +/// "4.7" +/// ``` +/// +pub external fn float(input: Float) -> Json = + "thoas_encode" "float" + /// The JSON value null. /// /// ## Examples diff --git a/test/gleam_json_test.gleam b/test/gleam_json_test.gleam index 79f17f9..7e93ab7 100644 --- a/test/gleam_json_test.gleam +++ b/test/gleam_json_test.gleam @@ -36,6 +36,22 @@ pub fn encode_null_test() { |> should_encode("null") } +pub fn encode_int_test() { + json.int(-50) + |> should_encode("-50") + + json.int(100) + |> should_encode("100") +} + +pub fn encode_float_test() { + json.float(-50.5) + |> should_encode("-50.5") + + json.float(100.0) + |> should_encode("100.0") +} + pub fn encode_object_test() { json.object([#("foo", json.int(5))]) |> should_encode("{\"foo\":5}") |