I have a very simple Java Interface:
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.
In order to deserialize the java interfaces I receive, I built an 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.
I am not an expert in Gson, but from the documentation I have read I did what I was supposed to do.
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 ?
The error, apparently, could be in this line:
builder.registerTypeAdapter(GeographicalAreaImpl.class, new ......
use the Implementation not the Interface and in InstanceCreatorGeographicalArea use this method: 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::
public class InstanceCreatorGeographicalArea implements InstanceCreator<GeographicalAreaImpl> {
@Override
public GeographicalAreaImpl createInstance(Type arg0) {
return new GeographicalAreaImpl();
}
}
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.