Skip to content

Commit

Permalink
The upgrade to Jackson 2.16.x brought the new recycler pool feature. …
Browse files Browse the repository at this point in the history
…We should support Jackson versions that do not support this feature to avoid forcing users to move Jackson 2.16.x

Use reflection to configure the pool instead and gracefully fail when configuration cannot be achieved.
  • Loading branch information
vietj committed Jan 31, 2024
1 parent 777a1b9 commit dd6a58e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<version>3.2.5</version>
<executions>
<execution>
<id>ssl-engine:default</id>
Expand Down Expand Up @@ -571,6 +571,29 @@
</classpathDependencyExcludes>
</configuration>
</execution>
<execution>
<id>no-recycler-pool-jackson</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>io/vertx/it/NoRecyclerPoolJacksonTest.java</include>
</includes>
<additionalClasspathDependencies>
<additionalClasspathDependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.1</version>
</additionalClasspathDependency>
</additionalClasspathDependencies>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.fasterxml.jackson.core:jackson-core</classpathDependencyExclude>
<classpathDependencyExclude>com.fasterxml.jackson.core:jackson-databind</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</execution>
<execution>
<id>no-jackson-databind</id>
<goals>
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/io/vertx/core/json/jackson/JacksonCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@

package io.vertx.core.json.jackson;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
Expand All @@ -35,6 +31,7 @@
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
Expand All @@ -54,11 +51,31 @@
*/
public class JacksonCodec implements JsonCodec {

private static final JsonFactory factory = JsonFactory.builder().recyclerPool(HybridJacksonPool.getInstance()).build();
private static JsonFactory buildFactory() {
TSFBuilder<?, ?> builder = JsonFactory.builder();
try {
// Use reflection to configure the recycler pool
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);
break;
}
}
} catch (Throwable e) {
// Ignore: most likely no Recycler Pool with Jackson < 2.16
}
return builder.build();
}

private static final JsonFactory factory = buildFactory();

static {
// Non-standard JSON but we allow C style comments in our JSON
factory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
JacksonCodec.factory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
}

@Override
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/io/vertx/it/NoRecyclerPoolJacksonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.it;

import com.fasterxml.jackson.core.Version;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonObject;
import io.vertx.test.core.VertxTestBase;
import org.junit.Test;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class NoRecyclerPoolJacksonTest extends VertxTestBase {

@Test
public void testJsonObject() {

Version version = com.fasterxml.jackson.core.json.PackageVersion.VERSION;
assertEquals("2.15.1", version.toString());

JsonObject obj = new JsonObject("{\"foo\":\"bar\"}");
assertEquals("bar", obj.getString("foo"));
assertEquals("{\"foo\":\"bar\"}", obj.toString());
try {
obj.mapTo(Object.class);
fail();
} catch (DecodeException ignore) {
// Expected
}
}
}

0 comments on commit dd6a58e

Please sign in to comment.