question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide built-in support for serializing a jOOQ Record to json

See original GitHub issue

Expected behavior

Consider a query returning a jOOQ Record (e.g., CustomerRecord) and the following Spring Boot REST Controller:

@GetMapping("/customer")
public CustomerRecord findAnyCustomer() {

    return {some_CustomerRecord};
}

Expecting that the default Spring Boot (Jackson based) serializer will output a JSON containing the CustomerRecord data.

Actual behavior

Initially, it throws an exception: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.jooq.TableOptions and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: jooq.generated.tables.records.CustomerRecord[“table”]->jooq.generated.tables.Customer[“options”])

After setting FAIL_ON_EMPTY_BEANS in application.properties I got a relatively big JSON that looks like this fragment: {“table”:{“name”:“customer”,“comment”:“”,“options”:{},“identity”:{“table”:{“name”:“customer”,“comment”:“”,“options”:{},“identity”:{“table”:{“name”:“customer”,“comment”:" …

Versions

  • Spring Boot: 2.3.6.RELEASE
  • jOOQ: 3.14.4
  • Java: 14 (Spring Boot 2.3.6)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
lukasedercommented, May 14, 2021

Well, a quick draft of letting Record <: Map<String, Object> (full comments to be seen here: https://github.com/jOOQ/jOOQ/issues/11889#issuecomment-841134429) yields very promising results:

var r2 = DSL.using(connection)
    .select(
        T_BOOK.ID,
        T_BOOK.fkTBookAuthorId().ID,
        row(T_BOOK.fkTBookAuthorId().FIRST_NAME, T_BOOK.fkTBookAuthorId().LAST_NAME).as("author"),
        array(select(row(T_BOOK_TO_BOOK_STORE.tBookStore().NAME, T_BOOK_TO_BOOK_STORE.STOCK))
            .from(T_BOOK_TO_BOOK_STORE)
            .where(T_BOOK_TO_BOOK_STORE.BOOK_ID.eq(T_BOOK.ID))).as("stock")
    )
    .from(T_BOOK)
    .fetch();
System.out.println(new ObjectMapper().writeValueAsString(r2));
System.out.println(new Gson().toJson(r2));

Producing this jOOQ result structure:

+----+----+----------------+--------------------------------------------------+
|  id|  id|author          |stock                                             |
+----+----+----------------+--------------------------------------------------+
|   2|   1|(George, Orwell)|[(Orell Füssli, 10)]                              |
|   1|   1|(George, Orwell)|[(Orell Füssli, 10), (Ex Libris, 1)]              |
|   4|   2|(Paulo, Coelho) |[]                                                |
|   3|   2|(Paulo, Coelho) |[(Orell Füssli, 10), (Ex Libris, 2), (Buchhandl...|
+----+----+----------------+--------------------------------------------------+

And then getting auto serialised by both Jackson and Gson to something like this:

[
   {
      "id":2,
      "id":1,
      "author":{
         "first_name":"George",
         "last_name":"Orwell"
      },
      "stock":[
         {
            "v0":"Orell Füssli",
            "v1":"10"
         }
      ]
   },
   {
      "id":1,
      "author":{
         "first_name":"George",
         "last_name":"Orwell"
      },
      "stock":[
         {
            "v0":"Orell Füssli",
            "v1":"10"
         },
         {
            "v0":"Ex Libris",
            "v1":"1"
         }
      ]
   },
   {
      "id":4,
      "id":2,
      "author":{
         "first_name":"Paulo",
         "last_name":"Coelho"
      },
      "stock":[
         
      ]
   },
   {
      "id":3,
      "id":2,
      "author":{
         "first_name":"Paulo",
         "last_name":"Coelho"
      },
      "stock":[
         {
            "v0":"Orell Füssli",
            "v1":"10"
         },
         {
            "v0":"Ex Libris",
            "v1":"2"
         },
         {
            "v0":"Buchhandlung im Volkshaus",
            "v1":"1"
         }
      ]
   }
]

We’re onto something. I think that this is exactly what everyone wants from jOOQ, and Spring Boot doesn’t need to change a thing (I think?) 😀

0reactions
lukasedercommented, May 14, 2021

Sure, greatly appreciated. I still wonder why the perception is different. Habits? People have requested a Record representation (e.g. formatting) like this:

ID: 1
FIRST_NAME: John
LAST_NAME: Doe

Kinda like in the old days with Informix, etc.

With that representation, the Iterable<Entry<String, Object>> type seems more obvious, and all the Map goodies, like Map.forEach(BiConsumer<? super String, ? super Object>) do, too…

Anyway. This issue here is about Spring Boot, and I agree that Spring Boot doesn’t need to take action. jOOQ might: https://github.com/jOOQ/jOOQ/issues/11889, and users already can, via intoMap()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exporting JSON - jOOQ
Fetch books and format them as JSON String json = create.selectFrom(BOOK).fetch().formatJSON();. The above query will result in a JSON document looking like ...
Read more >
Custom data type conversion - jOOQ
Custom data type conversion · A Converter for GregorianCalendar · Enum Converters · Using Converters in generated source code · Community · Support...
Read more >
DataType (jOOQ 3.17.5 API)
A common interface to all dialect-specific data types. jOOQ provides built in data types through SQLDataType . Users can define their own data...
Read more >
Serialized Form (jOOQ 3.17.5 API)
Class org.jooq.JSON. class JSON extends Object implements Serializable ... class BuiltInDataType extends DefaultDataType<T> implements Serializable ...
Read more >
jooq deserialize a polymorphic class - Stack Overflow
I'm assuming you're trying to use the built-in ConverterProvider logic that makes use of Jackson, e.g. when writing things like:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found