java basic tutorialThe column introduces the Java JSON parser
Recommended (Free): Java Basics Tutorial
In today’s programming world, JSON has become the preferred protocol for transmitting information from the client to the server. It is no exaggeration to say that XML is the former wave that was photographed dead on the beach.
Unfortunately, JDK does not have a JSON library. I don’t know why I don’t work on it. At the time of Log4j, java.util.logging was also launched in order to compete, although in the end not many people used it.
The reason why Java is awesome is that its ecology is very complete. JDK does not have a JSON library, but third-party libraries do, which are quite good. For example, the pig's feet in this article - Jackson, GitHub star 6.1k, Spring Boot's default JSON parser.
How to prove this?
When we create a new Spring Boot Web project through starter, we can see Jackson in the Maven dependencies.
Jackson has many advantages:
Jackson's core module consists of three parts:
To use Jackson, you need to add Jackson dependencies in the pom.xml file.
<dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>2.10.1</version> </dependency>
jackson-databind depends on jackson-core and jackson-annotations, so after adding jackson-databind, Maven will automatically introduce jackson-core and jackson-annotations into the project.
The reason why Maven is so lovable is that it can secretly help us do what we need to do.
Jackson’s most commonly used API is ObjectMapper based on “object binding”. It serializes Java objects into JSON through a series of writeValue methods, and can store them in different format.
writeValueAsString(Object value)
method, stores the object as a string writeValueAsBytes(Object value)
method, stores the object Into a byte arraywriteValue(File resultFile, Object value)
method, store the object as a fileLet’s take a look at the code example of storing it as a string :
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * 微信搜索「沉默王二」,回复 Java * * @author 沉默王二 * @date 2020/11/26 */ public class Demo { public static void main(String[] args) throws JsonProcessingException { Writer wanger = new Writer("沉默王二", 18); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(wanger); System.out.println(jsonString); } } class Writer { private String name; private int age; public Writer(String name, int age) { this.name = name; this.age = age; } 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; } }
The program output result is as follows:
{ "name" : "沉默王二", "age" : 18 }
Not all fields support serialization and deserialization and need to comply with the following rules:
If you want to change the default serialization and deserialization rules, you need to call the setVisibility()
method of ObjectMapper. Otherwise, an InvalidDefinitionException will be thrown.
ObjectMapper deserializes JSON into Java objects from different data sources through the readValue series of methods.
readValue(String content, Class<t> valueType)</t>
method, deserializes the string into a Java objectreadValue(byte[ ] src, Class<t> valueType)</t>
method, deserializes the byte array into a Java object readValue(File src, Class<t> valueType)</t>
method, deserializes the byte array into a Java object Deserializing files into Java objectsLet’s take a look at the code example of deserializing strings into Java objects:
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * 微信搜索「沉默王二」,回复 Java * * @author 沉默王二 * @date 2020/11/26 */ public class Demo { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String jsonString = "{\n" + " \"name\" : \"沉默王二\",\n" + " \"age\" : 18\n" + "}"; Writer deserializedWriter = mapper.readValue(jsonString, Writer.class); System.out.println(deserializedWriter); } } class Writer{ private String name; private int age; // getter/setter @Override public String toString() { return "Writer{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
The program output is as follows:
Writer{name='沉默王二', age=18}
PS: If the deserialized object has a constructor with parameters, it must have an empty default constructor, otherwise an InvalidDefinitionException
line will be thrown.
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.itwanger.jackson.Writer` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{ "name" : "沉默王二", "age" : 18 }"; line: 2, column: 3] at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1589) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1055) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173) at com.itwanger.jackson.Demo.main(Demo.java:19)
Jackson's most commonly used API is ObjectMapper based on "object binding".
ObjectMapper can also parse JSON into JsonNode objects based on "tree model", see the example below.
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * 微信搜索「沉默王二」,回复 Java * * @author 沉默王二 * @date 2020/11/26 */ public class JsonNodeDemo { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String json = "{ \"name\" : \"沉默王二\", \"age\" : 18 }"; JsonNode jsonNode = mapper.readTree(json); String name = jsonNode.get("name").asText(); System.out.println(name); // 沉默王二 } }
借助 TypeReference 可以将 JSON 字符串数组转成泛型 List,来看下面的示例:
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; /** * 微信搜索「沉默王二」,回复 Java * * @author 沉默王二 * @date 2020/11/26 */ public class TypeReferenceDemo { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String json = "[{ \"name\" : \"沉默王三\", \"age\" : 18 }, { \"name\" : \"沉默王二\", \"age\" : 19 }]"; List<author> listAuthor = mapper.readValue(json, new TypeReference<list>>(){}); System.out.println(listAuthor); } } class Author{ private String name; private int age; // getter/setter // toString }</list></author>
Jackson 之所以牛掰的一个很重要的因素是可以实现高度灵活的自定义配置。
在实际的应用场景中,JSON 中常常会有一些 Java 对象中没有的字段,这时候,如果直接解析的话,会抛出 UnrecognizedPropertyException 异常。
下面是一串 JSON 字符串:
String jsonString = "{\n" + " \"name\" : \"沉默王二\",\n" + " \"age\" : 18\n" + " \"sex\" : \"男\",\n" + "}";
但 Java 对象 Writer 中没有定义 sex 字段:
class Writer{ private String name; private int age; // getter/setter }
我们来尝试解析一下:
ObjectMapper mapper = new ObjectMapper(); Writer deserializedWriter = mapper.readValue(jsonString, Writer.class);
不出意外,抛出异常了,sex 无法识别。
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "sex" (class com.itwanger.jackson.Writer), not marked as ignorable (2 known properties: "name", "age"]) at [Source: (String)"{ "name" : "沉默王二", "age" : 18, "sex" : "男" }"; line: 4, column: 12] (through reference chain: com.itwanger.jackson.Writer["sex"])
怎么办呢?可以通过 configure()
方法忽略掉这些“无法识别”的字段。
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
除此之外,还有其他一些有用的配置信息,来了解一下:
// 在序列化时忽略值为 null 的属性 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 忽略值为默认值的属性 mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
对于日期类型的字段,比如说 java.util.Date,如果不指定格式,序列化后将显示为 long 类型的数据,这种默认格式的可读性很差。
{ "age" : 18, "birthday" : 1606358621209 }
怎么办呢?
第一种方案,在 getter 上使用 @JsonFormat
注解。
private Date birthday; // GMT+8 是指格林尼治的标准时间,在加上八个小时表示你现在所在时区的时间 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; }
再来看一下结果:
{ "age" : 18, "birthday" : "2020-11-26 03:02:30" }
具体代码如下所示:
ObjectMapper mapper = new ObjectMapper(); Writer wanger = new Writer("沉默王二", 18); wanger.setBirthday(new Date()); String jsonString = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(wanger); System.out.println(jsonString);
第二种方案,调用 ObjectMapper 的 setDateFormat()
方法。
ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(StdDateFormat.getDateTimeInstance()); Writer wanger = new Writer("沉默王二", 18); wanger.setBirthday(new Date()); String jsonString = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(wanger); System.out.println(jsonString);
输出结果如下所示:
{ "name" : "沉默王二", "age" : 18, "birthday" : "2020年11月26日 上午11:09:51" }
在将 Java 对象序列化为 JSON 时,可能有些字段需要过滤,不显示在 JSON 中,Jackson 有一种比较简单的实现方式。
@JsonIgnore 用于过滤单个字段。
@JsonIgnore public String getName() { return name; }
@JsonIgnoreProperties 用于过滤多个字段。
@JsonIgnoreProperties(value = { "age","birthday" }) class Writer{ private String name; private int age; private Date birthday; }
当 Jackson 默认序列化和反序列化不能满足实际的开发需要时,可以自定义新的序列化和反序列化类。
自定义的序列化类需要继承 StdSerializer,同时重写 serialize()
方法,利用 JsonGenerator 生成 JSON,示例如下:
/** * 微信搜索「沉默王二」,回复 Java * * @author 沉默王二 * @date 2020/11/26 */ public class CustomSerializer extends StdSerializer<man> { protected CustomSerializer(Class<man> t) { super(t); } public CustomSerializer() { this(null); } @Override public void serialize(Man value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeStartObject(); gen.writeStringField("name", value.getName()); gen.writeEndObject(); } } class Man{ private int age; private String name; public Man(int age, String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }</man></man>
定义好自定义序列化类后,要想在程序中调用它们,需要将其注册到 ObjectMapper 的 Module 中,示例如下所示:
ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); module.addSerializer(Man.class, new CustomSerializer()); mapper.registerModule(module); Man man = new Man( 18,"沉默王二"); String json = mapper.writeValueAsString(man); System.out.println(json);
程序输出结果如下所示:
{"name":"沉默王二"}
自定义序列化类 CustomSerializer 中没有添加 age 字段,所以只输出了 name 字段。
再来看一下自定义的反序列化类,继承 StdDeserializer,同时重写 deserialize()
方法,利用 JsonGenerator 读取 JSON,示例如下:
public class CustomDeserializer extends StdDeserializer<woman> { protected CustomDeserializer(Class> vc) { super(vc); } public CustomDeserializer() { this(null); } @Override public Woman deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = p.getCodec().readTree(p); Woman woman = new Woman(); int age = (Integer) ((IntNode) node.get("age")).numberValue(); String name = node.get("name").asText(); woman.setAge(age); woman.setName(name); return woman; } } class Woman{ private int age; private String name; public Woman() { } // getter/setter @Override public String toString() { return "Woman{" + "age=" + age + ", name='" + name + '\'' + '}'; } }</woman>
通过 JsonNode 把 JSON 读取到一个树形结构中,然后通过 JsonNode 的 get 方法将对应字段读取出来,然后生成新的 Java 对象,并返回。
定义好自定义反序列化类后,要想在程序中调用它们,同样需要将其注册到 ObjectMapper 的 Module 中,示例如下所示:
ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule("CustomDeserializer", new Version(1, 0, 0, null, null, null)); module.addDeserializer(Woman.class, new CustomDeserializer()); mapper.registerModule(module); String json = "{ \"name\" : \"三妹\", \"age\" : 18 }"; Woman woman = mapper.readValue(json, Woman.class); System.out.println(woman);
程序输出结果如下所示:
Woman{age=18, name='三妹'}
哎呀,好像不错哦,Jackson 绝对配得上“最牛掰”这三个字,虽然有点虚。如果只想简单的序列化和反序列化,使用 ObjectMapper 的 write 和 read 方法即可。
如果还想更进一步的话,就需要对 ObjectMapper 进行一些自定义配置,或者加一些注解,以及直接自定义序列化和反序列化类,更贴近一些 Java 对象。
需要注意的是,对日期格式的字段要多加小心,尽量不要使用默认配置,可读性很差。
好了,通过这篇文章的系统化介绍,相信你已经完全摸透 Jackson 了,我们下篇文章见。
PS:如果你恰好需要一份 Java 精进路线的话,我这里有一份,我差不多花了 3 天的时间整理的,还挺受欢迎的,已经 2000 多赞了,每个阶段都很详细。
相关免费学习推荐:php编程(视频)
The above is the detailed content of The Java JSON parser that I think is the best: Jackson. For more information, please follow other related articles on the PHP Chinese website!