-
I would love to have the strong type guarantee of an Union, but render as an Int into JSON. What is good way to achieve this? |
Beta Was this translation helpful? Give feedback.
Answered by
bioball
Feb 27, 2024
Replies: 1 comment
-
I think the best thing to do here is to add a type constraint, rather than a union. num: Int(this == 1 || this == 2 || this == 3) One other idea is to use a string literal union, and plus a converter. But this feels like a little bit of unnecessary indirection. num: "1"|"2"|"3"
output {
renderer = new JsonRenderer {
converters {
["num"] = (it) -> it.toInt()
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
bfloch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the best thing to do here is to add a type constraint, rather than a union.
One other idea is to use a string literal union, and plus a converter. But this feels like a little bit of unnecessary indirection.