1

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?

beresfordt
  • 5,088
  • 10
  • 35
  • 43
Dzmitry
  • 87
  • 1
  • 8

1 Answers1

2

This problem is called erasure.

If you have this code

Sample<String> mysample = ...

and want to transform mysample in {"string":value}

It is not possible.

At runtime, the < String> information is not available. This is called erasure.

What is the concept of erasure in generics in Java?

Effects of Type Erasure and Bridge Methods

Community
  • 1
  • 1
Rogel Garcia
  • 1,895
  • 14
  • 16