Class JsonUtilities
public final class JsonUtilities
Counterpart to XmlUtilities for working with JSON resources using the
Jakarta JSON Binding (JSON-B) and
Jakarta JSON Processing (JSON-P) APIs.
The class provides two layers of access:
- A binding layer ([URL)][#read(Class,], [Path)][#read(Class,], [Path)][#save(Object,])
that maps JSON documents to plain Java objects via JSON-B, mirroring the API offered by
XmlUtilitiesfor JAXB. - A tree-model layer (
readTree(URL),readTree(Path), [Path)][#saveTree(JsonValue,]) that exposes the underlyingJsonStructurefor cases where direct DOM-style access is preferable to data binding.
Both layers cache their factories and Jsonb instances per configuration so repeated use
does not pay the cost of recreating them.
-
Method Summary
Modifier and TypeMethodDescriptionstatic jakarta.json.bind.JsonbgetJsonb(boolean pretty) Returns a cachedJsonbinstance configured with the requested formatting.static <T> TReads a JSON document from the given reader and binds it to an instance of the requested class.static <T> TReads a JSON document from the given URL and binds it to an instance of the requested class.static <T> TReads a JSON document from the given path and binds it to an instance of the requested class.static jakarta.json.JsonStructureReads the JSON document from the given reader into aJsonStructure.static jakarta.json.JsonStructureReads the JSON document referenced by the given URL into aJsonStructure(object or array).static jakarta.json.JsonStructureReads the JSON document at the given path into aJsonStructure(object or array).static voidBinds the given object to JSON and writes it to the supplied writer.static PathBinds the given object to JSON and writes it to the specified path (pretty-printed).static PathBinds the given object to JSON and writes it to the specified path, ensuring the path ends with the supplied extension (e.g."json"or".json").static PathConvenience overload that pretty-prints by default.static PathWrites aJsonValuetree to the specified path.static StringwriteTreeToString(jakarta.json.JsonValue value, boolean pretty) Serializes the given JSON value to a string.
-
Method Details
-
getJsonb
public static jakarta.json.bind.Jsonb getJsonb(boolean pretty) Returns a cachedJsonbinstance configured with the requested formatting.- Parameters:
pretty- Whether the returned instance produces pretty-printed output.- Returns:
- A shared, thread-safe
Jsonbinstance.
-
read
Reads a JSON document from the given URL and binds it to an instance of the requested class.- Type Parameters:
T- The bound type.- Parameters:
cls- The target class.path- The URL of the JSON document.- Returns:
- The bound instance, or
nullif the document cannot be read.
-
read
Reads a JSON document from the given path and binds it to an instance of the requested class.- Type Parameters:
T- The bound type.- Parameters:
cls- The target class.filePath- The path to the JSON document.- Returns:
- The bound instance, or
nullif the document cannot be read.
-
read
-
readTree
Reads the JSON document referenced by the given URL into aJsonStructure(object or array). Useful when the document does not follow a fixed schema.- Parameters:
path- The URL of the JSON document.- Returns:
- The parsed structure, or
nullif the document cannot be read.
-
readTree
Reads the JSON document at the given path into aJsonStructure(object or array).- Parameters:
filePath- The path to the JSON document.- Returns:
- The parsed structure, or
nullif the document cannot be read.
-
readTree
Reads the JSON document from the given reader into aJsonStructure.- Parameters:
reader- The reader providing the JSON content.- Returns:
- The parsed structure.
-
save
-
save
Binds the given object to JSON and writes it to the specified path, ensuring the path ends with the supplied extension (e.g."json"or".json").- Parameters:
object- The object to serialize.path- The destination path.extension- The file extension (with or without a leading dot).- Returns:
- The actual destination path used.
-
save
-
saveTree
Writes aJsonValuetree to the specified path.- Parameters:
value- The JSON value to write.filePath- The destination path.pretty- Whether to pretty-print the output.- Returns:
- The destination path, or
nullif writing fails.
-
saveTree
-
writeTreeToString
Serializes the given JSON value to a string.- Parameters:
value- The JSON value to serialize.pretty- Whether to pretty-print the output.- Returns:
- The serialized JSON string.
-