glitr_convert

Package Version Hex Docs

Encode and decode from and to Gleam types effortlessly !

Define a converter once and encode and decode as much as you want.

Installation

gleam add glitr_convert

Usage

import glitr_convert/converter as c
import gleam/json

type Person {
  Person(name: String, age: Int)
}

pub fn main() {
  let converter = c.object({
    use name <- c.parameter
    use age <- c.parameter
    use <- c.constructor
    Person(name:, age:)
  })
  |> field("name", fn(v) { Ok(v.name) }, c.string())
  |> field("age", fn(v) { Ok(v.age) }, c.int())
  |> c.to_converter

  converter
  |> c.json_encode(Person("Anna", 21))
  |> json.to_string
  // '{"name": "Anna", "age": 21}'

  "{\"name\": \"Bob\", \"age\": 36}"
  |> json.decode(c.json_decode(converter, _))
  // Ok(Person("Bob", 36))
}

Further documentation can be found at https://hexdocs.pm/glitr_convert.

Features

Potential developments

Feel free to open PRs and issues if you want more features !

Development

gleam run   # Run the project
gleam test  # Run the tests
Search Document