How to map the following json to a classB object using Jackson
JSON Input
{"pattern":3,"graphs":4}
Class A
class ClassA{
String name;
int count;
}
Class B
class ClassB{
Set<ClassA> data;
}
How to map the following json to a classB object using Jackson
JSON Input
{"pattern":3,"graphs":4}
Class A
class ClassA{
String name;
int count;
}
Class B
class ClassB{
Set<ClassA> data;
}
Assuming you have a constructor in ClassA
as follows:
class ClassA {
String name;
int count;
public ClassA(String name, int count) {
this.name = name;
this.count = count;
}
}
You can use @JsonCreator
to fine tune the constructor or factory method used in the deserialization as follows:
class ClassB {
Set<ClassA> data;
@JsonCreator
public ClassB(Map<String, Integer> data){
this.data = data.entrySet().stream()
.map(entry -> new ClassA(entry.getKey(), entry.getValue()))
.collect(Collectors.toSet());
}
}