Skip to content

Commit

Permalink
Rendering JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Sep 21, 2024
1 parent 685c564 commit 5858cf9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions universal/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions universal/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
91 changes: 91 additions & 0 deletions universal/test/formats/rendering_json.gleam
Original file line number Diff line number Diff line change
@@ -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", ""),
)
}

0 comments on commit 5858cf9

Please sign in to comment.