Skip to content

Commit

Permalink
perf: Use and re-use ObjectWriter for data serialization (fixes adobe…
Browse files Browse the repository at this point in the history
…#1381)

Also moves the configuration of ComponentData's serializer inside ComponentDataImpl
  • Loading branch information
dhardtke committed Feb 6, 2021
1 parent 2bd4fcf commit 77bebb2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 77bebb2

Please sign in to comment.