AEM resources such as JCR nodes can be easily converted into JSON format using com.day.cq.commons.TidyJsonItemWriter class which extends org.apache.sling.commons.json.jcr.JsonItemWriter class.
The dump() method of TidyJsonItemWriter class dumps the given node in JSON, optionally recursing into its child nodes and allows us to specify recursion level (-1 for infinity) and produces JSON in a nicely formatted way if the tidy parameter is passed as true.
Method details:
public void dump(javax.jcr.Node node,
Writer w,
int maxRecursionLevels,
boolean tidy)
throws javax.jcr.RepositoryException,
JSONException
Writer w,
int maxRecursionLevels,
boolean tidy)
throws javax.jcr.RepositoryException,
JSONException
Parameters:
maxRecursionLevels: -1 for infinity recursion
tidy: if true the json dump is nicely formatted
tidy: if true the json dump is nicely formatted
Throws:
javax.jcr.RepositoryException
JSONException
javax.jcr.RepositoryException
JSONException
Sample code:
Node node = session.getNode(“/apps/geometrixx-media/components/advancedtext”);
StringWriter stringWriter = new StringWriter();
JsonItemWriter jsonWriter = new JsonItemWriter(null);
jsonWriter.dump(node, stringWriter, -1, true);
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); //disableHtmlEscaping in JSON
return gson.toJson(stringWriter.toString());
StringWriter stringWriter = new StringWriter();
JsonItemWriter jsonWriter = new JsonItemWriter(null);
jsonWriter.dump(node, stringWriter, -1, true);
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); //disableHtmlEscaping in JSON
return gson.toJson(stringWriter.toString());
Sample output:
{
"jcr:primaryType": "cq:Component",
"jcr:createdBy": "admin",
"jcr:title": "Advanced Text",
"jcr:created": "Thu May 17 2018 16:45:33 GMT+0530",
"sling:resourceSuperType": "foundation/components/text",
"componentGroup": "Adobe",
"advancedtext.jsp": {
"jcr:primaryType": "nt:file",
"jcr:createdBy": "admin",
"jcr:created": "Thu May 17 2018 16:45:33 GMT+0530",
"jcr:content": {
"jcr:primaryType": "nt:resource",
"jcr:lastModifiedBy": "admin",
"jcr:mimeType": "text/plain",
"jcr:lastModified": "Thu May 17 2018 16:45:33 GMT+0530",
":jcr:data": 127,
"jcr:uuid": "8d249dd9-b903-4fc8-abc9-9a76406f4f62"
}
},
"cq:editConfig": {
"jcr:primaryType": "cq:EditConfig",
"jcr:createdBy": "admin",
"jcr:created": "Thu May 17 2018 16:45:33 GMT+0530",
"cq:dialogMode": "floating",
"cq:listeners": {
"jcr:primaryType": "cq:EditListenersConfig",
"afteredit": "REFRESH_PAGE"
}
},
"dialog": {
"jcr:primaryType": "cq:Dialog",
"modal": true,
"title": "Advanced Text",
"width": "1030",
"xtype": "dialog",
"items": {
"jcr:primaryType": "cq:Widget",
"xtype": "tabpanel",
"items": {
"jcr:primaryType": "cq:WidgetCollection",
"text": {
"jcr:primaryType": "cq:Widget",
"anchor": "100%",
"title": "Text",
"xtype": "panel",
"items": {
"jcr:primaryType": "cq:WidgetCollection",
"text": {
"jcr:primaryType": "cq:Widget",
"name": "./text",
"xtype": "richtext",
"hideLabel": true,
"rtePlugins": {
"jcr:primaryType": "nt:unstructured",
"spellcheck": {
"jcr:primaryType": "nt:unstructured",
"features": "*"
},
"links": {
"jcr:primaryType": "nt:unstructured",
"features": "*"
},
"subsuperscript": {
"jcr:primaryType": "nt:unstructured",
"features": "*"
},
"misctools": {
"jcr:primaryType": "nt:unstructured",
"features": [
"specialchars",
"sourceedit"
]
},
"undo": {
"jcr:primaryType": "nt:unstructured",
"features": "*"
},
"findreplace": {
"jcr:primaryType": "nt:unstructured",
"features": "*"
},
"edit": {
"jcr:primaryType": "nt:unstructured",
"features": [
"cut",
"copy",
"paste-plaintext"
],
"defaultPasteMode": "plaintext"
}
}
},
"isRichTextFlag": {
"jcr:primaryType": "cq:Widget",
"name": "./textIsRich",
"value": "true",
"xtype": "hidden",
"ignoreData": true
}
}
}
}
}
}
}
No comments:
Post a Comment
If you have any doubts or questions, please let us know.