Using Jackson with generic types

时间:2015-06-25 18:28:48

标签: java json jackson mixins

Let's assume I have the following class:

public class Sample<T> {
    private String smth;
}

I need to have the name of the "smth" property depends on the generic type. For example: Sample<String> should be turned to {"string":value} and Sample<Integer> to {"int":value}

I tried to use Jackson MixIn but it can be applied only on the concrete class (not generic)

Can anyone propose somethig to sort it out?

1 个答案:

答案 0 :(得分:2)

此问题称为删除

如果您有此代码

Sample<String> mysample = ...

并希望转换{"string":value}

中的 mysample

这是不可能的。

在运行时,&lt;字符串&gt; 信息不可用。这称为擦除。

What is the concept of erasure in generics in Java?

Effects of Type Erasure and Bridge Methods