How to make custom `Show` instances for an Enum?

Let’s say I have a record, where I want to use typesafety to prevent myself from using non-prime numbers, ie.

let Prime = < Two | Three | Five >
let myRecord = {..., somePrime : Prime }

This is all well, except for when I serialize it to JSON and I get, say, {..., "somePrime": "Two"}. Instead, I would like this to be of type Natural - or at least be serialized as 2.

I could perhaps use a function such as

myPrettyPrint p = merge { Two = 2, Three = 3, Five = 5} p

My question is - how do I apply this function when I serialize myRecord in a generic way? I might not know, or it might be too tedious, to specify the exact/multiple occurence(s) of Prime in that record.

If this was Haskell, I would make a newtype Prime = Int and create a custom Show instance.

I believe you wouldn’t be able to use the dhall-to-json utility for this purpose, but with some effort you could do it using the Prelude.JSON.render utility:

https://store.dhall-lang.org/Prelude-v20.0.0/JSON/render.dhall.html