0

Parent class:

@Data
public class ParentDAO {
    @JsonDeserialize(using = CustomInstantDeserializer.class)
    private final Instant estimatedTimeOfBerthing;
    @JsonDeserialize(using = CustomInstantDeserializer.class)
    private final Instant actualTimeOfBerthing;
}

Child class:

@Data
public class ChildDAO extends ParentDAO {
    private final int countOfCnr1;
    private final int countOfCnr2;


    @Builder
    public Child(Instant estimatedTimeOfBerthing, Instant actualTimeOfBerthing,
                            int countOfCnr1, int countOfCnr2) {

        super(estimatedTimeOfBerthing, actualTimeOfBerthing);
        this.countOfCnr1= countOfCnr1;
        this.countOfCnr2= countOfCnr2;
    }
}

CustomInstantDeserializer class:

public class CustomInstantDeserializer extends JsonDeserializer<Instant> {
    @Override
    public Instant deserialize(JsonParser jsonparser, DeserializationContext context)
            throws IOException {
        return BpmsDateUtils.fromString(jsonparser.getText());
    }
}

Sample of JSON from data layer (JsonSerialized):

{
"estimatedTimeOfBerthing": "2019-10-10T05:30:00+08:00",
"actualTimeOfBerthing": "2019-10-10T06:50:00+08:00",
"countOfCnr1":  30
"countOfCnr2":  40
}

In the scenario above, my API layer is retrieving data from the data layer(JSON). Mapping into ParentDAO has got no issues but when mapping into ChildDAO, the following exception is thrown -> JSON parse error: Cannot deserialize value of type `java.time.Instant.

I kind of know where the issue lies in -> I'm passing in an Instant to the child constructor without it having any knowledge of how to deserialize it. Anyone has got any ideas or have encountered the same issue? Any help would be greatly appreciated.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
DanialChan
  • 100
  • 7
  • Have you tried putting `@JsonDeserialize` on the `ChildDAO` constructor parameters? – Jan Rieke Nov 12 '19 at 13:13
  • What’s a _DAO_ in your scenario? Looks like a value object to me, not a DAO. – Michael Piefel Nov 12 '19 at 13:45
  • @JanRiekeTried and it doesn't work – DanialChan Nov 12 '19 at 13:48
  • @MichaelPiefel sorry about the naming! my vendors left it this way...and i have yet change it – DanialChan Nov 12 '19 at 13:48
  • @DanialChan, which version of `Jackson` do you use? You can annotate constructor using `JsonCreator` annotation and check this question: [Jackson tries to use all argument constructor since update to 2.8.1](https://stackoverflow.com/questions/39417909/jackson-tries-to-use-all-argument-constructor-since-update-to-2-8-1) – Michał Ziober Nov 12 '19 at 19:33

0 Answers0