diff --git a/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/datalayer/ComponentDataImpl.java b/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/datalayer/ComponentDataImpl.java index 5a1ee4eece..fc2051aa35 100644 --- a/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/datalayer/ComponentDataImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/datalayer/ComponentDataImpl.java @@ -15,10 +15,13 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ package com.adobe.cq.wcm.core.components.internal.models.v1.datalayer; -import com.adobe.cq.wcm.core.components.models.datalayer.builder.DataLayerSupplier; +import com.adobe.cq.wcm.core.components.internal.jackson.ComponentDataModelSerializer; import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData; +import com.adobe.cq.wcm.core.components.models.datalayer.builder.DataLayerSupplier; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.module.SimpleModule; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -37,6 +40,13 @@ public class ComponentDataImpl implements ComponentData { */ private static final Logger LOGGER = LoggerFactory.getLogger(ComponentDataImpl.class); + /** + * The {@link ObjectWriter} used for JSON serialization. We can safely re-use the instance since it's thread-safe. + */ + private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().registerModule( + new SimpleModule().addSerializer(ComponentData.class, new ComponentDataModelSerializer()) + ).writer(); + /** * The current data layer supplier. */ @@ -194,7 +204,8 @@ public final String getJson() { try { return String.format("{\"%s\":%s}", this.getId(), - new ObjectMapper().writeValueAsString(this)); + OBJECT_WRITER + .writeValueAsString(this)); } catch (JsonProcessingException e) { LOGGER.error("Unable to generate dataLayer JSON string", e); } diff --git a/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/models/Component.java b/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/models/Component.java index a04bdca079..7eb4bf03bb 100644 --- a/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/models/Component.java +++ b/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/models/Component.java @@ -15,15 +15,12 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ package com.adobe.cq.wcm.core.components.models; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.osgi.annotation.versioning.ConsumerType; - import com.adobe.cq.export.json.ComponentExporter; -import com.adobe.cq.wcm.core.components.internal.jackson.ComponentDataModelSerializer; import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.annotation.versioning.ConsumerType; /** * A base interface to be extended by components that need to provide access to common properties. @@ -55,12 +52,10 @@ default String getId() { * Returns the data layer information associated with the component * * @return {@link ComponentData} object associated with the component - * * @since com.adobe.cq.wcm.core.components.models 12.12.0 */ @Nullable @JsonProperty("dataLayer") - @JsonSerialize(using = ComponentDataModelSerializer.class) default ComponentData getData() { return null; }