【发布时间】:2012-05-28 11:01:55
【问题描述】:
我正在尝试使用 GSON 序列化一个包含 7000 个 POJO 的数组,并且序列化时间非常慢。序列化以下对象的数组大约需要 3-5 秒:
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;
}
使用自定义序列化器/反序列化器对关键字段进行序列化:
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);
}
}
为了测试手动编写 JSON 序列化程序的性能,我测试了以下代码,它序列化相同的 Case 对象数组的速度比 GSON 快大约 10 倍。
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();
你知道为什么 GSON 在这种情况下表现如此糟糕吗?
更新
下面是实际开始序列化的代码:
Object retVal = someFunctionThatReturnsAList();
String json = g.toJson(retVal);
resp.getWriter().print(json);
更新2
这是一个非常简单的测试用例,说明了相对于 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));
Foo 在哪里:
public class Foo {
public Long id;
}
这个输出:
Serialization time using Gson: 0.233
Serialization time using org.json: 0.028
true
几乎 10 倍的性能差异!
【问题讨论】:
-
您使用的是哪个 gson 版本以及生成输出的代码是什么?
-
我正在使用 gson 2.2,用代码更新问题以产生输出
-
如何创建 Gson 对象?
-
GsonBuilder gson = new GsonBuilder(); gson.addSerializationExclusionStrategy(new GsonExclusionStrategy()); gson.registerTypeHierarchyAdapter(Key.class, new GsonKeySerializerDeserializer()); gson.registerTypeHierarchyAdapter(Date.class, new GsonDateSerializerDeserializer()); gson.registerTypeHierarchyAdapter(BlobKey.class, new GsonBlobKeySerializerDeserializer());返回 gson.create();
-
@aloo 是的,GSON 需要很长时间来初始化,我猜序列化/反序列化还可以,节省了编码时间,而不是运行时间,但是 GSON 的初始化很慢。您找到解决方案了吗?
标签: java json performance serialization gson