Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic parsing JSON example #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions universal/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lustre = ">= 4.4.4 and < 5.0.0"
gleam_crypto = ">= 1.3.0 and < 2.0.0"
tom = ">= 1.1.0 and < 2.0.0"
envoy = ">= 1.0.2 and < 2.0.0"
decode = ">= 0.4.1"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions universal/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# You typically do not need to edit this file

packages = [
{ name = "decode", version = "0.4.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "90C83E830B380EAF64A0A20D0116C4C173AD753594AF1A37E692C1A699244244" },
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
Expand All @@ -18,6 +19,7 @@ packages = [
]

[requirements]
decode = { version = ">= 0.4.1" }
envoy = { version = ">= 1.0.2 and < 2.0.0" }
gleam_crypto = { version = ">= 1.3.0 and < 2.0.0" }
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
Expand Down
59 changes: 59 additions & 0 deletions universal/test/formats/parsing_json.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//// # Parsing JSON
////
//// The gleam_json library can be used to decode JSON.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/gleam_json

import decode/zero
import gleam/json
import gleam/option
import gleeunit/should

pub type User {
User(name: String, age: Int, is_admin: Bool)
}

pub fn main_test() {
// Simple types
let decoder = {
use name <- zero.field("name", zero.string)
zero.success(name)
}
"{\"name\": \"cookbook\"}"
|> json.decode(zero.run(_, decoder))
|> should.equal(Ok("cookbook"))

// Lists
let decoder = {
use scores <- zero.field("scores", zero.list(zero.int))
zero.success(scores)
}
"{\"scores\": [1, 2, 3]}"
|> json.decode(zero.run(_, decoder))
|> should.equal(Ok([1, 2, 3]))

// Optionals
let decoder = {
use name <- zero.field("name", zero.optional(zero.string))
zero.success(name)
}
"{\"name\": \"cookbook\"}"
|> json.decode(zero.run(_, decoder))
|> should.equal(Ok(option.Some("cookbook")))
"{\"name\": null}"
|> json.decode(zero.run(_, decoder))
|> should.equal(Ok(option.None))

// Records
let decoder = {
use name <- zero.field("name", zero.string)
use age <- zero.field("age", zero.int)
use is_admin <- zero.field("is_admin", zero.bool)
zero.success(User(name:, age:, is_admin:))
}
"{\"name\": \"Alice\", \"age\": 42, \"is_admin\": true}"
|> json.decode(zero.run(_, decoder))
|> should.equal(Ok(User("Alice", 42, True)))
}