Multiple Registration of TypeAdapter issue (2.2.4)

730 views
Skip to first unread message

David Pipes

unread,
Jul 31, 2013, 8:40:43 PM7/31/13
to googl...@googlegroups.com
Zip.java
public class Zip {
private String zipCode;
private String zipCodeType;
private String city;
private String state;
private String locationType;
private Double latitude;
private Double longitude;
private String location;
private Boolean decommisioned;
private Long taxReturnsFiled = Long.valueOf(0);
private Long estimatedPopulation = Long.valueOf(0);
private Long totalWages = Long.valueOf(0);
private Long avgWagePerPerson = Long.valueOf(0);
private Long avgWagePerTaxReturn = Long.valueOf(0);
}

ZipSerializer.java
public class ZipSerializer implements JsonSerializer<Zip>{
@Override
public JsonElement serialize(Zip obj, Type type, JsonSerializationContext jsc) {
Gson gson = new Gson();
                JsonObject jObj = (JsonObject)gson.toJsonTree(obj);
                jObj.remove("taxReturnsFiled");
                jObj.remove("estimatedPopulation");
                jObj.remove("totalWages");
                jObj.remove("avgWagePerPerson");
                jObj.remove("avgWagePerTaxReturn");        
                return jObj;
}
}

JsonResponse.java
public class JsonResponse {
private JsonRequest request = null;
private String jsonrpc = "2.0";
private Object result = null;
private JsonError error = null;
private String id = null;
}

JsonResponseSerializer.java
public class JsonResponseSerializer implements JsonSerializer<JsonResponse> {
@Override
public JsonElement serialize(JsonResponse obj, Type type, JsonSerializationContext jsc) {
Gson gson = new Gson();
                JsonObject jObj = (JsonObject)gson.toJsonTree(obj);
                jObj.remove("request");
        
                if(obj.getError() != null) {
                     jObj.remove("result");
                } else {
             jObj.remove("error");
                }        
                return jObj;
}
}

This is some shorthand code representing my 2 objects and their serializers. When I run the following code trying to register both adapters  it seems to only use the jsonresponseserializer and never takes into consideration of the zipserializer unless it's the only one define. Am I doing something wrong here?

// Configure GSON
Gson gsonResponse = new GsonBuilder()
.registerTypeAdapter(Zip.class, new ZipSerializer())
            .registerTypeAdapter(JsonResponse.class, new JsonResponseSerializer())    
            .serializeNulls()
            .setPrettyPrinting()
            .create();

Hari Krishnan

unread,
Aug 3, 2013, 4:17:57 PM8/3/13
to googl...@googlegroups.com
I don't think so.  This is what I did to test it.  I was testing it with 2.2.4 on windows 7.

================
Gson gsonResponse = new GsonBuilder()
.registerTypeAdapter(Zip.class, new ZipSerializer())
.registerTypeAdapter(JsonResponse.class, new JsonResponseSerializer())    
.serializeNulls()
.setPrettyPrinting()
.create();

Zip z = new Zip();
String s = gsonResponse.toJson( z);
System.out.println( s );

and this is the output
{
  "zipCode": null,
  "zipCodeType": null,
  "city": null,
  "state": null,
  "locationType": null,
  "latitude": null,
  "longitude": null,
  "location": null,
  "decommisioned": null
}

I had to change Zipserializer to serialize nulls.
 gson = new GsonBuilder().serializeNulls().create();
instead of 
gson = new Gson();
Reply all
Reply to author
Forward
0 new messages