'JsonbDeserializer's deserialize method does NOT read existing element

Currently I'm using WildFly 21.0.2 and JSON-B and JSON-P APIs. The Yasson version in WildFly modules is 1.0.5. I have the following JSON coming from REST endpoint:

{
  "circuitInfoResponseList": [
    {
      "org.my.company.dto.FiberCircuitInfoResponse": {
        "attendanceType": "BY_RADIUS",
        "index": 0,
...

This is my JsonbDeserializer implementation:

public CircuitInfoResponse deserialize(JsonParser jsonParser, DeserializationContext deserializationContext, Type type) {
        jsonParser.next();

        String className = jsonParser.getString();
        jsonParser.next();

        try {
            return deserializationContext.deserialize(Class.forName(className).asSubclass(CircuitInfoResponse.class), jsonParser);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new JsonbException("Cannot deserialize object.");
        }
        //return deserializationContext.deserialize(FiberCircuitInfoResponse.class, jsonParser);
    }

This method gets the SECOND entry from the json attendanceType and NOT the desired org.my.company.dto.FiberCircuitInfoResponse. BTW... when I serialize the JSON Object I can see the string org.my.company.dto.FiberCircuitInfoResponse however when it arrives and the client side it does NOT contain that string. It comes likes this:

[
  {
    "circuitInfoResponseList": [
      {
        "attendanceType": "BY_RADIUS",
        "index": 0,

Without that information I cannot tell which subclass to create. I've already tried to follow this tips but without success:

These are my POJO classes.

Parent class:

import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.json.bind.annotation.JsonbTypeDeserializer;

@Data
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonbTypeDeserializer(CircuitInfoResponseJsonbXerializer.class)
public class CircuitInfoResponse {
...
}

Child class:

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FiberCircuitInfoResponse extends CircuitInfoResponse {
...
}

Serialize code:

Type responseListType = new ArrayList<SimulationServiceResponse>() {}.getClass().getGenericSuperclass();

        JsonbConfig config = new JsonbConfig()
                .withSerializers(new CircuitInfoResponseJsonbXerializer());

        Jsonb jsonb = JsonbBuilder.create(config);

        String json = jsonb.toJson(response, responseListType);
        System.out.println(json);

        return Response.status(Response.Status.OK).entity(json).build();

Deserialize code:

String restJsonResponse = restResponse.readEntity(String.class);
            JsonbConfig config = new JsonbConfig()
                    .withDeserializers(new CircuitInfoResponseJsonbXerializer());
            Jsonb jsonbCustom = JsonbBuilder.create(config);
            List<SimulationServiceResponse> restResponseEntity = jsonbCustom.fromJson(restJsonResponse, new ArrayList<SimulationServiceResponse>() {}.getClass().getGenericSuperclass());

This is the class that contains a list of Parent class above:

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class SimulationServiceResponse {

    ...
    
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private List<CircuitInfoResponse> circuitInfoResponseList;
    
    public List<CircuitInfoResponse> getCircuitInfoResponseList() {
        if (circuitInfoResponseList == null) {
            circuitInfoResponseList = new ArrayList<>();
        }
        return circuitInfoResponseList;
    }

    public void setCircuitInfoResponseList(List<CircuitInfoResponse> list) {
        this.circuitInfoResponseList = list;
    }

}

Do you guys have any idea of what I'm doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source