https://kotlinlang.org logo
#http4k
Title
# http4k
m

MrNiamh

10/11/2023, 4:40 PM
👋 Playing around with Values4k and http4k. Say I have quite a few IDs in my system, all done like this
Copy code
class NoteId(value: UUID): AbstractValue<UUID>(value)
This seems to kind of work, however now it's returning in my API as:
Copy code
"id" : {
         "value" : "00000000-0000-0000-0000-000000000000"
       }
Instead of what i'd prefer which is
Copy code
"id" : "00000000-0000-0000-0000-000000000000"
I am trying to avoid adding each ID to my
ConfigurableJackson
as there's potentially quite a few. I tried to see where the
AbstractValue
was being auto-converted but couldn't see it anywhere in the
ConfigurableJackson
. Not sure if i'm missing something obvious here.
a

Andrew O'Hara

10/11/2023, 4:48 PM
Sadly, registering them all individually in your
ConfigurableJackson
is the best way I know of.
j

James Richardson

10/11/2023, 4:49 PM
tbh - i tend to just list them - even if there are quite a few. doesn't take long. I also write a test that serializing all types that are a stringvalue that are in use don't put "value" in the json, so its all tested.
d

dave

10/11/2023, 4:51 PM
You need to register them with the value() method in the JSON config. Also - each microtype should extend the relevant ValueTypeFactory for maximum ease.
m

MrNiamh

10/11/2023, 5:11 PM
Dang that's unfortunate, well if I must - thanks
a

Andrew O'Hara

10/11/2023, 5:28 PM
Yes, if you're extending the
ValueFactory
properly, you should have something like this:
Copy code
class UserId private constructor(value: UUID): UuidValue(value) {
    companion object: UuidValueFactory<UserId>(::UserId)
}

val myJson = Jackson.custom {
    value(UserId)
}
d

dave

10/11/2023, 5:30 PM
The ValueFactory is pretty important for taking advantage of validation and masking. Also - the main constructor should be private to enforce the usage of the UserId.of() for construction
I've actually been thinking about it and wondering if there is a possibility that we could write a generic adapter to catch all of the value types. Will think a bit harder and see if it's possible...
🤔 1
a

Andrew O'Hara

10/12/2023, 1:56 PM
If it's possible, it would allow me to delete almost all my custom marshallers!
m

MrNiamh

10/12/2023, 1:57 PM
Haha i'm glad it troubles you too @dave, it felt like I was able to get something close but not quite there