vertx-jooq 2.3.5 gives you control over the generated JSON

One of the goodies of vertx-jooq is that it allows you to automatically convert POJOs and jOOQ-Records from and into JSON-Objects. Until now however, the key of the generated JSON-field was fixed to the POJO’s member name that represents that column (which stays the default). Starting with version 2.3.5 this behavior can be changed in different ways:

  1. Subclass the VertxGenerator of your choice and overwrite getJsonKeyName-method.
  2. Subclass VertxGeneratorStrategy of your choice and overwrite getJsonKeyName-method.
  3. Set a different delegate used by the VertxGeneratorStrategy (see also #6) and change the way how the POJO’s member names are rendered.

Imagine you want the JSON keys to be exactly the same as their database column counterparts. Here is how you would do it in all three different methods:

Option 1:


public class ClassicAsInDBVertxGenerator extends ClassicVertxGenerator {
@Override
protected String getJsonKeyName(TypedElementDefinition<?> column) {
return column.getName();
}
}

Option 2:


public class ClassicAsInDBGeneratorStrategy extends ClassicGeneratorStrategy {
@Override
public String getJsonKeyName(TypedElementDefinition<?> column) {
return column.getName();
}
}

Option 3:


public class ClassicAsInDBVertxGenerator extends ClassicGeneratorStrategy{
public ClassicAsInDBVertxGenerator() {
super(ClassicVertxGenerator.VERTX_DAO_NAME, new MemberAsInDBGeneratorStrategy());
}
static class MemberAsInDBGeneratorStrategy extends DefaultGeneratorStrategy{
@Override
public String getJavaMemberName(Definition definition, Mode mode) {
if(mode==Mode.POJO){
return definition.getName();
}
return super.getJavaMemberName(definition, mode);
}
}
}

Whatever way you prefer, you also need to alter your code generation configuration and set the correct Generator/GeneratorStrategy. Checkout the github page for a how-to or if you’re new to vertx-jooq.

One thought on “vertx-jooq 2.3.5 gives you control over the generated JSON”

Leave a comment