I have tested below example before I do my exact task of converting Java Objects to JSON.
在完成将Java对象转换为JSON的确切任务之前,我已经在下面的示例中进行了测试。
Converting Java objects to JSON with Jackson
使用Jackson将Java对象转换为JSON
And I was looking for the better performance (Converting time should be very less).
我一直在寻找更好的性能(转换时间应该非常少)。
This article is showing the stats for the performance in between different APIs from this answer.
本文显示了此答案中不同API之间性能的统计信息。
My finding was for example with the first link that I mentioned (with few records):
我的发现是例如我提到的第一个链接(记录很少):
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);
And I'm going to convert this object
by using Jackson:
我将使用Jackson转换此对象:
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");
And now I'm doing by using StringBuilder
:
现在我正在使用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");
I got the timing as given below just for the list size
7:
我得到了下面给出的时间仅为列表大小7:
taken time using jackson = 534 milli seconds
taken time using StringBuilder = 1 milli seconds
I want to convert the object with the information list
size more than 10k but the time should be very less.
我想转换信息列表大小超过10k的对象,但时间应该非常少。
- Creating JSON buy using
StringBuilder
will help in this case? - Is there other API gives the facility that I require?
在这种情况下,使用StringBuilder创建JSON购买会有所帮助吗?
是否有其他API提供我需要的设施?
Please help me on this.
请帮帮我。
1 个解决方案
#1
4
Thanks Sam B.
谢谢Sam B.
I have tried with jakson-afterburner:
我试过jakson-afterburner:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new AfterburnerModule());
ow = mapper.writer().withDefaultPrettyPrinter();
json = ow.writeValueAsString(object);
And I have tested with list sizes 7, 7000, 70000 and 700000:
我已经测试了列表大小7,7000,70000和700000:
timing was:
For 7:
taken time using jackson = 217 milli seconds
taken time using StringBuilder = 1 milli seconds
taken time using after-burner = 25 milli seconds
For 7000:
taken time using jackson = 310 milli seconds
taken time using StringBuilder = 31 milli seconds
taken time using after-burner = 65 milli seconds
For 70000:
taken time using jackson = 469 milli seconds
taken time using StringBuilder = 149 milli seconds
taken time using after-burner = 101 milli seconds
For 700000:
taken time using jackson = 1120 milli seconds
taken time using StringBuilder = 705 milli seconds
taken time using after-burner = 623 milli seconds
When the list size increases, afterburner is efficient.
当列表大小增加时,加力燃烧室是有效的。
#1
4
Thanks Sam B.
谢谢Sam B.
I have tried with jakson-afterburner:
我试过jakson-afterburner:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new AfterburnerModule());
ow = mapper.writer().withDefaultPrettyPrinter();
json = ow.writeValueAsString(object);
And I have tested with list sizes 7, 7000, 70000 and 700000:
我已经测试了列表大小7,7000,70000和700000:
timing was:
For 7:
taken time using jackson = 217 milli seconds
taken time using StringBuilder = 1 milli seconds
taken time using after-burner = 25 milli seconds
For 7000:
taken time using jackson = 310 milli seconds
taken time using StringBuilder = 31 milli seconds
taken time using after-burner = 65 milli seconds
For 70000:
taken time using jackson = 469 milli seconds
taken time using StringBuilder = 149 milli seconds
taken time using after-burner = 101 milli seconds
For 700000:
taken time using jackson = 1120 milli seconds
taken time using StringBuilder = 705 milli seconds
taken time using after-burner = 623 milli seconds
When the list size increases, afterburner is efficient.
当列表大小增加时,加力燃烧室是有效的。