Hi all,
I'm struggling with exporting JSON from a component with a Parsys.
Basically I have this component (a sort of container) without fields but only with a parsys.
This parsys could only accept one type of child component.
Now i'm interested to export a JSON of this "father" component using a SlingModelExporter.
I've already made the exporter for child component and it's fine. Can you show me some example?
Solved! Go to Solution.
Views
Replies
Total Likes
The Sling Models Exporter uses the Jackson library for serializing data, or in other words, creates a JSON object from a Java object (and vice versa), in the case of exporter for sling models, it will rely on specifically getter methods. As long as you have getters, the information will be populated in the final JSON output.
That being said, you just need to do something like below. Keep in mind that MyChildrenModel needs its own getters
public MyChildrenModel getMyChildrenComponentModel() {
//return the resource(the component below your parsys) adapted to your children model
}
The Sling Models Exporter uses the Jackson library for serializing data, or in other words, creates a JSON object from a Java object (and vice versa), in the case of exporter for sling models, it will rely on specifically getter methods. As long as you have getters, the information will be populated in the final JSON output.
That being said, you just need to do something like below. Keep in mind that MyChildrenModel needs its own getters
public MyChildrenModel getMyChildrenComponentModel() {
//return the resource(the component below your parsys) adapted to your children model
}
Hi @Giuderos1 -
In your Container Component (the component with the parsys), configure the Parsys with the granite:class property set to wcm/foundation/components/parsys/json. This configuration instructs AEM to use the JSON Exporter for the Parsys.
Here's an example of how you can use the AEM's OOTB JSON Exporter for your case:
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = Resource.class,
resourceType = "your-parent/component/resourceType"
)
@Exporter(
name = "jackson",
extensions = "json",
selector = "core-components",
options = {
@ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "true")
}
)
public class ParentComponentModel {
@Inject
private SlingHttpServletRequest request;
@ValueMapValue
private String property1;
@ValueMapValue
private String property2;
// Other fields and methods
}
Hi @Giuderos1 , Once you add the child component inside parent component we can access it via @ChildResource in parent slingmodel which has exporter configured to it.
@Model(adaptables = { Resource.class,
SlingHttpServletRequest.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = ParentContainerModel.resourceType)
@Exporter(name = "jackson", extensions = "json")
public class ParentContainerModel {
private static final Logger log = LoggerFactory.getLogger(SecondaryNavModel.class);
public static final String resourceType = "your_parent_container_resourcetype";
@ChildResource
private ThatAllowedComponentModel thatAllowedComponentModel;
@PostConstruct
protected void init() {
log.debug("Inside Post Construct of ParentContainer Model..");
}
public ThatAllowedComponentModel getThatAllowedComponentModel() {
return thatAllowedComponentModel;
}
}
Now you will get child components data too in json format when you hit parent component.
Thanks,
Krishna
I think this won't work because the ChildResource will give you the direct children of the "Resource", on @Giuderos1 case, he has the component within a parsys.
Got it Thanks for correcting me @EstebanBustamante. In @Giuderos1 case child component resource needs to be adapted to its respective slingmodel and have a getter method for the same in this parent model so that complete json object is returned
Views
Likes
Replies
Views
Likes
Replies