Home > front end >  Is it possible to map jooq Record from string
Is it possible to map jooq Record from string

Time:02-19

I have a case where I need to map jooq Record from json to class instance. We have used our own mapper for this in the past, but underlying table had only basic types so it was working fine. Now we have a need to map Postgres interval type to jooq YearToSecond and it is not working anymore.

I was wondering if there is a way to create these via jooq api or at least somehow get to the underlying mapper that jooq uses (I assume jooq has some kind of a mapper underneath).

Example: {"my_text":"This is text","my_interval_type":"05:00:00"} to deserialize to MyRecord class which will have String myText and YearToSecond myIntervalType fields

CodePudding user response:

The basic way to map JSON strings to jOOQ Result values is by using DSLContext.fetchFromJSON(String).

Unfortunately, as of jOOQ 3.16, you cannot yet provide data type information to this mechanism, so everything will be a String, there's no implied YearToSecond support. There's a pending feature request https://github.com/jOOQ/jOOQ/issues/12012, which should ship soon as it is indeed very useful.

That feature request is merely a matter of opening up jOOQ's internals, which you already correctly assumed, existed. The relevant class in jOOQ 3.16 is org.jooq.impl.JSONReader, which could do the trick for you as a workaround, if you're willing to access jOOQ's internals via reflection?

  • Related