Jackson学习
Posted zh1164
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jackson学习相关的知识,希望对你有一定的参考价值。
Jackson 是一个能够将java对象序列化为JSON字符串,也能够将JSON字符串反序列化为java对象的框架。
无论是序列化还是反序列化,Jackson都提供了三种方式:
1. JSON <--> Java Object
2. JSON <--> JsonNode Tree(类似于XML的DOM树)
3. JSON <--> Json Stream (这是一个低层次的api,很强大,但是很繁琐)
Jackson提供了很多有用的注解来定制序列化,但是我们完全不用它的注解也可以完成绝大多数的工作。下面就从上面三种方式来一一介绍。
JSON <--> Java Object
下面的Person类是一个普通的java POJO。它含有基本类型(包括String、Date)和一些集合类型,以及一个自定义的Address类型。
import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class Person { private String name; private int age; public Date birth; private Address address; private List<String> friends = new ArrayList<>(); public Map<String, String> info = new HashMap<>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getFriends() { return friends; } public void setFriends(List<String> friends) { this.friends = friends; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } //注意这个默认构造器,如果没有默认的构造器,应该有一个@JsonCreator修饰的构造器 public Person(){} public Person(String name, int age, Address address, Date birth, String... friends){ this.name = name; this.age = age; this.address = address; this.birth = birth; this.friends.addAll(Arrays.asList(friends)); } @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("name: " + this.name + "\n"); sb.append("age: " + this.age + "\n"); sb.append("address: " + this.address + "\n"); sb.append("birth: " + this.birth + "\n"); this.friends.forEach(x -> sb.append("friend:"+ x + "\n")); return sb.toString(); } }
public class Address { public String homeAddress; public String workAddress; //跟Person一样,我们也必须提供一个无参的默认构造器 public Address(){} public Address(String homeAddress, String workAddress) { this.homeAddress = homeAddress; this.workAddress = workAddress; } @Override public String toString() { return "home:" + this.homeAddress + " " + "work:" + this.workAddress; } }
下面我们使用Jackson来(反)序列化 这个Person对象。
序列化:
import java.io.File; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class SerializeTest { public static void main(String[] args) throws ParseException, JsonGenerationException, JsonMappingException, IOException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date birth = format.parse("2010-10-10"); Address address = new Address("New York", "Tokyo"); Person person = new Person("zhangsan", 11, address, birth, "weiying", "caifang"); person.info.put("height", "175cm"); person.info.put("weight", "80kg"); //使用ObjectMapper来序列化和反序列化 ObjectMapper mapper = new ObjectMapper(); //配置序列化的输出缩进 mapper.configure(SerializationFeature.INDENT_OUTPUT, true); //如果没有DateFormat,ObjectMapper将会把Date类型序列化为毫秒数 mapper.setDateFormat(format); //按照map的key的自然排序来产生序列化结果 mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); //序列化的过程就这一行代码,当然也可以选择输出到文件或其他流中 mapper.writeValue(new File("person.json"), person); } }
反序列化:
import java.io.File; import java.io.IOException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class DeserializeTest { public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { //反序列化同样从ObjectMapper开始 ObjectMapper mapper = new ObjectMapper(); //配置在反序列化过程中如果json字符串中存在无法匹配的属性不会失败 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //上面的configure(xxx,false) 等同于disable(xxx),例如下面这行和上面作用是一样的。 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); //反序列化的过程也仅仅只有一行代码,同样可以从文件或其他流等输入中进行反序列化 Person person = mapper.readValue(new File("person.json"), Person.class); System.out.println(person); } }
以上是关于Jackson学习的主要内容,如果未能解决你的问题,请参考以下文章