From 5858cf9ba8167f230c5f7fb1a420ed6e7b2b35ed Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Sat, 21 Sep 2024 22:34:31 +0100 Subject: [PATCH] Rendering JSON --- README.md | 1 + universal/gleam.toml | 1 + universal/manifest.toml | 3 + universal/test/formats/rendering_json.gleam | 91 +++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 universal/test/formats/rendering_json.gleam diff --git a/README.md b/README.md index 1140915..8c61216 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,5 @@ Examples showing how to do many things in Gleam! ## Formats +- [Rendering JSON](./universal/test/formats/rendering_json.gleam) - [Rendering XML](./universal/test/formats/rendering_xml.gleam) diff --git a/universal/gleam.toml b/universal/gleam.toml index a8d6d1d..99d7f9a 100644 --- a/universal/gleam.toml +++ b/universal/gleam.toml @@ -16,6 +16,7 @@ version = "1.0.0" gleam_stdlib = ">= 0.34.0 and < 2.0.0" simplifile = ">= 2.1.0 and < 3.0.0" xmb = ">= 1.0.0 and < 2.0.0" +gleam_json = ">= 1.0.1 and < 2.0.0" [dev-dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/universal/manifest.toml b/universal/manifest.toml index 4d62d57..ec26927 100644 --- a/universal/manifest.toml +++ b/universal/manifest.toml @@ -3,13 +3,16 @@ packages = [ { name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, + { name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" }, { name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" }, { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, { name = "simplifile", version = "2.1.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "BDD04F5D31D6D34E2EDFAEF0B68A6297AEC939888C3BFCE61133DE13857F6DA2" }, + { name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" }, { name = "xmb", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "xmb", source = "hex", outer_checksum = "99425CD67BA2AF3E4A38515FB4BFCC121007B69444C81DE6EB837421290F14B4" }, ] [requirements] +gleam_json = { version = ">= 1.0.1 and < 2.0.0" } gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } simplifile = { version = ">= 2.1.0 and < 3.0.0" } diff --git a/universal/test/formats/rendering_json.gleam b/universal/test/formats/rendering_json.gleam new file mode 100644 index 0000000..1604fe0 --- /dev/null +++ b/universal/test/formats/rendering_json.gleam @@ -0,0 +1,91 @@ +//// # Generate JSON +//// +//// The gleam_json package can be used to generate JSON. It works on any +//// target. +//// +//// ## Dependencies +//// +//// - https://hex.pm/packages/gleam_json + +import gleam/json +import gleam/string +import gleeunit/should + +pub fn main_test() { + // The `gleam/json` module has a function for each different type of data in + // a JSON document. e.g. `bool`, `string`, `int`, etc. + // + // The `preprocessed_array` function is used for arrays made from list + // that already contain JSON data. + // + let document = + json.object([ + #( + "bookstore", + json.preprocessed_array([ + json.object([ + #("genre", json.string("Technology")), + #("title", json.string("Introduction to JSON")), + #("author", json.string("Kwame Nkrumah")), + ]), + json.object([ + #("genre", json.string("Programming")), + #("title", json.string("Learning Gleam")), + #("author", json.string("Mei Wong")), + ]), + ]), + ), + ]) + + // The document can be converted to a string + document + |> json.to_string + |> should.equal( + " +{ + \"bookstore\":[ + { + \"genre\":\"Technology\", + \"title\":\"Introduction to JSON\", + \"author\":\"Kwame Nkrumah\" + }, + { + \"genre\":\"Programming\", + \"title\":\"Learning Gleam\", + \"author\":\"Mei Wong\" + } + ] +} + " + |> string.replace(" ", "") + |> string.replace("\n", ""), + ) + + // If you have a collection of items and you wish to map over each of them to + // make JSON then you can use the `array` function rather than the + // `preprocessed_array` function. + let directions = ["North", "East", "South", "West"] + json.object([ + #( + "directions", + json.array(directions, fn(direction) { + json.object([#("name", json.string(direction))]) + }), + ), + ]) + |> json.to_string + |> should.equal( + " +{ + \"directions\":[ + {\"name\":\"North\"}, + {\"name\":\"East\"}, + {\"name\":\"South\"}, + {\"name\":\"West\"} + ] +} + " + |> string.replace(" ", "") + |> string.replace("\n", ""), + ) +}