Android using MSGPack Core and Jackson Mapper - decode class variable of unknown type
Asked Answered
A

2

6

I am sending/receiving a custom class from a server to Android, the class is as;

import org.msgpack.value.Value;
public class myClass {

    public String status;
    public Value data;

}

The problem is that I always get the error;

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.msgpack.value.Value, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
        at [Source: java.io.BufferedInputStream@f478759; line: -1, column: 100] (through reference chain:xxx.xxxxxxxxxx.xxx.xxxxxx.myClass["data"]

If I change the variable "data" to say MAP<String, String> data then it works fine, however, data is of an unknown type! (well normally HashMap or an Array maybe a String, not some other class).

MessagePackFactory factory = new MessagePackFactory();
ObjectMapper mapper = new ObjectMapper(factory);
myClass response = mapper.readValue(inputStream, myClass.class);

How can I specify an unknown type?

Acton answered 16/12, 2015 at 16:13 Comment(0)
A
2

So I changed the class to;

public class myClass{

    public String status;
    public Object data;

}

And I now just test the Object type. I am not sure why I did not try this before!

Acton answered 4/1, 2016 at 17:0 Comment(1)
Because now I do not have a nice data.getInteger function.Acton
T
1

org.msgpack.value.Value is an interface.

When you are de-serializing values using ObjectMapper, the target must be a Class with Default Constructor. Other wise OM cannot create a target Object.

Twitch answered 8/1, 2016 at 23:16 Comment(1)
But if I do not know what "data" type is going to be how can I create a Constructor?Acton

© 2022 - 2024 — McMap. All rights reserved.