Configuring JSON-B in Liberty

You can configure your Liberty environment to use JSON-B by enabling the default jsonb-1.0 feature or by implementing the jsonbContainer-1.0 feature.

About this task

Open Liberty In version 21.0.0.1 and later, for information about using JSON-B with Liberty, see the Open Liberty website.

JSON Binding (JSON-B) 1.0 for WebSphere® Application Server Liberty is built on the Eclipse Yasson open source project.

Eclipse Yasson is the reference implementation of the JSON-B 1.0 specification, and is the JSON-B implementation that Liberty provides by default when the jsonb-1.0 feature is enabled. It is possible to configure an alternative implementation of JSON-B through the jsonbContainer-1.0 feature.

Procedure

  • Select the correct JSON-B feature in Liberty.
    The following features are offered for JSON-B 1.0 integration with Liberty:
    • jsonb-1.0
    • jsonbContainer-1.0

    The default JSON-B feature is jsonb-1.0, which provides generic JSON-B support and can use any specification-compliant implementation. This feature provides the JSON-B API to applications and features that depend on it. Unless specified otherwise by other features, this feature uses the Eclipse Yasson implementation that is included in Liberty.

    The jsonbContainer-1.0 feature provides the JSON-B API and provides JSON-B support by using a user supplied JSON-B implementation that is configured as a BELL (Basic Extension using Liberty Libraries). The <bell> server configuration element is used to configure this implementation as a BELL.

    For example, Apache Johnzon is another JSON-B compliant implementation. In this example, a server is configured to use Johnzon instead of Yasson:

    <server>
      <featureManager>
        <feature>jsonbContainer-1.0</feature>
      </featureManager>
    
      <bell libraryRef="johnzon"/>
    
      <library id="johnzon">
        <fileset dir="${server.config.dir}/johnzon"includes="*.jar"/>
      </library>
    </server>
  • Write features that depend on JSON-B.

    When you write a feature that depends on JSON-B, special design considerations must be made:

    Instead of obtaining a javax.json.bind.spi.JsonbProvider instance by using standard mechanisms, such as JsonbBuilder.create(), or JsonbProvider.provider(), features declare OSGi service dependencies on JsonbProvider, and JsonpProvider, and use these injected services when building objects. The following code is an example:
    @Component( ... )
    public class ServiceThatRequiresJsonb {
    
      @Reference
      private JsonbProvider jsonbProvider;
    
      @Reference
      private JsonProvider jsonpProvider;
    
      public void doSomethingWithJsonb() {
        Jsonb jsonb = JsonbBuilder.newBuilder(jsonbProvider).withProvider(jsonpProvider).build();
        // ...
      }
    }