简体   繁体   English

gson无法反序列化简单界面

[英]gson fails in deserializing simple interface

I have a very simple Java Interface: 我有一个非常简单的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);
}

with a corresponding simple implementation: 具有相应的简单实现:

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

I do not have their source code: I received them in a Jar that I need to use. 我没有它们的源代码:我将它们放在需要使用的Jar中。

In order to deserialize the java interfaces I receive, I built an InstanceCreator : 为了反序列化我收到的Java接口,我构建了一个InstanceCreator

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

and register it with: 并注册到:

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

However, gson fails in deserializing the data I receive: it gives no errors, but all the fields are left empty/null. 但是,gson无法反序列化我收到的数据:它没有给出错误,但是所有字段均保留为空/空。

I am not an expert in Gson, but from the documentation I have read I did what I was supposed to do. 我不是Gson的专家,但是从阅读的文档中我已经做了我应该做的事情。

What did I do wrong? 我做错了什么?

Have you read these: 1) http://www.javacodegeeks.com/2012/04/json-with-gson-and-abstract-classes.html 2) Gson deserializing nested objects with InstanceCreator ? 您是否已阅读以下内容:1) http://www.javacodegeeks.com/2012/04/json-with-gson-and-abstract-classes.html 2) Gson使用InstanceCreator反序列化嵌套对象

The error, apparently, could be in this line: 显然,该错误可能在以下行中:

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

use the Implementation not the Interface and in InstanceCreatorGeographicalArea use this method: public GeographicalAreaImpl createInstance(Type arg0) { return new GeographicalAreaImpl(); 使用实现而不是接口,并在InstanceCreatorGeographicalArea中使用此方法:public GeographicalAreaImpl createInstance(Type arg0){return new GeographicalAreaImpl(); } }

Test class 测试班

    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());
}

Output: 输出:

Latitude 25.0    

InstanceCreatorGeographicalArea class:: InstanceCreatorGeographicalArea类:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM