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());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.