From ec0ca6d617729754ae70f45b3096c241e8cbc67d Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Sun, 27 Mar 2022 19:48:38 +0400 Subject: [PATCH] chore: Reuse same JSON mapping instances. --- .../http/ext/spi/CompositeJsonMappingProvider.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/nstdio/http/ext/spi/CompositeJsonMappingProvider.java b/src/main/java/io/github/nstdio/http/ext/spi/CompositeJsonMappingProvider.java index deed74a..3f750f0 100644 --- a/src/main/java/io/github/nstdio/http/ext/spi/CompositeJsonMappingProvider.java +++ b/src/main/java/io/github/nstdio/http/ext/spi/CompositeJsonMappingProvider.java @@ -29,12 +29,20 @@ static boolean hasAnyImplementation() { @Override public JsonMapping get() { if (isJacksonPresent()) { - return new JacksonJsonMapping(); + return JacksonMappingHolder.INSTANCE; } if (isGsonPresent()) { - return new GsonJsonMapping(); + return GsonMappingHolder.INSTANCE; } throw new JsonMappingProviderNotFoundException(NO_JSON_MAPPING_FOUND); } + + private static class JacksonMappingHolder { + private static final JacksonJsonMapping INSTANCE = new JacksonJsonMapping(); + } + + private static class GsonMappingHolder { + private static final GsonJsonMapping INSTANCE = new GsonJsonMapping(); + } }