JSON Handling
JNX provides the com.hcl.domino.json.JsonSerializer type to allow serializing Document objects to JSON.
Implementations
The actual implementation of this is reliant on using a secondary module for your environment. Currently, the implementing modules are:
domino-jnx-vertx-json- Uses the Vert.x JSON classes and assumes that you havevertx-corepresent in your project. ThegetObjectmethod emits a Vert.xJsonObjectinstancedomino-jnx-jsonb- Uses JSON-B classes and assumes you have the Jakarta EE 9+jakarta.json.bind-apiandjakarta.json.json-apimodules and an implementation present in your project. ThegetObjectmethod emits aString
Usage
Serialization
String jsonString = JsonSerializer.createSerializer()
.excludeItems(Arrays.asList("received"))
.lowercaseProperties(true)
.excludeTypes(Arrays.asList(ItemDataType.TYPE_COMPOSITE))
.includeMetadata(true)
.richTextConvertOption(HtmlConvertOption.XMLCompatibleHTML, "0")
.richTextConvertOption(HtmlConvertOption.DisablePassThruHTML, "1")
.customProcessor("Foo", (doc, itemName) -> "Custom Value")
.toJsonString(doc);
excludeItemsallows for a denylist of case-insensitive item names to exclude from the outputincludeItemsallows for an allowlist of items to include, instead of the default of including all itemslowercasePropertiesforces all output item names to be lower-cased. By default, the output JSON matches the item capitalization in the documentexcludeTypesallows for a denylist ofItemDataTypevalues to exlude items by typeincludeMetadataincludes a@metaproperty containing the note ID, UNID, creation date, modification date, accessed date, modified-in-file date, added-to-file date, and array of note classes.falseby defaultbooleanItemNamesspecifies which items to consider boolean values. This should be used in concert withbooleanTrueValuesbooleanTrueValuesspecifies which item values (of various types) should be consideredtruewhen found in an item named inbooleanItemNamesdateRangeFormatallows for customization of date/time ranges:ISO(the default) concatenates the two components with a/OBJECTcreates an inner JSON object withfromandtoproperties
richTextConvertOptionallows specification of “HTMLOptions” values for conversion of rich text to HTMLcustomProcessorallows specification of a method that will be called when encountering a named item instead of normal processing. TheBiFunctionprovided as the second parameter is called with the context document and current item name and should produce a JSON-compatible Java object (e.g.StringorMap<String, Object>)
Deserialization
Deserialization of JSON to a document has two modes: writing to an existing document or creating a new in-memory document in a database:
Document doc = JsonDeserializer.createDeserializer()
.target(database)
.booleanValues("Y", "N")
.dateTimeItems(Collections.singleton("Posted"))
.fromJson(json);
Document doc = fetchDocumentFromSomewhere();
JsonDeserializer.createDeserializer()
.target(doc)
.removeMissingItems(true)
.detectDateTime(true)
.customProcessor("foo", (value, propName, doc) -> { doc.replaceItemValue("Foo", "Bar"); })
.fromJson(json);
booleanValuesallows you to specify how JSON boolean literals will be stored in the document. This defaults to1and0dateTimeItemsallows you to specify which items in the JSON should be interpreted as date/time values. These items will be interpreted as ISO 8601 strings or arrays of those strings, and the deserializer will throw an exception if it encounters an unparseable valueremoveMissingItemsapplies only when targeting an existing document and allows you to have items in the document not found in the JSON removed, other than “Form” and items with names beginning with “$”detectDateTimeallows you to specify whether the deserializer will attempt to detect ISO-format string values and convert them as date/time values in the destination documentcustomProcessorallows specification of a method that will be called when encountering a named property (case-sensitive) instead of normal processing. TheCustomProcessorprovided as the second parameter is called with the JSON value, the property name, and the target document