Jackson实现序列化和反序列化

编程入门 行业动态 更新时间:2024-10-13 08:21:35

Jackson实现<a href=https://www.elefans.com/category/jswz/34/1771358.html style=序列化和反序列化"/>

Jackson实现序列化和反序列化

简介

通俗的来说,Jackson是一个 Java 用来处理 JSON 格式数据的类库,其性能非常好。Jackson具有比较高的序列化和反序列化效率,据测试,无论是哪种形式的转换,Jackson > Gson > Json-lib,而且Jackson的处理能力甚至高出Json-lib近10倍左右,且正确性也十分高。

使用

Jackson提供了很多类和方法,而在序列化和反序列化中使用的最多的类则是ObjectMapper这个类,此类比较类似于Json-lib中JsonObject和ArrayObject。此类中提供了readTree(),readValue(),writeValueAsString()等方法用于转换。

1 常用注解

1、 @JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把testPwd属性序列化为pwd,@JsonProperty(value="pwd")。

2、@JsonPropertyOrder
作用在类上,被用来指明当序列化时需要对属性做排序,它有2个属性一个是alphabetic:布尔类型,表示是否采用字母拼音顺序排序,默认是为false,即不排序。如@JsonPropertyOrder(alphabetic=true)。

3、@JsonInclude
是用在实体类的方法类的头上 作用是实体类的参数查询到的为null的不显示,比如说你想传一些json数据到前台,但是不想传值为null的数据,就可以使用该标签。如@JsonInclude(JsonInclude.Include.NON_NULL)

4、@JsonIgnoreProperties
可以注明是想要忽略的属性列表如@JsonIgnoreProperties({"name","age","title"}),也可以注明过滤掉未知的属性如@JsonIgnoreProperties(ignoreUnknown=true),@JsonIgnore表示忽略当前属性。

5、@JsonFormat
用在属性和方法上,可以方便的进行格式转换,如把Date转换为我们要的模式@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。

6、@JsonUnwrapped
当实体类中成员属性是一个类的对象时候,忽略包装。直接显示属性。

2 序列化
/*** 测试用户类** @author 杨小华* @create 2017-09-27 9:39*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(alphabetic=true)
public class User{/*** id
   * 不JSON序列化id */@JsonIgnoreprivate Integer id;/*** 昵称*/private String name;/*** 密码* 序列化testPwd属性为pwd*/@JsonProperty("pwd")private String testPwd;/*** 注册时间* 格式化日期属性  */@JsonFormat(pattern = "yyyy-MM-dd")private Date time;
2.1 对自定义类的序列化
/*** @desc JAVA对象和集合转JSON[JSON序列化]* @author 杨小华* @create 2017/9/27 10:15**/@Testpublic void write() {User user = new User();user.setId(1);user.setName("测试");user.setTestPwd("123456");user.setTime(new Date());List<String> jsonList = new ArrayList<String>();jsonList.add("王大锤");jsonList.add("王尼玛");ObjectMapper objectMapper = new ObjectMapper();try {String json = objectMapper.writeValueAsString(user);System.out.printf(json);String list = objectMapper.writeValueAsString(jsonList);System.out.printf(list);} catch (JsonProcessingException e) {e.printStackTrace();}}输出结果:
{"name":"测试","time":"2017-09-27","pwd":"123456"}
["大锤","二锤"]
2.2 另一种序列化方法

objectMapper.getFactory().createGenerator()此方法不仅可以将Json直接写入网络流,还可以将Json写入文件流或者内存流。所以用途更广。

/*** @desc 另一种序列化方法* @author 杨小华* @create 2017/9/27 10:30**/
@Test
public void jsonGenerator() {ObjectMapper objectMapper = new ObjectMapper();User user = new User();user.setId(1);user.setName("测试");user.setTestPwd("123456");user.setTime(new Date());try {JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(System.out, JsonEncoding.UTF8);jsonGenerator.writeObject(user);} catch (IOException e) {e.printStackTrace();}
}输出结果:{"name":"测试","pwd":"123456","time":"2017-09-27"}
3 反序列化
3.1 一次性反序列化

此方法中主要利用ObjectMapper提供的<T> readValue(String content, Class<T> valueType)方法。此方法需要输入Json串以及对应的需要填充的类的Class,返回填充后的类。将Json串解析到自定义类中。

*** @desc JSON转Java类[JSON反序列化]* @author 杨小华* @create 2017/9/27 10:17**/
@Test
public void read() {String json = "{\"name\":\"测试\",\"time\":\"2017-09-27\",\"pwd\":\"123456\"}";ObjectMapper objectMapper = new ObjectMapper();try {User user = objectMapper.readValue(json, User.class);System.out.printf(user.getName());} catch (IOException e) {e.printStackTrace();}
}输出结果:
测试/*** @desc JSON转Map类[JSON反序列化]* @author 杨小华* @create 2017/9/27 10:43**/
@Test
public void jsonMap() {ObjectMapper objectMapper = new ObjectMapper();String json = "{\"error\":0,\"data\":{\"name\":\"ABC\",\"age\":20,\"phone\":{\"home\":\"abc\",\"mobile\":\"def\"},\"friends\":[{\"name\":\"DEF\",\"phone\":{\"home\":\"hij\",\"mobile\":\"klm\"}}," +"{\"name\":\"GHI\",\"phone\":{\"home\":\"nop\",\"mobile\":\"qrs\"}}]},\"other\":{\"nickname\":[]}}";try {Map<String, Map<String, Object>> map = objectMapper.readValue(json, Map.class);System.out.printf(map.get("error") + "\n");System.out.printf(map.get("data").get("phone") + "");} catch (IOException e) {e.printStackTrace();}
}输出结果:
0
{home=abc, mobile=def}

3.2 渐次反序列化

此方法更灵活,可以只将用户感兴趣的Json串信息值提取出来。主要利用ObjectMapper提供的readTree和Jackson提供的JsonNode类来实现(此方法类似于XML解析中的DOM方式解析,其好处是结构明细,便于提取想要的信息。当然,其缺点也一样:耗时费空间)。

/*** @desc 渐次反序列化* @author 杨小华* @create 2017/9/27 11:04**/
@Test
public void jsonTree() {ObjectMapper objectMapper = new ObjectMapper();String test = "{\"results\":[{\"objectID\":357,\"geoPoints\":[{\"x\":504604.59802246094,\"y\":305569.9150390625}]},{\"objectID\":358,\"geoPoints\":[{\"x\":504602.2680053711,\"y\":305554.43603515625}]}]}";try {JsonNode jsonNode = objectMapper.readTree(test);//将Json串以树状结构读入内存JsonNode contents = jsonNode.get("results");//得到results这个节点下的信息for (int i = 0; i < contents.size(); i++) {System.out.printf(contents.get(i).get("objectID").intValue() + "\n");JsonNode geoContent = contents.get(i).get("geoPoints");//得到geoPoints这个节点下的信息for (int j = 0; j < geoContent.size(); j++) {System.out.printf(geoContent.get(j).get("x").doubleValue() + "\n");System.out.printf(geoContent.get(j).get("y").decimalValue() + "\n");}}} catch (IOException e) {e.printStackTrace();}
}

总结

Jackson关于Json的操作主要如上所示,其方法使用起来很便利,而且也很灵活,即提供了一次性完成的操作,也提供了可以按需读取信息的操作。并且Jackson的功能很齐全,可以对序列化和反序列化进行多种细节的控制,例如注解功能和对于Hibernate的延迟注入功能以及设置时间格式功能等,因为这些功能目前不太需要,所以仔细研究留待以后。同时,Jackson还支持对XML的一系列序列化和反序列化的操作,其思路与解析Json的大致相同。
对于Jackson目前的缺点,网上有人测试所比Json-lib更占内存一些。而利用空间换时间,一般是值得的。

更多推荐

Jackson实现序列化和反序列化

本文发布于:2024-03-05 23:49:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   Jackson

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!