jakartaee / jakartaee/jsonb-api

Default subtype in @JsonbTypeInfo (for polymorphism deserialization)

Open
#368 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
95
Forks
41
Avg merge
1d 6h
Merged PRs (30d)
35

Description

With `@JsonbTypeInfo` and `@JsonbSubtype` it is possible to implement polymorphism on deserialization.

But sometimes (especially in the case of the evolution of a REST API over time) the attribute corresponding to the defined `key` might not be present in the provided JSON message.

For this use-case, being able to specify the "default subtype" would be necessary.

Currently *yasson* is failing with this error:

```
jakarta.json.bind.JsonbException: Cannot infer a type for unmarshalling into: snippet.Snippet$Animal
```

Complete example:

```java
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbException;
import jakarta.json.bind.annotation.JsonbSubtype;
import jakarta.json.bind.annotation.JsonbTypeInfo;

public class Snippet {

@JsonbTypeInfo({
@JsonbSubtype(alias = "dog", type = Dog.class),
@JsonbSubtype(alias = "cat", type = Cat.class)
})
public static interface Animal {
}

public static final class Dog implements Animal {
public boolean isDog = true;

@Override
public String toString() {
return "Dog [isDog=" + isDog + "]";
}
}

public static final class Cat implements Animal {
public boolean isCat = true;

@Override
public String toString() {
return "Cat [isCat=" + isCat + "]";
}
}

public static void main(String[] args) {
// Create a Jsonb instance
Jsonb jsonb = JsonbBuilder.create();

// Create instances of Dog and Cat
Dog myDog = new Dog();
Cat myCat = new Cat();

// Serialize Dog instance to JSON
try {
String dogJson = jsonb.toJson(myDog);
System.out.println("Serialized Dog JSON: " + dogJson);

// Serialize Cat instance to JSON
String catJson = jsonb.toJson(myCat);
System.out.println("Serialized Cat JSON: " + catJson);

// Deserialize JSON (polymorphic deserialization) Dog
Animal deserializedAnimalFromDogJson = jsonb.fromJson("{\"@type\":\"dog\"}", Animal.class);
System.out.println("Deserialized Dog: " + deserializedAnimalFromDogJson);
System.out.println("Deserialized Animal from Dog (instance of check): " + (deserializedAnimalFromDogJson instanceof Dog));

// Deserialize JSON (polymorphic deserialization) Cat
Animal deserializedAnimalFromCatJson = jsonb.fromJson("{\"@type\":\"cat\"}", Animal.class);
System.out.println("Deserialized Cat: " + deserializedAnimalFromCatJson);
System.out.println("Deserialized Animal from Cat (instance of check): " + (deserializedAnimalFromCatJson instanceof Cat));

// Deserialize JSON (default impl)
Animal a = jsonb.fromJson("{}", Animal.class);
System.out.println("Deserialized Animal: " + a);

} catch (JsonbException e) {
e.printStackTrace();
}
}
}
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the complete Java example in the issue and the @JsonbTypeInfo/@JsonbSubtype annotation definitions. Define how a default subtype is specified and selected when the JSON lacks the key, while preserving explicit alias-based deserialization; done means the example's {} input deserializes successfully without changing dog or cat behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.