gson无法反序列化简单的接口

时间:2015-04-12 07:25:59

标签: java gson

我有一个非常简单的Java接口:

public interface GeographicalArea {
 java.lang.String getTown();
 java.lang.String getProvince();
 java.lang.String getStreet();
 java.lang.String getStreetNumber();
 float getLongitude();
 float getLatitude();
 java.lang.String getPhoneNumber();
 void setTown(java.lang.String s);
 void setProvince(java.lang.String s);
 void setStreet(java.lang.String s);
 void setStreetNumber(java.lang.String s);
 void setLongitude(float v);
 void setLatitude(float v);
 void setPhoneNumber(java.lang.String s);
}

带有相应的简单实现:

public class GeographicalAreaImpl implements GeographicalArea {
  private java.lang.String town;
  private java.lang.String province;
  private java.lang.String street;
  private java.lang.String streetNumber;
  private float longitude;
  private float latitude;
  private java.lang.String phoneNumber;

  public GeographicalAreaImpl() { ...  [compiled code]
  public java.lang.String getTown() { ... [compiled code]
 ... all the other getters and setters declared in the Interface

我没有他们的源代码:我在一个我需要使用的Jar中收到它们。

为了反序列化我收到的java接口,我构建了一个InstanceCreator

public class InstanceCreatorGeographicalArea implements 
   InstanceCreator<GeographicalArea> {
       @Override
       public GeographicalArea createInstance(Type type) {
          return new GeographicalAreaImpl();
       }
   }

并将其注册为:

 GsonBuilder builder = new GsonBuilder();
 builder.registerTypeAdapter(GeographicalArea.class, new InstanceCreatorGeographicalArea());
 ...
  Gson gson = builder.create();

但是,gson无法反序列化我收到的数据:它没有错误,但所有字段都留空/空。

我不是Gson的专家,但从我读过的文档中我做了我应该做的事。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你读过这些: 1)http://www.javacodegeeks.com/2012/04/json-with-gson-and-abstract-classes.html 2)Gson deserializing nested objects with InstanceCreator

显然,错误可能就在这一行:

  
    

builder.registerTypeAdapter(GeographicalAreaImpl.class,new ......

  

使用Implementation而不是Interface 并在InstanceCreatorGeographicalArea中使用此方法:     public GeographicalAreaImpl createInstance(Type arg0){         返回新的GeographicalAreaImpl();     }

测试课

    public static void main(String[] args) {
    GeographicalArea ga = new GeographicalAreaImpl();
    ga.setLatitude(25);
    ga.setLongitude(2);
    ga.setPhoneNumber("2344");
    ga.setProvince("xxx");
    ga.setStreet("yyyyy");
    ga.setStreetNumber("2");
    ga.setTown("zzzzzzz");
    Gson gson = new Gson();
    String userJson = gson.toJson(ga);
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(GeographicalAreaImpl.class, new InstanceCreatorGeographicalArea());
    Gson parser = builder.create();
    GeographicalArea request = parser.fromJson(userJson, GeographicalAreaImpl.class);
    System.out.println("Latitude " + request.getLatitude());
}

输出:

Latitude 25.0    

InstanceCreatorGeographicalArea类::

public class InstanceCreatorGeographicalArea implements InstanceCreator<GeographicalAreaImpl> {
        @Override
        public GeographicalAreaImpl createInstance(Type arg0) {
            return new GeographicalAreaImpl();
        }

}