在完成将Java对象转换为JSON的确切任务之前,我已经在下面的示例中进行了测试。
Converting Java objects to JSON with Jackson
我一直在寻找更好的性能(转换时间应该非常少)。
This article显示了来自this answer的不同API之间的效果统计信息。
我的发现是例如我提到的第一个链接(记录很少):
TabbedPannelItem
我将使用杰克逊转换此 ValueData object = new ValueData();
List<ValueItems> information = new ArrayList<ValueItems>();
ValueItems v1 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat1", 1, "data1");
ValueItems v2 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat2", 2, "data2");
ValueItems v3 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat3", 3, "data3");
ValueItems v4 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat4", 4, "data4");
ValueItems v5 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat5", 5, "data5");
ValueItems v6 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat6", 6, "data6");
ValueItems v7 = new ValueItems(String.valueOf(Calendar.getInstance().getTimeInMillis()), "feat7", 7, "data7");
information.add(v1);
information.add(v2);
information.add(v3);
information.add(v4);
information.add(v5);
information.add(v6);
information.add(v7);
object.setInformation(information);
:
object
现在我正在使用 long smili = Calendar.getInstance().getTimeInMillis();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
long emili = Calendar.getInstance().getTimeInMillis();
System.out.println("taken time using jackson = " + (emili - smili) + " milli seconds");
:
StringBuilder
我得到了下面给出的时间表 smili = Calendar.getInstance().getTimeInMillis();
StringBuilder sb = new StringBuilder();
sb.append("{\n\"information\" : [\n");
for (ValueItems vi : object.getInformation()) {
sb.append("{\n\"timestamp\" : \""+vi.getTimestamp()+"\",");
sb.append("\"feature\" : \""+vi.getFeature()+"\",");
sb.append("\"ean\" : "+vi.getEan()+",");
sb.append("\"data\" : \""+vi.getData()+"\"\n},");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]\n}");
emili = Calendar.getInstance().getTimeInMillis();
System.out.println("taken time using StringBuilder = " + (emili - smili) + " milli seconds");
7:
size
我希望转换taken time using jackson = 534 milli seconds
taken time using StringBuilder = 1 milli seconds
大小超过10k的对象,但时间应该更短。
information list
创建JSON购买会有所帮助吗?请帮我解决这个问题。
答案 0 :(得分:4)
感谢Sam B.
我尝试过jakson-afterburner:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new AfterburnerModule());
ow = mapper.writer().withDefaultPrettyPrinter();
json = ow.writeValueAsString(object);
我已经测试了列表大小7,7000,70000和700000:
时间是:7:
taken time using jackson = 217 milli seconds
taken time using StringBuilder = 1 milli seconds
taken time using after-burner = 25 milli seconds
7000:
taken time using jackson = 310 milli seconds
taken time using StringBuilder = 31 milli seconds
taken time using after-burner = 65 milli seconds
对于70000:
taken time using jackson = 469 milli seconds
taken time using StringBuilder = 149 milli seconds
taken time using after-burner = 101 milli seconds
对于700000:
taken time using jackson = 1120 milli seconds
taken time using StringBuilder = 705 milli seconds
taken time using after-burner = 623 milli seconds
当列表大小增加时,加力燃烧室效率很高。