Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

JSON processing utilities

Christian Kreutzfeldt edited this page Apr 26, 2016 · 5 revisions

Description

The JSON processing utilities provide a set of features for interacting with JSON documents, eg. reading or modifying content.

The class is widely used throughout the operator library and uses the Apache Sling project as well to represent JSON documents.

Usage

To apply the utilities inside an implementation it is sufficient to create an instance and use it accordingly.

JsonProcessingUtils utils = new JsonProcessingUtils();
utils.getTextFieldValue(jsonObject, new String[]{"path","to","content"});

Methods

getTextFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as string.

in/out/ex Type Description
in JSONObject JSON object to read text content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out String Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location

getZonedDateTimeFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as ZonedDateTime.

in/out/ex Type Description
in JSONObject JSON object to read content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out ZonedDateTime Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location
ex ParseException Thrown if parsing a zoned time date from a text field fails

getDateTimeFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as Date.

in/out/ex Type Description
in JSONObject JSON object to read content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out Date Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location
ex ParseException Thrown if parsing a date from a text field fails

getIntegerFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as integer.

in/out/ex Type Description
in JSONObject JSON object to read text content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out Integer Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location
ex NumberFormatException Thrown if parsing an integer from a text field fails

getDoubleFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as double.

in/out/ex Type Description
in JSONObject JSON object to read text content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out Double Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location
ex NumberFormatException Thrown if parsing an integer from a text field fails

getBooleanFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as boolean.

in/out/ex Type Description
in JSONObject JSON object to read text content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out Boolean Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location
ex ParseException Thrown if parsing a boolean from a text field fails

getFieldValue

Extracts the value of a referenced field from a provided JSON object. The returned value is represented as Object.

in/out/ex Type Description
in JSONObject JSON object to read text content from
in String[] Path pointing into the JSON object referencing the element to read content from
in boolean Indicator if field is required to exist
out Object Value of referenced field represented as text
ex JSONException Thrown if anything fails while accessing the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing
ex NoSuchElementException Thrown if the path does not point to an existing location

insertField

Inserts a value of arbitrary type (atomic or sub-structure provided as JSON object) at a given location into the existing JSON object

in/out/ex Type Description
in JSONObject JSON object to modify
in String[] Path pointing into the JSON object referencing the element to write content to
in Object Value to insert
in boolean Override existing content
out JSONObject Modified JSON object
ex JSONException Thrown if anything fails while accessing or modifying the JSON object
ex IllegalArgumentException Thrown if a required parameter value is missing