Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is javax.json from Glassfish needed as a dependency when using Yasson with JSON-B?

In order to use Java API for JSON Binding (JSON-B), I have found it necessary to include the following three dependencies in my Maven POM:

    <!-- https://mvnrepository.com/artifact/jakarta.json.bind/jakarta.json.bind-api -->
    <dependency>
        <groupId>jakarta.json.bind</groupId>
        <artifactId>jakarta.json.bind-api</artifactId>
        <version>1.0.1</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.eclipse/yasson -->
    <dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>yasson</artifactId>
        <version>1.0.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish/javax.json -->
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.1.4</version>
    </dependency>

The first two make sense to me.

  • jakarta.json.bind-api is the JSON-B API defined by JSR 367.
  • yasson is the reference implementation of that API, Eclipse Yasson.

➥ But what exactly does the third dependency, javax.json from Glassfish, bring to the party? Why is it required for my app to work?

If omitted, when running Jsonb jsonb = JsonbBuilder.create();, I get this error:

javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found

I am confused because I thought Yasson is my JSON processing implementation.

like image 343
Basil Bourque Avatar asked Oct 15 '22 15:10

Basil Bourque


1 Answers

Actually, your code should only depend on the api jakarta.json.bind-api, so you don't accidentally use implementation details from yasson, e.g. the internal org.eclipse.yasson.internal.ReflectionUtils. To do this, you should add <scope>runtime</scope> to your yasson dependency. Only to run it, you need an implementation, and you picked the reference implementation yasson.

But JSON-B is just a layer on top of JSON-P: it does the binding part, while it delegates all the raw JSON processing to JSON-P. You can mix and match any JSON-B implementation with any JSON-P implementation.

As yasson has to be able to work with any JSON-P implementation, it can't have a hard dependency on, e.g., Glassfish JSON-P; you have to specify it yourself (also with a runtime scope). The error message you see mentions Glassfish, as that's the fallback implementation JSON-P is looking for.

like image 116
rü- Avatar answered Nov 15 '22 05:11

rü-