Questions tagged [jsonb-api]

For questions about Java API for JSON Binding (JSON-B, JSR-367), part of Java EE 8.

From json-b.net:

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.

To serialize/deserialize a Java Object to/from JSON

The Java Class

public class Dog {
    public String name;
    public int age;
    public boolean bitable;
}

JSON-B API calls

public static void main(String[] args) {

    // Create a dog instance
    Dog dog = new Main.Dog();
    dog.name = "Falco";
    dog.age = 4;
    dog.bitable = false;

    // Create Jsonb and serialize
    Jsonb jsonb = JsonbBuilder.create();
    String result = jsonb.toJson(dog);

    System.out.println(result);

    // Deserialize back
    dog = jsonb.fromJson("{\"name\":\"Falco\",\"age\":4,\"bitable\":false}", Dog.class);

}

The JSON representation

{
    "name": "Falco",
    "age": 3,
    "bitable": false
}
52 questions
7
votes
1 answer

How to reject deserialization of unrecognized properties with JSON-B

I am attempting to migrate the implementation details of some JSON databinding code to use the Java EE 8 JSON-B APIs instead of Jackson. In order to match the default behavior of Jackson, I want to reject any attempts to deserialize a JSON payload…
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
6
votes
1 answer

Deserialize JSON into polymorphic POJO with JSON-B / Yasson

I have a PATCH endpoint in a resource class with an abstract class as a request body. I'm getting the following error: 22:59:30 SEVERE [or.ec.ya.in.Unmarshaller] (on Line: 64) (executor-thread-63) Can't create instance It seems that because the…
spaykit
  • 121
  • 1
  • 8
6
votes
1 answer

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:
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
6
votes
2 answers

How to deserialize an interface with json-b?

I'm adapting this Jackson code: @JsonDeserialize(as = EntityImpl.class) public interface Entity { ... } The original code works well, even for nested Entity objects. How to do the same with the new json-b specification? I tried using…
David
  • 840
  • 6
  • 17
  • 37
5
votes
3 answers

JSON-B REST payload validation

I've developed a Java REST service using JSON-B to map the incoming payload to a POJO. Now what I'd like to do is to validate the incoming payload, possibly against a JSON schema, but I haven't been able to find anything in this sense so far... Is…
voccoeisuoi
  • 325
  • 4
  • 11
4
votes
1 answer

Instead of @JsonbIgnore, is it possible to require explicit inclusion of an object's fields on jax-rs (jersey+moxy) JSON serialization?

I have a JPA entity implementing an interface and I want to expose via jax-rs endpoint only the fields that are defined by that interface. Something that looks like: public interface ConnectedAppExternal { String getId(); String…
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
4
votes
2 answers

Deserializing JSON array with JSON-B

I'm trying to deserialize a JSON Array with JSONB. JSON [ { "id": "1", "animal": "dog", "age": "3" }, { "id": "2", "animal": "cat", "age": "5" } ] Controller Jsonb jsonb = JsonbBuilder.create(); Animal…
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
4
votes
1 answer

Convert JsonObject to pojo efficiently with JSON-B 1.0 (e.g. Yasson, Java EE 8)

A JsonObject can be transformed into it's corresponding class instance via: Pojo pojo = JsonbBuilder.create().fromJson(jsonObject.toString(), Pojo.class) However, it seems to be inefficient to use jsonObject.toString() as a String is an other…
Marc Dzaebel
  • 425
  • 4
  • 12
4
votes
1 answer

How to deserialize a JSON string to a non-public class using JSON-B?

I've created a trival Java 9 Maven app with two classes to test the serialization and deserialization of JSON using JSON-B. Here's the code: package com.jsonbdemos; import javax.json.bind.Jsonb; import javax.json.bind.JsonbBuilder; import…
skomisa
  • 16,436
  • 7
  • 61
  • 102
2
votes
0 answers

how to make distinction between an intentional null and a non present field parsed from json as null?

I'm developing a CRUD web application which exposes a REST endpoint to update an existing document in a MongoDB collection. The json object sent by the frontend only contains the fields that have to change. Since the json parser will initialize all…
Fab
  • 135
  • 9
2
votes
1 answer

Java Jsonb deserializing UTC datetime in ISO8601

I'm using JSON-B (yasson implementation) and I'm receiving data for an object with a field like this { ... "timestamp": "2020-03-19T06:42:42Z", ... } which is perfectly standard ISO 8601 for a UTC date-time value. Now the corresponding Java…
2
votes
1 answer

JSON Binding @JsonbTypeDeserializer annotation ignored on enums?

I'm converting a JAXB application to JSON-B and I've run into an issue while trying to deserialize a Java enum using a custom JsonbDeserializer inside one of my tests. The original JSON I need to deserialize contains ints referencing the enum's…
Lukáš Petrovický
  • 3,945
  • 1
  • 11
  • 20
2
votes
2 answers

Access JAX-RS resource annotations from a JsonbSerializer

I have an application running on Payara 4 using a custom GSON JSON adapter. I would like to migrate to Payara 5 (5.191) and start using JSON-B. In our current application we can control the JSON output using annotations on a resource. For example…
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
2
votes
1 answer

How can I prevent empty strings and empty collections to be outputted with eclipse Yasson

We want to create a json string for some Java objects, but we don't want empty strings or empty arrays to be added to the json output. We are using Eclipse Yasson 1.0.1 to create the json strings. Actually what we want is the behaviour of…
Gert
  • 23
  • 4
2
votes
1 answer

Serialize a UUID as canonical hex string in JSON using JSON-B

UUID A universally unique identifier (UUID) is a 128-bit value. Represented in Java by the java.util.UUID class. Hex string For display and for serialization, it is canonically formatted as a 36-character hexadecimal string arranged in five groups…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1
2 3 4