JsonItemWriter
class does exactly what you need:
Node node = session.getNode("/content/geometrixx/en/toolbar/contacts");
StringWriter stringWriter = new StringWriter();
JsonItemWriter jsonWriter = new JsonItemWriter(null);
jsonWriter.dump(node, stringWriter, -1, true);
String json = stringWriter.toString();
The dump()
method allows to specify recursion level (-1
for infinity) and optionally produces nicely formatted output (pass true
as the last parameter).