Expand my Community achievements bar.

SOLVED

SlingModelExporter with Parsys

Avatar

Level 2

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?

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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
}

 



Esteban Bustamante

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

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
}

 



Esteban Bustamante

Avatar

Community Advisor

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

}

 

 

Avatar

Community Advisor

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

Avatar

Community Advisor

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.



Esteban Bustamante

Avatar

Community Advisor

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