Skip to content

Commit

Permalink
Improve JsonFactory creation and propagate its usage to DatabindCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco authored and vietj committed Feb 8, 2024
1 parent 1b8a5bd commit 4bdf223
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class DatabindCodec extends JacksonCodec {

private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = new ObjectMapper(JacksonCodec.factory);

static {
initialize();
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/io/vertx/core/json/jackson/JacksonCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ private static JsonFactory buildFactory() {
Method[] methods = builder.getClass().getMethods();
for (Method method : methods) {
if (method.getName().equals("recyclerPool")) {
Class<?> poolClass = JacksonCodec.class.getClassLoader().loadClass("io.vertx.core.json.jackson.HybridJacksonPool");
Method getInstanceMethod = poolClass.getMethod("getInstance");
Object pool = getInstanceMethod.invoke(null);
method.invoke(builder, pool);
method.invoke(builder, JacksonPoolHolder.pool);
break;
}
}
Expand All @@ -71,7 +68,12 @@ private static JsonFactory buildFactory() {
return builder.build();
}

private static final JsonFactory factory = buildFactory();
private static final class JacksonPoolHolder {
// Use Initialization-on-demand holder idiom to lazy load the HybridJacksonPool only when we know that we are on Jackson 2.16+
private static final Object pool = HybridJacksonPool.getInstance();
}

static final JsonFactory factory = buildFactory();

static {
// Non-standard JSON but we allow C style comments in our JSON
Expand Down

0 comments on commit 4bdf223

Please sign in to comment.