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 9, 2024
1 parent 2283ff4 commit 1f9df5a
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);
private static final ObjectMapper prettyMapper = new ObjectMapper();

static {
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 @@ -57,10 +57,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 @@ -70,7 +67,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 1f9df5a

Please sign in to comment.