测试结果如下:
- Java对象转化成为json字符串耗时:1244ms
- 集合对象转化成为json字符串耗时:86041ms
- 字符串转化成json对象耗时:1009ms
- 字符串转化成java对象耗时:793ms
- 字符串转化为json数组耗时:62112ms
- 字符串转化成为java集合耗时:51087ms
Jackson测试
测试代码如下:
- public static void main(String[] args) throws Exception {
- ObjectMapper mapper = new ObjectMapper();
-
- // 构造对象
- Person person = new Person();
- person.setId(99999);
- person.setUsername("Happyjava");
- person.setAddress("广东省广州市,上海市,北京市,广东省深圳市,浙江省杭州市,");
- person.setAge(100);
-
- // Java对象转化成为json字符串
- long start = System.currentTimeMillis();
- for (int i = 0; i < 1000000; i++) {
- String string = mapper.writeValueAsString(person);
- }
- System.out.println("Java对象转化成为json字符串耗时:" + (System.currentTimeMillis() - start) + "ms");
-
- // 集合对象转化成为json字符串
- List<Person> list = new ArrayList<>();
- for (int i = 0; i < 100; i++) {
- list.add(person);
- }
- start = System.currentTimeMillis();
- for (int i = 0; i < 1000000; i++) {
- String string = mapper.writeValueAsString(list);
- }
- System.out.println("集合对象转化成为json字符串耗时:" + (System.currentTimeMillis() - start) + "ms");
-
- // 字符串转化成json对象
- start = System.currentTimeMillis();
- String jsonsString = mapper.writeValueAsString(person);
- for (int i = 0; i < 1000000; i++) {
- ObjectNode objectNode = mapper.readValue(jsonsString, ObjectNode.class);
- }
- System.out.println("字符串转化成json对象耗时:" + (System.currentTimeMillis() - start) + "ms");
-
- // 字符串转化成java对象
- start = System.currentTimeMillis();
- for (int i = 0; i < 1000000; i++) {
- Person person1 = mapper.readValue(jsonsString, Person.class);
- }
- System.out.println("字符串转化成java对象耗时:" + (System.currentTimeMillis() - start) + "ms");
-
- // 字符串转化为json数组
- start = System.currentTimeMillis();
- String jsonArrayStr = mapper.writeValueAsString(list);
- for (int i = 0; i < 1000000; i++) {
- ArrayNode arrayNode = mapper.readValue(jsonArrayStr, ArrayNode.class);
- }
- System.out.println("字符串转化为json数组耗时:" + (System.currentTimeMillis() - start) + "ms");
-
- // 字符串转化成为java集合
- start = System.currentTimeMillis();
- JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Person.class);
- for (int i = 0; i < 1000000; i++) {
- List<Person> list1 = mapper.readValue(jsonArrayStr, javaType);
- }
- System.out.println("字符串转化成为java集合耗时:" + (System.currentTimeMillis() - start) + "ms");
- }
-
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|