diff --git a/universal/gleam.toml b/universal/gleam.toml index daaff51..78e0511 100644 --- a/universal/gleam.toml +++ b/universal/gleam.toml @@ -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" diff --git a/universal/manifest.toml b/universal/manifest.toml index 69ac9f9..225d20b 100644 --- a/universal/manifest.toml +++ b/universal/manifest.toml @@ -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" }, @@ -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" } diff --git a/universal/test/formats/parsing_json.gleam b/universal/test/formats/parsing_json.gleam new file mode 100644 index 0000000..4e7ffc6 --- /dev/null +++ b/universal/test/formats/parsing_json.gleam @@ -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))) +}