Skip to content

How to express an Enum that renders as Int in JSON? #258

Answered by bioball
bfloch asked this question in Q&A
Discussion options

You must be logged in to vote

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()
    }
  }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by bfloch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants