Skip to content

Commit fae54c2

Browse files
refactor: extract tryCreate helper to reduce repetition in create()
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c4d79b3 commit fae54c2

1 file changed

Lines changed: 21 additions & 36 deletions

File tree

core-api/src/main/java/com/optimizely/ab/event/internal/serializer/DefaultJsonSerializer.java

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.optimizely.ab.config.parser.MissingJsonParserException;
2323

2424
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
26+
import java.util.concurrent.Callable;
2527

2628
/**
2729
* Factory for generating {@link Serializer} instances, based on the json library available on the classpath.
@@ -47,47 +49,30 @@ public static Serializer getInstance() {
4749
*/
4850
private static @Nonnull
4951
Serializer create() {
50-
if (isPresent("com.fasterxml.jackson.databind.ObjectMapper")) {
51-
try {
52-
Serializer serializer = new JacksonSerializer();
53-
logger.debug("using json serializer: {}", serializer.getClass().getSimpleName());
54-
return serializer;
55-
} catch (Throwable t) {
56-
logger.warn("Jackson found on classpath but serializer initialization failed, trying next option.", t);
57-
}
52+
Serializer serializer;
53+
if ((serializer = tryCreate("com.fasterxml.jackson.databind.ObjectMapper", JacksonSerializer::new)) != null ||
54+
(serializer = tryCreate("com.google.gson.Gson", GsonSerializer::new)) != null ||
55+
(serializer = tryCreate("org.json.simple.JSONObject", JsonSimpleSerializer::new)) != null ||
56+
(serializer = tryCreate("org.json.JSONObject", JsonSerializer::new)) != null) {
57+
logger.debug("using json serializer: {}", serializer.getClass().getSimpleName());
58+
return serializer;
5859
}
59-
if (isPresent("com.google.gson.Gson")) {
60-
try {
61-
Serializer serializer = new GsonSerializer();
62-
logger.debug("using json serializer: {}", serializer.getClass().getSimpleName());
63-
return serializer;
64-
} catch (Throwable t) {
65-
logger.warn("Gson found on classpath but serializer initialization failed, trying next option.", t);
66-
}
67-
}
68-
if (isPresent("org.json.simple.JSONObject")) {
69-
try {
70-
Serializer serializer = new JsonSimpleSerializer();
71-
logger.debug("using json serializer: {}", serializer.getClass().getSimpleName());
72-
return serializer;
73-
} catch (Throwable t) {
74-
logger.warn("json-simple found on classpath but serializer initialization failed, trying next option.", t);
75-
}
76-
}
77-
if (isPresent("org.json.JSONObject")) {
78-
try {
79-
Serializer serializer = new JsonSerializer();
80-
logger.debug("using json serializer: {}", serializer.getClass().getSimpleName());
81-
return serializer;
82-
} catch (Throwable t) {
83-
logger.warn("org.json found on classpath but serializer initialization failed.", t);
84-
}
85-
}
86-
8760
throw new MissingJsonParserException("unable to locate a JSON parser. "
8861
+ "Please see <link> for more information");
8962
}
9063

64+
private static @Nullable Serializer tryCreate(String className, Callable<Serializer> factory) {
65+
if (!isPresent(className)) {
66+
return null;
67+
}
68+
try {
69+
return factory.call();
70+
} catch (Throwable t) {
71+
logger.warn("{} found on classpath but serializer init failed, trying next option.", className, t);
72+
return null;
73+
}
74+
}
75+
9176
private static boolean isPresent(@Nonnull String className) {
9277
try {
9378
Class.forName(className);

0 commit comments

Comments
 (0)