Apache Sling Models 1.3.0 introduces Sling Model Exporter, an elegant way to export or serialize Sling Model objects into custom abstractions. This article juxtaposes the traditional use-case of using Sling Models to populate HTL scripts, with leveraging the Sling Model Exporter framework to serialize a Sling Model into JSON.
Traditional Sling Model HTTP Request flow
The traditional use-case for Sling Models is to provide a business abstraction for a resource or request, which provides HTL scripts (or, previously JSPs) an interface for accessing business functions.
Common patterns are developing Sling Models that represent AEM Components or Pages, and using the Sling Model objects to feed the HTL scripts with data, with an end result of HTML that's displayed in the browser.
Sling Model Exporter HTTP Request flow
Apache Sling Model Exporter comes with a Sling provided Jackson Exporter that automatically serializes an "ordinary" Sling Model object into JSON. The Jackson Exporter, while quite configurable, at its core inspects the Sling Model object, and generates JSON using any "getter" methods as JSON keys, and the getter return values as the JSON values.