使用gson TypeAdapter将嵌套的JSON转换为嵌套的Java对象

我正在尝试使用Google gson TypeAdapter将嵌套的JSON转换为对每个类都具有TypeAdapter实现的嵌套Java对象.但是我不想在单个适配器类中编写完整的read()方法逻辑.我通过网络引用了几个问题和blog示例.但是完整的读取逻辑在单个类中.

对于小的嵌套对象,最好在单个Adapter中具有逻辑,但对于大的对象(每个类具有10-15个以上的字段),则不好.

[更新]

例如,json键看起来与类属性相同,但是实际上我将以连字符分隔的小写字母键而不是Camel的小写字母键来获取输入.因此,我的json和java类的属性名称将不同,因此我必须编写用于映射的自定义逻辑.

例如.
样本Json输入:

{
  "id": 1,
  "name": "Alex",
  "emailId": "alex@gmail.com",
  "address": {
    "address": "21ST & FAIRVIEW AVE",
    "district": "district",
    "city": "EATON",
    "region": "PA",
    "postalCode": "18044",
    "country": "US"
  }
}

Java bean如下:

//Employee object class
public class Employee {

  private int id;
  private String name;
  private String emailId;
  private Address address;
  ..
}

//Address object class
public class Address {

  private String address;
  private String district;
  private String city;
  private String region;
  private String postalCode;
  private String country;
  ..
}

我想拥有两个不同的适配器,并在read()方法中集成多个适配器.

public class EmployeeAdapter extends TypeAdapter<Employee> {
  @Override
  public void write(JsonWriter out, Employee employee) throws IOException {
    //
  }

  @Override
  public Employee read(JsonReader jsonReader) throws IOException {
    //read logic for employee class using AddressAdapter for address json
  }
}

public class AddressAdapter extends TypeAdapter<Address> {
  @Override
  public void write(JsonWriter out, Address address) throws IOException {
    //
  }

  @Override
  public Address read(JsonReader jsonReader) throws IOException {
    //read logic for Address class
  }
}

如何在EmployeeAdapter中使用AddressAdapter?

最佳答案
我遇到了同样的问题,并找到了适合我的解决方案.

您可以获得一个新的TypeAdapter< T>.实例借助Gson对象及其方法getAdapter(Class< T>类型).

因此,您提供的示例如下所示:

Java Bean:

//Employee object class
@JsonAdapter(EmployeeAdapter.class)
public class Employee {

  private int id;
  private String name;
  private String emailId;
  private Address address;
  ..
}

//Address object class
@JsonAdapter(AddressAdapter.class)
public class Address {

  private String address;
  private String district;
  private String city;
  private String region;
  private String postalCode;
  private String country;
  ..
}

类型适配器:

public class EmployeeAdapter extends TypeAdapter<Employee> {
  @Override
  public void write(JsonWriter out, Employee employee) throws IOException {
    //
  }

  @Override
  public Employee read(JsonReader jsonReader) throws IOException {
    Employee employee = new Employee();

    jsonReader.beginObject();
    //read your Employee fields

    TypeAdapter<Address> addressAdapter = new Gson().getAdapter(Address.class);
    employee.setAddress(addressAdapter.read(jsonReader);

    return employee;
  }
}

public class AddressAdapter extends TypeAdapter<Address> {
  @Override
  public void write(JsonWriter out, Address address) throws IOException {
    //
  }

  @Override
  public Address read(JsonReader jsonReader) throws IOException {
    Address address = new Address();
    //read your Address fields
    return address;
  }
}

使用此解决方案,由于Beans JsonAdapter批注中只有唯一的依赖项,因此可以享受松耦合代码的好处.
另外,您将每个Bean的读/写逻辑拆分为自己的TypeAdapter.

转载注明原文:使用gson TypeAdapter将嵌套的JSON转换为嵌套的Java对象 - 代码日志