dirty_deeds_done_dirt_cheap
Values
pub fn convert_with_understanding_of_risks_and_reasons(
value: anything,
) -> dynamic.Dynamic
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 JSON.
This function with convert value types as follows:
- Gleam’s
Nilto JSON’snull. - Gleam’s
Trueto JSON’strue. - Gleam’s
Falseto JSON’sfalse. - Gleam’s
Stringto JSON’s string. - Gleam’s
IntandFloatto JSON’s number. - Gleam’s
option.Somewill unwarp its internal value and be converted depend on its type. - Gleam’s
option.Noneto JSON’snull. - Gleam’s tuple,
Listandset.Setto JSON’sarrayand all of its values inside with also be converted recursively. - Gleam’s
dict.Dictto JSON’s object and all of its keys/values with also be converted recursively. - Gleam’s
BitArrayto 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: Stringand all of its fields be converted recursively (unlabeled fields will be numbered instead). - Other type will be keep the same.
Examples
Nil
|> convert_with_understanding_of_risks_and_reasons()
// -> null
True
|> convert_with_understanding_of_risks_and_reasons()
// -> true
False
|> convert_with_understanding_of_risks_and_reasons()
// -> false
69
|> convert_with_understanding_of_risks_and_reasons()
// -> 69
-3.14
|> convert_with_understanding_of_risks_and_reasons()
// -> -3.14
"foo"
|> convert_with_understanding_of_risks_and_reasons()
// -> "foo"
#(Nil, True, -2, 3.14, "bar")
|> convert_with_understanding_of_risks_and_reasons()
// -> [null, true, -2, 3.14, "bar"]
<<1:9, 2:8, 3:7, 4:6>>
|> convert_with_understanding_of_risks_and_reasons()
// -> {
// bit_size: 30,
// byte_size: 4,
// bit_offset: 0,
// raw_buffer: [0, 129, 3, 16],
// }
Some("Thing")
|> convert_with_understanding_of_risks_and_reasons()
// -> "Thing"
None
|> convert_with_understanding_of_risks_and_reasons()
// -> null
Ok(3.14)
|> convert_with_understanding_of_risks_and_reasons()
// -> { type: "ok", 0: 3.14 }
Error("baz")
|> convert_with_understanding_of_risks_and_reasons()
// -> { type: "error", 0: "baz" }
[0, 1, 2, 3, 4, 5]
|> convert_with_understanding_of_risks_and_reasons()
// -> [0, 1, 2, 3, 4, 5]
set.from_list(["a", "b", "c"])
|> convert_with_understanding_of_risks_and_reasons()
// -> ["a", "b", "c"]
dict.from_list([#("a", 1), #("b", 2), #("c", 3)])
|> convert_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_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_with_understanding_of_risks_and_reasons()
// -> { type: "basic" }
BasicWithValues(a: 3.14, b: "PI")
|> convert_with_understanding_of_risks_and_reasons()
// -> { type: "basic_with_values", a: 3.14, b: "PI" }