Вы можете написать собственный десериализатор или использовать аннотацию @JsonAnySetter
.Вы можете сделать это следующим образом:
- Десериализация
JSON
в середину POJO
структура, которая использует @JsonAnySetter
аннотацию - Преобразование средней
POJO
структуры в Map
- Преобразование
Map
в место назначения POJO
структура.
Десериализационная часть и преобразование в Map
может выглядеть следующим образом:
class Fields {
private Field[] fields;
public Field[] getFields() {
return fields;
}
public void setFields(Field[] fields) {
this.fields = fields;
}
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
for (Field field : fields) {
map.put(field.getTypeName(), field.getFieldValue().resolve());
}
return map;
}
@Override
public String toString() {
return "Fields{" +
"fields=" + Arrays.toString(fields) +
'}';
}
}
class Field {
private String typeName;
private FieldValue fieldValue;
@JsonAnySetter
private void setValue(String propertyName, Object value) {
FieldValueBuilder builder = new FieldValueBuilder();
if (builder.accept(propertyName)) {
this.fieldValue = builder.build(propertyName, value);
}
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public FieldValue getFieldValue() {
return fieldValue;
}
public void setFieldValue(FieldValue fieldValue) {
this.fieldValue = fieldValue;
}
@Override
public String toString() {
return "Field{" +
"typeName='" + typeName + '\'' +
", fieldValue=" + fieldValue +
'}';
}
}
class FieldValueBuilder {
private List<String> ignoreFields = Arrays.asList("multiple", "typeClass");
public boolean accept(String propertyName) {
return !ignoreFields.contains(propertyName);
}
public FieldValue build(String propertyName, Object value) {
if (value instanceof String) {
return new StringFieldValue(value.toString());
}
if (value instanceof List) {
return deserialiseList((List) value);
}
System.out.println("Need to parse: key = " + propertyName + ", value = " + value);
return null;
}
private FieldValue deserialiseList(List list) {
if (list.isEmpty()) {
return null;
}
// It is a tricky part. From example it looks like that value is always a single-element-array.
// If not, handle it.
Object item = list.get(0);
if (item instanceof String) {
return new StringFieldValue(item.toString());
} else if (item instanceof Map) {
List<Field> fields = new ArrayList<>();
Map<String, Object> map = (Map<String, Object>) item;
for (Object valueItem : map.values()) {
if (valueItem instanceof Map) {
Map<String, Object> mapItem = (Map<String, Object>) valueItem;
Field field = new Field();
field.setTypeName(mapItem.get("typeName").toString());
field.setFieldValue(build("value", mapItem.get("value")));
fields.add(field);
}
}
return new ListFieldValues(fields);
} else {
System.out.println(item);
}
return new NullFieldValue();
}
}
interface FieldValue {
Object resolve();
}
class StringFieldValue implements FieldValue {
private final String value;
public StringFieldValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public Object resolve() {
return value;
}
@Override
public String toString() {
return "StringFieldValue{" +
"value='" + value + '\'' +
'}';
}
}
class ListFieldValues implements FieldValue {
private final List<Field> fields;
public ListFieldValues(List<Field> fields) {
this.fields = fields;
}
public List<Field> getFields() {
return fields;
}
@Override
public Object resolve() {
Map<String, Object> map = new HashMap<>();
for (Field field : fields) {
map.put(field.getTypeName(), field.getFieldValue().resolve());
}
return map;
}
@Override
public String toString() {
return "ListFieldValues{" +
"fields=" + fields +
'}';
}
}
class NullFieldValue implements FieldValue {
@Override
public Object resolve() {
return null;
}
}
Final POJO
структура может выглядеть следующим образом:
class Book {
private String subject;
private Author author;
private Description dsDescription;
private String dateOfDeposit;
private String depositor;
private String title;
private Contact datasetContact;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Description getDsDescription() {
return dsDescription;
}
public void setDsDescription(Description dsDescription) {
this.dsDescription = dsDescription;
}
public String getDateOfDeposit() {
return dateOfDeposit;
}
public void setDateOfDeposit(String dateOfDeposit) {
this.dateOfDeposit = dateOfDeposit;
}
public String getDepositor() {
return depositor;
}
public void setDepositor(String depositor) {
this.depositor = depositor;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Contact getDatasetContact() {
return datasetContact;
}
public void setDatasetContact(Contact datasetContact) {
this.datasetContact = datasetContact;
}
@Override
public String toString() {
return "Book{" +
"subject='" + subject + '\'' +
", author=" + author +
", dsDescription=" + dsDescription +
", dateOfDeposit='" + dateOfDeposit + '\'' +
", depositor='" + depositor + '\'' +
", title='" + title + '\'' +
", datasetContact=" + datasetContact +
'}';
}
}
class Author {
private String authorName;
private String authorAffiliation;
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getAuthorAffiliation() {
return authorAffiliation;
}
public void setAuthorAffiliation(String authorAffiliation) {
this.authorAffiliation = authorAffiliation;
}
@Override
public String toString() {
return "Author{" +
"authorName='" + authorName + '\'' +
", authorAffiliation='" + authorAffiliation + '\'' +
'}';
}
}
class Description {
private String dsDescriptionValue;
public String getDsDescriptionValue() {
return dsDescriptionValue;
}
public void setDsDescriptionValue(String dsDescriptionValue) {
this.dsDescriptionValue = dsDescriptionValue;
}
@Override
public String toString() {
return "Description{" +
"dsDescriptionValue='" + dsDescriptionValue + '\'' +
'}';
}
}
class Contact {
private String datasetContactEmail;
private String datasetContactAffiliation;
private String datasetContactName;
public String getDatasetContactEmail() {
return datasetContactEmail;
}
public void setDatasetContactEmail(String datasetContactEmail) {
this.datasetContactEmail = datasetContactEmail;
}
public String getDatasetContactAffiliation() {
return datasetContactAffiliation;
}
public void setDatasetContactAffiliation(String datasetContactAffiliation) {
this.datasetContactAffiliation = datasetContactAffiliation;
}
public String getDatasetContactName() {
return datasetContactName;
}
public void setDatasetContactName(String datasetContactName) {
this.datasetContactName = datasetContactName;
}
@Override
public String toString() {
return "Contact{" +
"datasetContactEmail='" + datasetContactEmail + '\'' +
", datasetContactAffiliation='" + datasetContactAffiliation + '\'' +
", datasetContactName='" + datasetContactName + '\'' +
'}';
}
}
Пример использования кода выше:
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonTest {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Fields fields = mapper.readValue(jsonFile, Fields.class);
System.out.println(fields);
Map<String, Object> map = fields.toMap();
System.out.println(map);
System.out.println(mapper.convertValue(map, Book.class));
}
}
Печать над кодом:
Fields{fields=[Field{typeName='title', fieldValue=StringFieldValue{value='Shapefile Dataset'}}, Field{typeName='author', fieldValue=ListFieldValues{fields=[Field{typeName='authorName', fieldValue=StringFieldValue{value='Quigley, Elizabeth'}}, Field{typeName='authorAffiliation', fieldValue=StringFieldValue{value='Harvard University'}}]}}, Field{typeName='datasetContact', fieldValue=ListFieldValues{fields=[Field{typeName='datasetContactName', fieldValue=StringFieldValue{value='Quigley, Elizabeth'}}, Field{typeName='datasetContactAffiliation', fieldValue=StringFieldValue{value='Harvard University'}}, Field{typeName='datasetContactEmail', fieldValue=StringFieldValue{value='equigley@iq.harvard.edu'}}]}}, Field{typeName='dsDescription', fieldValue=ListFieldValues{fields=[Field{typeName='dsDescriptionValue', fieldValue=StringFieldValue{value='Dataset for shapefile'}}]}}, Field{typeName='subject', fieldValue=StringFieldValue{value='Earth and Environmental Sciences'}}, Field{typeName='depositor', fieldValue=StringFieldValue{value='Quigley, Elizabeth'}}, Field{typeName='dateOfDeposit', fieldValue=StringFieldValue{value='2015-07-13'}}]}
{author={authorName=Quigley, Elizabeth, authorAffiliation=Harvard University}, subject=Earth and Environmental Sciences, dsDescription={dsDescriptionValue=Dataset for shapefile}, dateOfDeposit=2015-07-13, depositor=Quigley, Elizabeth, title=Shapefile Dataset, datasetContact={datasetContactEmail=equigley@iq.harvard.edu, datasetContactAffiliation=Harvard University, datasetContactName=Quigley, Elizabeth}}
Book{subject='Earth and Environmental Sciences', author=Author{authorName='Quigley, Elizabeth', authorAffiliation='Harvard University'}, dsDescription=Description{dsDescriptionValue='Dataset for shapefile'}, dateOfDeposit='2015-07-13', depositor='Quigley, Elizabeth', title='Shapefile Dataset', datasetContact=Contact{datasetContactEmail='equigley@iq.harvard.edu', datasetContactAffiliation='Harvard University', datasetContactName='Quigley, Elizabeth'}}