简体   繁体   中英

Jackson doesn't deserialize

I am struggling with deserializing with Jackson fasterXml. I guess there is something about type information and polymorphic use. I tried different things after reading jackson docs and this but still do not manage to get it. Serialization goes well but deserializing the produced string does not make it.

Thanks in advance for your help

Here is the classes :

   @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")  
private class DynReachSlotInternalRep {

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")

    private class ListStartStop extends MergeableLinkedList<DynTimeSlot> {
        private static final long serialVersionUID = 1L;
    }

     private ListStartStop[] weekRep = new ListStartStop[7];

    @SuppressWarnings("unused")
    public ListStartStop[] getWeekRep() {
        return weekRep;
    }

    @SuppressWarnings("unused")
    public void setWeekRep(ListStartStop[] weekRep) {
        this.weekRep = weekRep;
    }

    public DynReachSlotInternalRep() {
        super();

    }
/...



@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public class DynTimeSlot extends IntervalComparison<DynTimeSlot> {

private int startHour;
private int startMinute;
private int stopHour;
private int stopMinute;


public int getStartHour() {
    return startHour;
}

public void setStartHour(int startHour) {
    this.startHour = startHour;
}

public int getStartMinute() {
    return startMinute;
}

public void setStartMinute(int startMinute) {
    this.startMinute = startMinute;
}

public int getStopHour() {
    return stopHour;
}

public void setStopHour(int stopHour) {
    this.stopHour = stopHour;
}

public int getStopMinute() {
    return stopMinute;
}

public void setStopMinute(int stopMinute) {
    this.stopMinute = stopMinute;
}

public DynTimeSlot(int startHour, int startMinute, int stopHour, int stopMinute) {
    super();
    if ((stopHour < startHour) || ((stopHour == startHour) && (startMinute > stopMinute)))
        throw new RuntimeException("Malformed timeslot");

    this.startHour = startHour;
    this.startMinute = startMinute;
    this.stopHour = stopHour;
    this.stopMinute = stopMinute;
}

public DynTimeSlot() {
    super();

}

/...

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public class MergeableLinkedList<A extends IntervalComparison<A>>  extends LinkedList<A> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public MergeableLinkedList() {
    super();
    }


public void insert(A toInsert) {
    recursiveInsert(toInsert, this.listIterator());
}

public void delete (A toDelete) {
    recursiveDelete(toDelete, this.listIterator());
}

Here is the stack trace :

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.dynamease.entity.DynReachSlot$DynReachSlotInternalRep]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@277b65b1; line: 1, column: 70]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:157)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:113)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:82)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:894)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:36)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.dynamease.entity.DynReachSlot$DynTimeSlotOperations.getFromDirectoryRepresentation(DynReachSlot.java:145)
at com.dynamease.entity.DynReachSlot.<init>(DynReachSlot.java:53)
at com.dynamease.entity.DynReachSlotTest.test2(DynReachSlotTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

From what you've posted, I think you're missing a JsonCreator() for the DynReachSlotInternalRep class.

Here 'sa nice tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM