dirty_deeds_done_dirt_cheap

Values

pub fn convert_to_json_string_with_understanding_of_risks_and_reasons(
  value: anything,
) -> String

Deprecated: This function relies on undocumented parts of the compiler that might change in the future. Use at your own risk!

Convert a value to a JSON string.

This function with convert value types as follows:

  • Gleam’s Nil to JSON’s null.
  • Gleam’s True to JSON’s true.
  • Gleam’s False to JSON’s false.
  • Gleam’s String to JSON’s string.
  • Gleam’s Int and Float to JSON’s number.
  • Gleam’s option.Some will unwarp its internal value and be converted depend on its type.
  • Gleam’s option.None to JSON’s null.
  • Gleam’s tuple, List and set.Set to JSON’s array and all of its values inside with also be converted recursively.
  • Gleam’s dict.Dict to JSON’s object and all of its keys/values with also be converted recursively.
  • Gleam’s BitArray to JSON’s object of { bit_size: Number, byte_size: Number, bit_offset: Number, raw_buffer: Array<Number> }.
  • Gleam’s custom type to JSON’s object with its type variant name in type: String and all of its fields be converted recursively (unlabeled fields will be numbered instead).
  • Other type will be keep the same.

Examples

Nil
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> null
True
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> true
False
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> false
69
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> 69
-3.14
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> -3.14
"foo"
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> "foo"
#(Nil, True, -2, 3.14, "bar")
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> [null, true, -2, 3.14, "bar"]
<<1:9, 2:8, 3:7, 4:6>>
|> 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")
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> "Thing"
None
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> null
Ok(3.14)
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> { type: "Ok", 0: 3.14 }
Error("baz")
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> { type: "Error", 0: "baz" }
[0, 1, 2, 3, 4, 5]
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> [0, 1, 2, 3, 4, 5]
set.from_list(["a", "b", "c"])
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> ["a", "b", "c"]
dict.from_list([#("a", 1), #("b", 2), #("c", 3)])
|> 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")
])
|> 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",
// }
type CustomType(a, b) {
  Basic
  BasicWithValues(a: a, b: b)
}
Basic
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> { type: "Basic" }
BasicWithValues(a: 3.14, b: "PI")
|> convert_to_json_string_with_understanding_of_risks_and_reasons()
// -> { type: "BasicWithValues", a: 3.14, b: "PI" }
Search Document