Pretty-Printing JSON in the (Desktop) Notes Client and Domino
Fri Jul 26 10:30:35 EDT 2024
In the OpenNTF Discord (join if you haven't!), Daniel Nashed brought up a task he was facing: in the Notes client, writing pretty-printed JSON. LotusScript has its NotesJSON* classes that can process JSON in their stark way, but the stringify output is meant for machine reading and doesn't include whitespace and line breaks, making it ill-suited for things like configuration files or other things a human might read or edit.
Since the goal is to get it working in the full Notes client and not Nomad, Java is on the table, but Java - for dumb historical reasons - has no proper built-in JSON library. However, as of 12.something HCL shunted IBM Commons down to the global classpath in order to support the "share Java design elements between XPages and agents" feature. Among many other things, IBM Commons includes a JSON library that can suit.
I wrote a post almost a decade ago talking about this library and its limited nature, but it's nonetheless less limited than the LotusScript classes, and it's up to the task. There are a couple ways to go about this, depending on your needs, but for now I'll just cover the basic case here of "I have a string of JSON and want to format it".
To do this, you can make a Script Library of the Java type named, say, "JsonPrettyPrint" and make a new class in the "com.example" package and named "JsonPrettyPrint" with this contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.example; import java.io.IOException; import com.ibm.commons.util.io.json.JsonException; import com.ibm.commons.util.io.json.JsonGenerator; import com.ibm.commons.util.io.json.JsonJavaFactory; import com.ibm.commons.util.io.json.JsonParser; public class JsonPrettyPrint { public String prettyPrint(String json) throws JsonException, IOException { Object jsonObj = JsonParser.fromJson(JsonJavaFactory.instanceEx, json); return JsonGenerator.toJson(JsonJavaFactory.instanceEx, jsonObj, false); } } |
Then, you can instantiate this object with LS2J and pass it a JSON string, such as one from NotesJSONNavigator:
1 2 3 4 5 6 7 8 | UseLSX "*javacon" Use "JsonPrettyPrint" Sub Initialize Dim jsession As New JavaSession, jsonPrinter As JavaObject Set jsonPrinter = jsession.GetClass("com.example.JsonPrettyPrint").CreateObject() MsgBox jsonPrinter.prettyPrint(|{"foo":{"bar":"baz"}}|) End Sub |
That'll get you something presentable:
While the stringify-parse-stringify process you'd do if you generated your JSON with the NotesJSON* classes is inefficient, it's not too bad, especially for the size of content you're likely to want to emit here. You could alternatively do more work with JsonJavaObject
and friends from IBM Commons directly to save a little overhead, but this path is a good way to do the vast majority of work in "normal" LotusScript and then only dip in to Java for the last step.
As mentioned at the start, the presence of Java means this won't work for Nomad, unfortunately. There may be a way to wrangle your way to this result using the primordial JavaScript runtime present in that, but that may not be worth the trouble unless you really need it. Better would be to vote for the Aha idea to add pretty printing to LS.
Patrick Kwinten - Wed Aug 14 04:46:34 EDT 2024
voted. however I could not get the example to work. it keeps complaining that the specified class could not be loaded/found