Package jakarta.json
Class Json
java.lang.Object
jakarta.json.Json
Factory class for creating JSON processing objects.
This class provides the most commonly used methods for creating these
objects and their corresponding factories. The factory classes provide
all the various ways to create these objects.
The methods in this class locate a provider instance using the method
JsonProvider.provider(). This class uses the provider instance
to create JSON processing objects.
The following example shows how to create a JSON parser to parse an empty array:
StringReader reader = new StringReader("[]");
JsonParser parser = Json.createParser(reader);
All the methods in this class are safe for use by multiple concurrent threads.
-
Method Summary
Modifier and TypeMethodDescriptionstatic JsonArrayBuilderCreates a JSON array builderstatic JsonArrayBuildercreateArrayBuilder(JsonArray array) Creates a JSON array builder, initialized with the specified arraystatic JsonArrayBuildercreateArrayBuilder(Collection<?> collection) Creates a JSON array builder, initialized with the content of specifiedcollection.static JsonBuilderFactorycreateBuilderFactory(Map<String, ?> config) Creates a builder factory for creatingJsonArrayBuilderandJsonObjectBuilderobjects.static JsonPatchcreateDiff(JsonStructure source, JsonStructure target) Generates a JSON Patch (RFC 6902) from the source and targetJsonStructure.static JsonGeneratorCreates a JSON generator for writing JSON to a byte stream.static JsonGeneratorcreateGenerator(Writer writer) Creates a JSON generator for writing JSON to a character stream.static JsonGeneratorFactorycreateGeneratorFactory(Map<String, ?> config) Creates a generator factory for creatingJsonGeneratorobjects.static JsonMergePatchcreateMergeDiff(JsonValue source, JsonValue target) Generates a JSON Merge Patch (RFC 7396) from the source and targetJsonValues which when applied to thesource, yields thetarget.static JsonMergePatchcreateMergePatch(JsonValue patch) Creates JSON Merge Patch (RFC 7396) from specifiedJsonValue.static JsonObjectBuilderCreates a JSON object builderstatic JsonObjectBuildercreateObjectBuilder(JsonObject object) Creates a JSON object builder, initialized with the specified object.static JsonObjectBuildercreateObjectBuilder(Map<String, Object> map) Creates a JSON object builder, initialized with the data from specifiedmap.static JsonParserCreates a JSON parser from a byte stream.static JsonParsercreateParser(Reader reader) Creates a JSON parser from a character stream.static JsonParserFactorycreateParserFactory(Map<String, ?> config) Creates a parser factory for creatingJsonParserobjects.static JsonPatchcreatePatch(JsonArray array) Creates a JSON Patch (RFC 6902) from the specified operations.static JsonPatchBuilderCreates a JSON Patch builder (RFC 6902).static JsonPatchBuildercreatePatchBuilder(JsonArray array) Creates a JSON Patch builder (RFC 6902), initialized with the specified operations.static JsonPointercreatePointer(String jsonPointer) Creates JSON Pointer (RFC 6901) from givenjsonPointerstring.static JsonReaderCreates a JSON reader from a byte stream.static JsonReadercreateReader(Reader reader) Creates a JSON reader from a character stream.static JsonReaderFactorycreateReaderFactory(Map<String, ?> config) Creates a reader factory for creatingJsonReaderobjects.static JsonNumbercreateValue(double value) Creates a JsonNumber.static JsonNumbercreateValue(int value) Creates a JsonNumber.static JsonNumbercreateValue(long value) Creates a JsonNumber.static JsonStringcreateValue(String value) Creates a JsonString.static JsonNumbercreateValue(BigDecimal value) Creates a JsonNumber.static JsonNumbercreateValue(BigInteger value) Creates a JsonNumber.static JsonWritercreateWriter(OutputStream out) static JsonWritercreateWriter(Writer writer) static JsonWriterFactorycreateWriterFactory(Map<String, ?> config) Creates a writer factory for creatingJsonWriterobjects.static StringdecodePointer(String escaped) Decodes a passed JSON-pointer string as defined by RFC 6901.static StringencodePointer(String pointer) Encodes (escapes) a passed string as defined by RFC 6901.
-
Method Details
-
createParser
Creates a JSON parser from a character stream.- Parameters:
reader- i/o reader from which JSON is to be read- Returns:
- a JSON parser
-
createParser
Creates a JSON parser from a byte stream. The character encoding of the stream is determined as specified in RFC 7159.- Parameters:
in- i/o stream from which JSON is to be read- Returns:
- a JSON parser
- Throws:
JsonException- if encoding cannot be determined or i/o error (IOException would be cause of JsonException)
-
createGenerator
Creates a JSON generator for writing JSON to a character stream.- Parameters:
writer- a i/o writer to which JSON is written- Returns:
- a JSON generator
-
createGenerator
Creates a JSON generator for writing JSON to a byte stream.- Parameters:
out- i/o stream to which JSON is written- Returns:
- a JSON generator
-
createParserFactory
Creates a parser factory for creatingJsonParserobjects. The factory is configured with the specified map of provider specific configuration properties. Provider implementations should ignore any unsupported configuration properties specified in the map.- Parameters:
config- a map of provider specific properties to configure the JSON parsers. The map may be empty or null- Returns:
- JSON parser factory
-
createGeneratorFactory
Creates a generator factory for creatingJsonGeneratorobjects. The factory is configured with the specified map of provider specific configuration properties. Provider implementations should ignore any unsupported configuration properties specified in the map.- Parameters:
config- a map of provider specific properties to configure the JSON generators. The map may be empty or null- Returns:
- JSON generator factory
-
createWriter
- Parameters:
writer- to which JSON object or array is written- Returns:
- a JSON writer
-
createWriter
Creates a JSON writer to write a JSONobjectorarraystructure to the specified byte stream. Characters written to the stream are encoded into bytes using UTF-8 encoding.- Parameters:
out- to which JSON object or array is written- Returns:
- a JSON writer
-
createReader
Creates a JSON reader from a character stream.- Parameters:
reader- a reader from which JSON is to be read- Returns:
- a JSON reader
-
createReader
Creates a JSON reader from a byte stream. The character encoding of the stream is determined as described in RFC 7159.- Parameters:
in- a byte stream from which JSON is to be read- Returns:
- a JSON reader
-
createReaderFactory
Creates a reader factory for creatingJsonReaderobjects. The factory is configured with the specified map of provider specific configuration properties. Provider implementations should ignore any unsupported configuration properties specified in the map.- Parameters:
config- a map of provider specific properties to configure the JSON readers. The map may be empty or null- Returns:
- a JSON reader factory
-
createWriterFactory
Creates a writer factory for creatingJsonWriterobjects. The factory is configured with the specified map of provider specific configuration properties. Provider implementations should ignore any unsupported configuration properties specified in the map.- Parameters:
config- a map of provider specific properties to configure the JSON writers. The map may be empty or null- Returns:
- a JSON writer factory
-
createArrayBuilder
Creates a JSON array builder- Returns:
- a JSON array builder
-
createArrayBuilder
Creates a JSON array builder, initialized with the specified array- Parameters:
array- the initial array in the builder- Returns:
- a JSON array builder
- Since:
- 1.1
-
createArrayBuilder
Creates a JSON array builder, initialized with the content of specifiedcollection. If the @{code collection} containsOptionals then resulting JSON array builder contains the value from thecollectiononly if theOptionalis not empty.- Parameters:
collection- the initial data for the builder- Returns:
- a JSON array builder
- Throws:
IllegalArgumentException- if the value from thecollectioncannot be converted to the correspondingJsonValue- Since:
- 1.1
-
createObjectBuilder
Creates a JSON object builder- Returns:
- a JSON object builder
-
createObjectBuilder
Creates a JSON object builder, initialized with the specified object.- Parameters:
object- the initial object in the builder- Returns:
- a JSON object builder
- Since:
- 1.1
-
createObjectBuilder
Creates a JSON object builder, initialized with the data from specifiedmap. If the @{code map} containsOptionals then resulting JSON object builder contains the key from themaponly if theOptionalis not empty.- Parameters:
map- the initial object in the builder- Returns:
- a JSON object builder
- Throws:
IllegalArgumentException- if the value from themapcannot be converted to the correspondingJsonValue- Since:
- 1.1
-
createPointer
Creates JSON Pointer (RFC 6901) from givenjsonPointerstring.- An empty
jsonPointerstring defines a reference to the target itself. - If the
jsonPointerstring is non-empty, it must be a sequence of '/' prefixed tokens.
- Parameters:
jsonPointer- the valid escaped JSON Pointer string- Returns:
- a JSON Pointer
- Throws:
NullPointerException- ifjsonPointerisnullJsonException- ifjsonPointeris not a valid JSON Pointer- Since:
- 1.1
- An empty
-
createPatchBuilder
Creates a JSON Patch builder (RFC 6902).- Returns:
- a JSON Patch builder
- Since:
- 1.1
-
createPatchBuilder
Creates a JSON Patch builder (RFC 6902), initialized with the specified operations.- Parameters:
array- the initial patch operations- Returns:
- a JSON Patch builder
- Since:
- 1.1
-
createPatch
Creates a JSON Patch (RFC 6902) from the specified operations.- Parameters:
array- patch operations- Returns:
- a JSON Patch
- Since:
- 1.1
-
createDiff
Generates a JSON Patch (RFC 6902) from the source and targetJsonStructure. The generated JSON Patch need not be unique.- Parameters:
source- the sourcetarget- the target, must be the same type as the source- Returns:
- a JSON Patch which when applied to the source, yields the target
- Since:
- 1.1
-
createMergePatch
Creates JSON Merge Patch (RFC 7396) from specifiedJsonValue.- Parameters:
patch- the patch- Returns:
- a JSON Merge Patch
- Since:
- 1.1
-
createMergeDiff
Generates a JSON Merge Patch (RFC 7396) from the source and targetJsonValues which when applied to thesource, yields thetarget.- Parameters:
source- the sourcetarget- the target- Returns:
- a JSON Merge Patch
- Since:
- 1.1
-
createBuilderFactory
Creates a builder factory for creatingJsonArrayBuilderandJsonObjectBuilderobjects. The factory is configured with the specified map of provider specific configuration properties. Provider implementations should ignore any unsupported configuration properties specified in the map.- Parameters:
config- a map of provider specific properties to configure the JSON builders. The map may be empty or null- Returns:
- a JSON builder factory
-
createValue
Creates a JsonString.- Parameters:
value- a JSON string- Returns:
- the JsonString for the string
- Since:
- 1.1
-
createValue
Creates a JsonNumber.- Parameters:
value- a JSON number- Returns:
- the JsonNumber for the number
- Since:
- 1.1
-
createValue
Creates a JsonNumber.- Parameters:
value- a JSON number- Returns:
- the JsonNumber for the number
- Since:
- 1.1
-
createValue
Creates a JsonNumber.- Parameters:
value- a JSON number- Returns:
- the JsonNumber for the number
- Since:
- 1.1
-
createValue
Creates a JsonNumber.- Parameters:
value- a JSON number- Returns:
- the JsonNumber for the number
- Since:
- 1.1
-
createValue
Creates a JsonNumber.- Parameters:
value- a JSON number- Returns:
- the JsonNumber for the number
- Since:
- 1.1
-
encodePointer
Encodes (escapes) a passed string as defined by RFC 6901. This method doesn't validate the passed JSON-pointer string.- Parameters:
pointer- the JSON-pointer string to encode- Returns:
- encoded JSON-pointer string
- Since:
- 1.1
-
decodePointer
Decodes a passed JSON-pointer string as defined by RFC 6901. This method doesn't validate the passed JSON-pointer string.- Parameters:
escaped- the JSON-pointer string to decode- Returns:
- decoded JSON-pointer string
- Since:
- 1.1
-