Serializacja GSON bardzo wolno
Próbuję serializować tablicę 7000 POJO przy użyciu GSON, a czas serializacji jest bardzo wolny. Szeregowanie tablicy następującego obiektu to 3-5 sekund:
public class Case {
private Long caseId;
private Key<Organization> orgKey;
private Key<Workflow> workflowKey;
private Key<User> creatorKey;
private Date creationTimestamp;
private Date lastUpdatedTimestamp;
private String name;
private String stage;
private String notes;
}
Pola klucza są serializowane przy użyciu niestandardowego serializatora / deserializatora:
public class GsonKeySerializerDeserializer implements JsonSerializer<Key<?>>, JsonDeserializer<Key<?>>{
@Override
public JsonElement serialize(Key<?> src, Type typeOfSrc, JsonSerializationContext arg2) {
return new JsonPrimitive(src.getString());
}
@Override
public Key<?> deserialize(JsonElement src, Type typeOfSrc, JsonDeserializationContext arg2) throws JsonParseException {
if (src.isJsonNull() || src.getAsString().isEmpty()) {
return null;
}
String s = src.getAsString();
com.google.appengine.api.datastore.Key k = KeyFactory.stringToKey(s);
return new Key(k);
}
}
Aby przetestować wydajność przeciwko ręcznemu pisaniu serializatora JSON, przetestowałem poniższy kod i mógł on serializować tę samą tablicę obiektów Case około 10 razy szybciej niż GSON.
List<Case> cases = (List<Case>) retVal;
JSONArray a = new JSONArray();
for (Case c : cases) {
JSONObject o = new JSONObject();
o.put("caseId", c.getCaseId());
o.put("orgKey", c.getOrgKey().getString());
o.put("workflowKey", c.getWorkflowKey().getString());
o.put("creatorKey", c.getCreatorKey().getString());
o.put("creationTimestamp", c.getCreationTimestamp().getTime());
o.put("lastUpdatedTimestamp", c.getLastUpdatedTimestamp().getTime());
o.put("name", c.getName());
o.put("stage", c.getStage());
o.put("notes", c.getNotes());
a.put(o);
}
String json = a.toString();
Jakieś pomysły, dlaczego GSON działa tak źle w tym przypadku?
AKTUALIZACJA
Oto kod, który faktycznie rozpoczyna serializację:
Object retVal = someFunctionThatReturnsAList();
String json = g.toJson(retVal);
resp.getWriter().print(json);
UPDATE2
Oto bardzo prosty przypadek testowy ilustrujący słabą wydajność w stosunku do org.json:
List<Foo> list = new ArrayList<Foo>();
for (int i = 0; i < 7001; i++) {
Foo f = new Foo();
f.id = new Long(i);
list.add(f);
}
Gson gs = new Gson();
long start = System.currentTimeMillis();
String s = gs.toJson(list);
System.out.println("Serialization time using Gson: " + ((double) (System.currentTimeMillis() - start) / 1000));
start = System.currentTimeMillis();
JSONArray a = new JSONArray();
for (Foo f : list) {
JSONObject o = new JSONObject();
o.put("id", f.id);
a.put(o);
}
String json = a.toString();
System.out.println("Serialization time using org.json: " + ((double) (System.currentTimeMillis() - start) / 1000));
System.out.println(json.equals(s));
Gdzie jest Foo:
public class Foo {
public Long id;
}
Ten sygnał wyjściowy:
Serialization time using Gson: 0.233
Serialization time using org.json: 0.028
true
Prawie 10-krotna różnica w wydajności!