Jackson - Deserialize Generic Class Variable

I posted this question incorrectly. I posted the question correctly here ...

I get a json string as an HTTP response. I know its structure. It looks like this:

public class Json<T> { public Hits<T> hits; } public class Hits<T> { public int found; public int start; public ArrayList<Hit<T>> hit; } public class Hit<T> { public String id; public Class<T> data; } 

The data field can belong to any class. I will only know this at runtime. I will get it as a parameter. This is how I deserialize.

 public <T> void deSerialize(Class<T> clazz { ObjectMapper mapper = new ObjectMapper(); mapper.readValue(jsonString, new TypeReference<Json<T>>() {}); } 

But I get an error -

cannot access private java.lang.class.Class () from java.lang.class. Failed to establish access. Cannot make java.lang.Class constructor available

+10
java json generics jackson
Jul 25 2018-12-25T00:
source share
4 answers

You will need to explicitly build JavaType if the generic type is only available dynamically:

 // do NOT create new ObjectMapper per each request! final ObjectMapper mapper = new ObjectMapper(); public Json<T> void deSerialize(Class<T> clazz, InputStream json) { return mapper.readValue(json, mapper.getTypeFactory().constructParametricType(Json.class, clazz)); } 
+9
Jul 27 '12 at 19:27
source share

An example of a typical deserialization interface:

 public interface Deserializable<T> { static final ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") default T deserialize(String rawJson) throws IOException { return mapper.readValue(rawJson, (Class<T>) this.getClass()); } } 
+2
Jan 20 '16 at 13:06
source share

Do you serialize and deserialize a class object in JSON? Perhaps save it as a String in Hit and create an additional getter that launches Class.forName, for example.

 public class Hit { public String id; public String data; public Class<?> getDataClass() throws Exception { return Class.forName(data); } } 
+1
Jul 26 2018-12-12T00:
source share

Deserialize General class variable

...

How can I say this to Jackson? Would gson be better?

The Gson User Guide contains a section on what I understand, which you are trying to execute, although this documented example may not be complete.

In a blog post, I examined in detail the solution using Gson 1.7.1. Below is the corresponding code example.

Similar (but more popular) solutions using Jackson (1.8.2) are also demonstrated and described in the same blog. (Hundreds of lines of code are used in different approaches and examples, so I skipped sending them again.)

 public class GsonInstanceCreatorForParameterizedTypeDemo { public static void main(String[] args) { Id<String> id1 = new Id<String>(String.class, 42); Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdInstanceCreator()).create(); String json1 = gson.toJson(id1); System.out.println(json1); // actual output: {"classOfId":{},"value":42} // This contradicts what the Gson docs say happens. // With custom serialization, as described in a // previous Gson user guide section, // intended output may be // {"value":42} // input: {"value":42} String json2 = "{\"value\":42}"; Type idOfStringType=new TypeToken<Id<String>>(){}.getType(); Id<String> id1Copy = gson.fromJson(json2, idOfStringType); System.out.println(id1Copy); // output: classOfId=class java.lang.String, value=42 Type idOfGsonType = new TypeToken<Id<Gson>>() {}.getType(); Id<Gson> idOfGson = gson.fromJson(json2, idOfGsonType); System.out.println(idOfGson); // output: classOfId=class com.google.gson.Gson, value=42 } } class Id<T> { private final Class<T> classOfId; private final long value; public Id(Class<T> classOfId, long value) { this.classOfId = classOfId; this.value = value; } @Override public String toString() { return "classOfId=" + classOfId + ", value=" + value; } } class IdInstanceCreator implements InstanceCreator<Id<?>> { @SuppressWarnings({ "unchecked", "rawtypes" }) public Id<?> createInstance(Type type) { Type[] typeParameters = ((ParameterizedType) type).getActualTypeArguments(); Type idType = typeParameters[0]; return new Id((Class<?>) idType, 0L); } } 
0
Jul 26 '12 at 16:26
source share



All Articles