0

Problem

The JSON-B user guide suggests here that I should be able to serialize null values with my code of:

    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig()
            .withNullValues(true)
            .withAdapters(new StatusAdapter()));

    jsonb.toJson(status,Status.class);

Here StatusAdapter is my own JsonbAdapter impl to serialize the app's Status class and looks something like this:

StatusAdapter

@Override
public JsonObject adaptToJson(Status status) throws Exception {

   // ... 

   return Json.createObjectBuilder()

            .add("field1", status.getField1())               
            .add("field2", status.getField2())               

             // ... continues ...

            .build();

But when one of these getters returns null, I end up getting exceptions like:

Caused by: java.lang.NullPointerException: Value in JsonObjects name/value pair cannot be null at org.glassfish.json.JsonObjectBuilderImpl.validateValue(JsonObjectBuilderImpl.java:222) at org.glassfish.json.JsonObjectBuilderImpl.add(JsonObjectBuilderImpl.java:90) at mypkg.StatusAdapter.adaptToJson(StatusAdapter.java:47) at mypkg.StatusAdapter.adaptToJson(StatusAdapter.java:14) at org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer.serialize(AdaptedObjectSerializer.java:103)

pom.xml

I used the latest versions of the Maven dependencies from the JSON-B Getting Started page:

            <dependency>
                    <groupId>javax.json.bind</groupId>
                    <artifactId>javax.json.bind-api</artifactId>
                    <version>1.0</version>
            </dependency>

            <dependency>
                    <groupId>org.eclipse</groupId>
                    <artifactId>yasson</artifactId>
                    <version>1.0.2</version>
            </dependency>

            <dependency>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.json</artifactId>
                    <version>1.1.3</version>
            </dependency>

Question

So am I misunderstanding the API? Or is this an incorrect combination of JSON-related libraries? A bug in one of them?

I realize too, that serializing a null might leave me with a second problem when it's time to deserialize it. I am going to want to use JSON-B to deserialize later, if that helps guide the answer. Thank you.

Update:

This yasson issue suggests it might be a bug. Added more detail there.

Scott Kurz
  • 4,985
  • 1
  • 18
  • 40
  • Are you telling Jsonb that the fields you're trying to use can be null? Cause setting the building to allow null values doesn't mean that it'll allow null values I would assume. Just by the docs alone, I would assume that you'd need to use the annotations shown in the docs. Have you done so? – Alex Couch Oct 24 '18 at 22:30
  • No, I would have tried class/package/field annotations on objects I developed from source but this is an external class, which is why I'm using the adapter to begin with. – Scott Kurz Oct 24 '18 at 23:39
  • While referenced yasson issue is actually a bug, it is not related to the issue in the question. – Roman Grigoriadi Nov 02 '18 at 14:29

1 Answers1

1

Exception which you get is basically telling you that you can't pass a null into JsonObjectBuilder#add method, which is thrown by JSONP implementation org.glassfish:javax.json.

Setting JsonbConfig#withNullValues(true) does not relate to it.

JSONB and JSONP are two different APIs, even so JSONB does depend on JSONP. org.eclipse:yasson is an implementation of JSONB API which in your case does use org.glassfish:javax.json - an implementation of JSONP API, because you provided it in your POM.

Setting JsonbConfig#withNullValues(true) is a call on JSONB API which does mean "print null values to JSON" for example:

{ firstname: null, lastname: null }

So when org.eclipse:yasson (JSONB) encounters a null value in a pojo object property it does something like javax.json.stream.JsonGenerator#writeNull(key).

This does not relate in any way to your code in StatusAdapter which is basically calling JSONP API JsonObjectBuilder#add with a null value, which is not allowed.

Roman Grigoriadi
  • 1,938
  • 14
  • 8
  • Thank you Roman for explaining. In stepping through the code, it didn't feel like a bug, and I was guessing I was misunderstanding the relationship btw. JSONB and JSONP here. – Scott Kurz Nov 02 '18 at 17:18