Override JSON Serialization inside SPA Page model request
Hey folks,
I'm working on an implementation for custom Sling Model exporters that rely on a GSON-based export framework we've built. What I want to do is change the way the JSON serialization happens every time this component is serialized.
I have it working so that if I fetch the component directly:
http://localhost:4502/content/my-app/us/en/jcr:content/root/container/container/section_container/mycomponent_54231355.model.json
I get the custom JSON I am expecting. However, if I fetch the page:
http://localhost:4502/content/my-app/us/en.model.json
Then I just get the OOTB serialization for this component (which is nested in the child components for the page).
How can I override the way this component is serialized including as part of the full page JSON?
My component:
@Model(
resourceType = Breadcrumb.RESOURCE_TYPE,
adaptables = {
Resource.class,
SlingHttpServletRequest.class
}
)
@Exporter(
name = CustomSlingModelExporter.NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION
)
public class Breadcrumb extends Component {
public static final String RESOURCE_TYPE = "my-app/components/breadcrumb";
// other fields and methods
}
The exporter:
@org.osgi.service.component.annotations.Component(service = ModelExporter.class)
public class CustomSlingModelExporter implements ModelExporter {
public static final String NAME = "gson";
public <T> T export(@NotNull Object model, Class<T> clazz, Map<String, String> options) throws org.apache.sling.models.factory.ExportException {
return (T) new Gson().toJson(model);
}
public String getName() {
return NAME;
}
public boolean isSupported(@NotNull Class<?> clazz) {
return clazz.equals(String.class);
}
}
