前言
目前的主要使用的库有三个:Jackson、Gson、Fastjson
作为 Spring MVC / SpringBoot 技术栈 开发的话,用默认的Jackson
是最好的!
Jackson
SpringBoot2 中的 web starter 中就内置了 jackson 三个jar依赖
目前最新版为 2.12.3 其中三个jar 各自大小为
- jackson-core 314Kb
- jackson-databind 1.3Mb
- jackson-annotations 65Kb
使用方式
Maven引入包:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-core</ artifactId > < version >2.12.3</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-databind</ artifactId > < version >2.12.3</ version > </ dependency > <!-- 注意:如没有用到注解去配置,可不用引入此依赖 --> < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-annotations</ artifactId > < version >2.12.3</ version > </ dependency > |
Java代码:
1 2 3 4 5 | //直接读取简单JSON List list = mapper.readValue(result, List. class ) //List转JSON ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeAsString(Arrays.asList( 1 , 2 , 3 )) |
Gson
目前最新版为 2.8.6 其jar大小为 235kb
使用方式
Maven引入包:
1 2 3 4 5 | < dependency > < groupId >com.google.code.gson</ groupId > < artifactId >gson</ artifactId > < version >2.8.6</ version > </ dependency > |
Java代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 例如转化一个类型不固定的List, 自定义类型同理 Gson gson = new Gson(); List origin = new ArrayList(); origin.add( 12 ); origin.add( "dfsf" ); System.out.println(gson.toJson(origin)); // 结果为: [12,"dfsf"] // 读取这个JSON数组 Gson gson = new Gson(); List result = gson.fromJson( "[12,\"dfsf\"]" , List. class ); for (Object o : result) { System.out.print(o.toString() + " " ); } // 结果为: 12.0 dfsf |
fastjson
目前最新版本为 1.2.76 jar包大小为 533kb
使用方式
Maven引入包:
1 2 3 4 5 | < dependency > < groupId >com.alibaba</ groupId > < artifactId >fastjson</ artifactId > < version >1.2.76</ version > </ dependency > |
性能测试对比
硬件配置
内存 8G 2400MHZ
CPU 8核 i7-7700
测试生成JSON
2000000 个 Person 对象,启动测试,直接吃掉 1.7G 内存
测试结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 第一次 >>>>>> GSON 耗时:<2581ms> 格式:<0h:0m:2s:581ms> <<<<<< >>>>>> Jackson 耗时:<1292ms> 格式:<0h:0m:1s:292ms> <<<<<< >>>>>> FastJson 耗时:<7237ms> 格式:<0h:0m:7s:237ms> <<<<<< 第二次 >>>>>> GSON 耗时:<2498ms> 格式:<0h:0m:2s:498ms> <<<<<< >>>>>> Jackson 耗时:<1316ms> 格式:<0h:0m:1s:316ms> <<<<<< >>>>>> FastJson 耗时:<7548ms> 格式:<0h:0m:7s:548ms> <<<<<< 第三次 >>>>>> GSON 耗时:<2465ms> 格式:<0h:0m:2s:465ms> <<<<<< >>>>>> Jackson 耗时:<1483ms> 格式:<0h:0m:1s:483ms> <<<<<< >>>>>> FastJson 耗时:<7432ms> 格式:<0h:0m:7s:432ms> <<<<<< |
2000 个对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | >>>>>> GSON 耗时:<51ms> 格式:<0h:0m:0s:51ms> <<<<<< >>>>>> Jackson 耗时:<176ms> 格式:<0h:0m:0s:176ms> <<<<<< >>>>>> FastJson 耗时:<126ms> 格式:<0h:0m:0s:126ms> <<<<<< >>>>>> GSON 耗时:<53ms> 格式:<0h:0m:0s:53ms> <<<<<< >>>>>> Jackson 耗时:<170ms> 格式:<0h:0m:0s:170ms> <<<<<< >>>>>> FastJson 耗时:<166ms> 格式:<0h:0m:0s:166ms> <<<<<< >>>>>> GSON 耗时:<57ms> 格式:<0h:0m:0s:57ms> <<<<<< >>>>>> Jackson 耗时:<178ms> 格式:<0h:0m:0s:178ms> <<<<<< >>>>>> FastJson 耗时:<129ms> 格式:<0h:0m:0s:129ms> <<<<<< >>>>>> GSON 耗时:<49ms> 格式:<0h:0m:0s:49ms> <<<<<< >>>>>> Jackson 耗时:<160ms> 格式:<0h:0m:0s:160ms> <<<<<< >>>>>> FastJson 耗时:<126ms> 格式:<0h:0m:0s:126ms> <<<<<< |
测试读取JSON
知乎: Fastjson这么快老外为啥还是热衷 Jackson?
Jackson:强项是灵活可定制,并且具有了一个生态,yaml 也能完美驾驭
Gson:轻量 简洁
Fastjson:似乎没有一个好的生态 , 性能也比较好