Skip to content

Latest commit

 

History

History
84 lines (61 loc) · 5.82 KB

JSON-B.adoc

File metadata and controls

84 lines (61 loc) · 5.82 KB

JSON-B

A short document about (de-)serialization of JSON using the JSON-B API. (See also JSON on Wikipedia for a good introduction, JSON Processing for manipulation of JSON content.)

Dependency

If you use Java SE, you need to add the required dependencies (jakarta.json-api, jakarta.json, jakarta.json.bind-api, yasson).

Specify dependencies in your pom.xml
<dependency>
	<groupId>jakarta.json</groupId>
	<artifactId>jakarta.json-api</artifactId>
	<version>2.1.0</version>
</dependency>
<dependency>
	<groupId>org.glassfish</groupId>
	<artifactId>jakarta.json</artifactId>
	<version>2.0.1</version>
	<scope>runtime</scope>
</dependency>

<dependency>
	<groupId>jakarta.json.bind</groupId>
	<artifactId>jakarta.json.bind-api</artifactId>
	<version>2.0.0</version>
</dependency>
<dependency>
	<groupId>org.eclipse</groupId>
	<artifactId>yasson</artifactId>
	<version>2.0.4</version>
	<scope>runtime</scope>
</dependency>

If you use Java EE 8, this is already provided by your application server.

Default mapping

The following rules apply in case of default mapping (thus with no customization) and for most usual cases (fields are not public, for example).

Deserialization of a class uses its public or protected no-argument constructor, which must exist.

Only public getter methods, for serialization (setters for deserialization) are considered and used, irrespective of whether a corresponding field exist.

A transient field is ignored, even if matching input name-value pair.

Deserialization into Collection is supported; into other interfaces, is not supported.

Serialization follows the lexicographical order of properties.

Customized mapping

Use @JsonbTransient for not processing a property. Annotating the field takes effect for both serialization and deserialization. Annotating the getter (setter) takes effect only for serialization (deserialization).

Use @JsonbPropertyOrder on the class for overriding the order of processing of properties.

Use @JsonbCreator to annotate the constructor or static factory method that deserialization should use, use @JsonbProperty on each parameter to map JSON names to parameters (otherwise “the proper mapping is NOT guaranteed”).

Implement JsonbAdapter to customize the mapping for a given type. Register it on the Json-B instance with JsonbConfig::withAdapters or annotate the related type with @JsonbTypeAdapter.

Implement JsonbSerializer in order to serialize an original type using a provided JsonGenerator; and similarly JsonbDeserializer using a provided JsonParser. Register those instances with JsonbConfig::withSerializers and JsonbConfig::withDeserializers or annotate the related type with @JsonbSerializer or @JsonbDeserializer.

References