diff options
-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}") |