hi folks!
I'm toying with basic-webserver and have a question regarding Json.to_str. it works! but parts of the signature seem to be somehow internal, so I can't just copy paste it to my code, and every time I try to type anything around it I get errors.
can I express "a" as a "Json-able" value yet ?
thanks!
If you have a FooDto like this:
FooDto := {
bar: Str,
# ...
}
and you want to say "yo I want to turn this into JSON", then you can add this encoder_for : _ definition, and static dispatch should handle the rest. Your type would then look like this:
FooDto := {
bar: Str,
# ...
}. {
encoder_for : _
}
If you have code like:
a = something_that_returns_foo_dto!()?
then you might need to add a type annotation like this:
a : FooDto
a = something_that_returns_foo_dto!()?
Does this answer your question? If not, a code example of yours might help.
I have a related issue where this:
respond : Status, a -> Response where [
a.encoder_for : Encoding.JsonEncoding ->
(a, Encoding.JsonEncodeState ->
Try(Encoding.JsonEncodeState, [])),
]
respond = |code, body| {
Response.from_status(code.to_u16())
.with_headers([
{
name: "Content-Type",
value: "application/json; charset=utf-8",
},
])
.with_body(Json.to_str(body).to_utf8())
}
doesn't compile because JsonEncoding and cie. are not in scope. Yet they seem to be part of the public documentation?
I know we can use a type hole, but it would be nice to make the "json-able" type explicit in the signature.
I was able to compile this code, the encoder_for had to be a one argument function though :
ok : a -> Try(Response, _)
where [
a.encoder_for : _ -> _,
]
ok = |response|
Ok(
Response.from_status(200)
.with_headers([{ name: "Content-Type", value: "application/json; charset=utf-8" }])
.with_body(Str.to_utf8(Json.to_str(response)))
)
@Maxime Pigeon precisely ! JsonEncoding is there, the compiler can find it, but we can't reference it.
(PS: cool prénom, probablement le meilleur)
Last updated: Sep 03 2026 at 15:16 UTC