Hi,
Running into issue while deserializing a class with value of type Object. 
Java class is as following.

public final class Message {

    private final MessageHeader header;
    private final Object body;

    @JsonCreator
    public <T> Message(@JsonProperty("header") MessageHeader header, 
@JsonProperty("body") T body) {
        this.header = header;
        this.body = body;
    }

    public MessageHeader getHeader() {
        return header;
    }

    public Object getBody() {
        return body;
    }

    @JsonIgnore
    public <T> T getBody(Class<T> objectType) {
        if(objectType.isInstance(this.body)) {
            return objectType.cast(this.body);
        }
        throw new Exception("Message body is not of type 
"+objectType.toString());
    }}

Using Jackson 2.8.4 for serializing and deserializing the object in JSON 
format using a mapper enabled with DefaultTyping 
(mapper.enableDefaultTyping()). Running into problems while trying to 
retrieve the message body part using generic version of getBody method. 
Getting object of type java.util.ArrayList instead of the actual object 
type passed. Any suggestions on what is missing mapper configuration or any 
other ?

Message.getBody is invoked as following.

DataSet data = getDataSet();Message message = new 
Message(header,data);...DataSet data = message.getBody(DataSet.class);

JSON message is as following which when attempted to deserialize is giving 
problem.

{ 
  "header" : {
    "hostId" : "XYZ",
    "hostname" : "X-123456789",
    "systemType" : "ABC"
  },"
  "body" : [ "com.foo.DataSet", {
    "createTimestamp" : 1502859923155,
    "records" : [ "java.util.ArrayList", [ {
      "name" : "Running",
      "timestamp" : 1502859923162,
      "value" : true
    }, {
      "name" : "Command",
      "timestamp" : 1502859923162,
      "value" : false
    } ] ]
  } ]}


[1] - 
https://stackoverflow.com/questions/45675200/json-deserialization-using-jackson-for-java-lang-object-returns-java-lang-arrayl

Regards
Vinu

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to