KeyBridge / lib-jsonb-adapter

Jakarta JSON Binding (JSON-B) utilities

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lib-jsonb-adapter

Jakarta JSON Binding (JSON-B) utilities

JSON-B is a standard binding layer for converting Java objects to/from JSON messages. It defines a default mapping algorithm for converting existing Java classes to JSON, while enabling developers to customize the mapping process through the use of Java annotations.

This utility library provides a convenient wrapper of the Jsonb the JSON Binding framework operations.

  • fromJson: read JSON input, deserialize to Java objects content tree
  • toJson: serialize Java objects content tree to JSON input

A custom JsonbUtility class is provided to simplify marshal and unmarshal of Java objects.

Usage

The utility should just work for marshaling and unmarshaling of Java classes to and from JSON encoded text.

// Marshal a Java class to a json string.
Entity entity = //get entity//
String json = new JsonbUtility().marshal(entity);

// Unmarshal a json string to a Java class
Entity recoveredEntity = new JsonbUtility().unmarshal(json, Entity.class);
      
// Entities should be identical
Assert.assertEquals(entity, recoveredEntity);

// Print the Json text for fun.
System.out.println(json);

Custom adapters and serializers / deserializaters. JTS geometry adapters are included by default (we use these extensively), and other custom adapters are provided in the ext package. Add these, or add your own using the standard 'with' method syntax.

JsonbUtility jsonbUtility = new JsonbUtility()
  .withAdapters(new JsonbBase64CompressedAdapter());

Dependency requirements
The following dependencies are marked as provided by this library POM file and must be added to your project. Note that JSONB is included with most current J2EE application servers. (Confirmed in Glassfish and Payara 5.x)

<dependencies>
    <!-- JSON-P -->
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.json</artifactId>
        <version>1.1.5</version>
        <scope>runtime</scope>
    </dependency>
    <!-- JSON-B API -->
    <dependency>
        <groupId>jakarta.json.bind</groupId>
        <artifactId>jakarta.json.bind-api</artifactId>
        <version>1.0.1</version>
    </dependency>
    <!-- Yasson (JSON-B reference implementation) -->
    <dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>yasson</artifactId>
        <version>1.0.3</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Encoding
In deserialization operations (fromJson), encoding of JSON data is detected automatically. In serialization operations (toJson), UTF-8 encoding is used by default for writing JSON data.

I-JSON support
I-JSON (short for "Internet JSON") is a restricted profile of JSON designed to maximize interoperability and increase confidence that software can process it successfully with predictable results.

Adapters
Adapter is a class implementing javax.json.bind.adapter.JsonbAdapter interface. It has a custom code to convert the “unmappable” type (Original) into another one that JSONB can handle (Adapted). This library implements custom adapters for:

  • JTS Geometry
  • JTS Envelope
  • GZip compressed byte array
  • Map of Doubles

The Gzip and Map adapters are located in the ext package and not included by default in the JsonbUtility configuration.

Compatibility with JAX-B
A custom PropertyVisibilityStrategy implementation is included in the default JsonbUtility configuration to recognize and respect JAXB annotations. The JsonbPropertyVisibilityStrategy tries to emulate the XmlAccessType.FIELD JAXB annotation strategy where all methods are ignored unless specifically annotated with XmlElement or XmlAttribute. All class fields are recognized by default unless annotated with XmlTransient.

License

Apache License Version 2.0. "Knock yerself out"

References

About

Jakarta JSON Binding (JSON-B) utilities

License:Apache License 2.0


Languages

Language:Java 100.0%