Enable JSON Export for a component enabling-json-export-for-a-component

Components can be adapted to generate JSON export of their content based on a modeler framework.

Overview overview

The JSON export is based on Sling Models, and on the Sling Model Exporter framework (which itself relies on Jackson annotations).

This approach means that the component must have a Sling Model if it must export JSON. Therefore, follow these two steps to enable JSON export on any component.

Define a Sling Model for the component define-a-sling-model-for-the-component

First, a Sling Model must be defined for the component.

NOTE
For an example of using Sling Models, see Developing Sling Model Exporters in AEM.

The Sling Model implementation class must be annotated with the following:

@Model(... adapters = {..., ComponentExporter.class})
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
@JsonSerialize(as = MyComponent.class)

Doing so ensures that your component could be exported on its own, using the .model selector and the .json extension.

In addition, it specifies that the Sling Model class can be adapted into the ComponentExporter interface.

NOTE
Jackson annotations are not specified at the Sling Model class level, but rather at the Model interface level. This approach is to ensure that the JSON export is considered as part of the component API.
NOTE
The ExporterConstants and ComponentExporter classes come from the com.adobe.cq.export.json bundle.

Use multiple selectors multiple-selectors

Although not a standard use case, it is possible to configure multiple selectors in addition to the model selector.

https://<server>:<port>/content/page.model.selector1.selector2.json

However, in such a case the model selector must be the first selector and the extension must be .json.

Annotate the Sling Model interface annotate-the-sling-model-interface

For the JSON Exporter framework to process it, the Model interface must implement the ComponentExporter interface (or ContainerExporter for a container component).

The corresponding Sling Model interface ( MyComponent) would be then annotated using Jackson annotations to define how it should be exported (serialized).

The Model interface must be properly annotated to define which methods are serialized. By default, all methods that respect the usual naming convention for getters are serialized and derive their JSON property names naturally from the getter names. This approach can be prevented or overridden using @JsonIgnore or @JsonProperty to rename the JSON property.

Example example

The Core Components have supported JSON export since release 1.1.0 of the core components and can be used as a reference.

For an example, see the Sling Model implementation of the Image Core Component and its annotated interface.

CODE ON GITHUB

You can find the code of this page on GitHub

recommendation-more-help
51c6a92d-a39d-46d7-8e3e-2db9a31c06a2