README.md

# dirty_deeds_done_dirt_cheap

[![Package Version](https://img.shields.io/hexpm/v/dirty_deeds_done_dirt_cheap)](https://hex.pm/packages/dirty_deeds_done_dirt_cheap)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/dirty_deeds_done_dirt_cheap/)

This is convenient utility library to Convert value of any type to a JSON string in the [Gleam programming language](https://gleam.run).

This is useful when doing rapid prototyping and making one way one-time/quick-test API (encode only)...

This library currently only support the JavaScript runtime.

## Warning

> ⚠️ ⚠️ ⚠️ WARNING: This library relies on undocumented parts of the compiler that might change in the future. Use at your own risk! ⚠️ ⚠️ ⚠️

Here are some reasons to not use this library:

Although this utility is convenient, it's against the language design intention. Quote from the creator of Gleam:

> One thing to keep in mind is Gleam's design with encoding and decoding is intentional. We don't have explicit structure to the types in our programs because we are stuck with them, we are explicit because we think it is the better choice!

Also Gleam LSP have a have built-in code actions to generate dynamic decoder and generate to-JSON function (if you have [`gleam_json`](hexdocs.pm/gleam_json) installed). So if you don't have to update encode/decode functions for many complex types lot of time, you don't even need this library. The code actions are already good enough.

> ⚠️ 🛑 ⚠️ 🛑 NOTE: _**DO NOT**_ report bug or issue to upstream project! 🛑 ⚠️ 🛑 ⚠️

## Installation

```sh
gleam add dirty_deeds_done_dirt_cheap
```

```gleam
import dirty_deeds_done_dirt_cheap

type CustomType(a, b) {
  Basic
  BasicWithValues(a: a, b: b)
}

pub fn main() {
  Nil
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> null

  True
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> true

  False
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> false

  69
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> 69

  -3.14
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> -3.14

  "foo"
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> "foo"

  #(Nil, True, -2, 3.14, "bar")
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> [null, true, -2, 3.14, "bar"]

  <<1:9, 2:8, 3:7, 4:6>>
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> {
  //   bit_size: 30,
  //   byte_size: 4,
  //   bit_offset: 0,
  //   raw_buffer: [0, 129, 3, 16],
  // }

  Some("Thing")
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> "Thing"

  None
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> null

  Ok(3.14)
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> { TYPE: "Ok", 0: 3.14 }

  Error("baz")
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> { TYPE: "Error", 0: "baz" }

  [0, 1, 2, 3, 4, 5]
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> [0, 1, 2, 3, 4, 5]

  set.from_list(["a", "b", "c"])
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> ["a", "b", "c"]

  dict.from_list([#("a", 1), #("b", 2), #("c", 3)])
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> { "a": 1, "b": 2, "c": 3 }

  dict.from_list([
    #(Error("1"), "a"),
    #(Ok(2), "b"),
    #(Ok(3), "c")
  ])
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> {
  //   '{"0":"1","TYPE":"Error"}': "a",
  //   '{"0":2,"TYPE":"Ok"}': "b",
  //   '{"0":3,"TYPE":"Ok"}': "c",
  // }

  Basic
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> { TYPE: "Basic" }

  BasicWithValues(a: 3.14, b: "PI")
  |> dirty_deeds_done_dirt_cheap.convert_to_json_string_with_understanding_of_risks_and_reasons()
  // -> { TYPE: "BasicWithValues", a: 3.14, b: "PI" }
}
```

Further documentation can be found at <https://hexdocs.pm/dirty_deeds_done_dirt_cheap>.