In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to master the Java JSON parser of Jackson". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Jackson how to master the Java JSON parser"!
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 front wave that was photographed to death on the beach.
Unfortunately, JDK doesn't have a JSON library, so I don't know why I didn't do it. Log4j, in order to compete, also launched java.util.logging, although in the end not many people use it.
The reason why Java is great is that its ecology is very complete. JDK does not have a JSON library, and there are third-party class libraries, which are quite good, such as the pig's foot in this article-the default JSON parser of the 6.1k Spring pencil Boot on Jackson,GitHub.
How do you prove it?
When we create a new Spring Boot Web project through starter, we can see Jackson in the Maven dependencies.
Jackson has many advantages:
It is faster to parse large files.
The runtime uses less memory and has better performance
API is flexible and easy to extend and customize.
The core module of Jackson consists of three parts:
Jackson-core, the core package, provides related API based on "flow mode" parsing, including JsonPaser and JsonGenerator.
Jackson-annotations, an annotation package, provides standard annotation functionality
Jackson-databind, the data binding package, provides related API (ObjectMapper) based on object binding parsing and related API (JsonNode) based on "tree model" parsing.
01. Introduce Jackson dependency
To use Jackson, you need to add Jackson dependencies to the pom.xml file.
Com.fasterxml.jackson.core jackson-databind 2.10.1
Jackson-databind depends on jackson-core and jackson-annotations, so after adding jackson-databind, Maven automatically introduces jackson-core and jackson-annotations into the project.
What makes Maven so attractive is that it can secretly help us do what needs to be done.
02. Use ObjectMapper
The most commonly used API for Jackson is ObjectMapper based on "object binding", which serializes Java objects to JSON through a series of writeValue methods, and can be stored in different formats.
WriteValueAsString (Object value) method, which stores the object as a string
The writeValueAsBytes (Object value) method, which stores objects as byte arrays
WriteValue (File resultFile, Object value) method, which stores the object as a file
Take a look at an example of code stored as a string:
Import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; * Wechat search for "Silent King 2" and reply to Java * * @ author Silent King 2 * @ date 2020-11-26 * / public class Demo {public static void main (String [] args) throws JsonProcessingException {Writer wanger = new Writer ("Silent King 2", 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 output of the program is as follows:
{"name": "Silent King II", "age": 18}
Not all fields support serialization and deserialization, and the following rules need to be met:
If the modifier for a field is public, the field can be serialized and deserialized (not standard).
If the modifier of a field is not public, but its getter and setter methods are public, the field can be serialized and deserialized. The getter method is used for serialization and the setter method is used for deserialization.
If the field has only the setter method of public and no getter method of public, the field can only be used for deserialization.
If you want to change the default serialization and deserialization rules, you need to call the setVisibility () method of ObjectMapper. Otherwise, an InvalidDefinitionException exception will be thrown.
ObjectMapper deserializes JSON into Java objects from different data sources through a series of readValue methods.
ReadValue (String content, Class valueType) method to deserialize a string into a Java object
ReadValue (byte [] src, Class valueType) method, which deserializes the byte array into a Java object
ReadValue (File src, Class valueType) method to deserialize files into Java objects
Take a look at an example of code that deserializes a string into a Java object:
Import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Demo {public static void main (String [] args) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper (); String jsonString = "{\ n" + "\" name\ ":\" Silent King 2\ ",\ 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 output of the program is as follows:
Writer {name=' Silent King 2', 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 ":" Silent King II "," 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)
The most commonly used API in Jackson is ObjectMapper based on "object binding"
ObjectMapper can also parse JSON into JsonNode objects based on the "tree model", as shown in the following example.
Import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonNodeDemo {public static void main (String [] args) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper (); String json = "{\" name\ ":\" Silent King 2 ",\" age\ ": 18}"; JsonNode jsonNode = mapper.readTree (json) String name = jsonNode.get ("name"). AsText (); System.out.println (name); / / Silent King 2}}
With TypeReference, you can convert an array of JSON strings to generic List. Take a look at the following example:
Import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; public class TypeReferenceDemo {public static void main (String [] args) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper () String json = "[{\" name\ ":\" Silent King 3\ ",\" age\ ": 18}, {\" name\ ":\" Silent King 2\ ",\" age\ ": 19}]"; List listAuthor = mapper.readValue (json, new TypeReference () {}); System.out.println (listAuthor);}} class Author {private String name; private int age / / getter/setter / / toString}
03. More advanced configuration
A very important factor that makes Jackson awesome is that it can achieve a highly flexible custom configuration.
In the actual application scenario, there are often some fields in the JSON that are not in the Java object. At this time, if it is parsed directly, a UnrecognizedPropertyException exception will be thrown.
Here is a string of JSON strings:
String jsonString = "{\ n" + "\" name\ ":\" Silent King 2\ ",\ n" + "\" age\ ": 18\ n" + "\" sex\ ":\" male\ ",\ n" + "}"
However, the sex field is not defined in the Java object Writer:
Class Writer {private String name; private int age; / / getter/setter}
Let's try to analyze it:
ObjectMapper mapper = new ObjectMapper (); Writer deserializedWriter = mapper.readValue (jsonString, Writer.class)
Not surprisingly, an exception was thrown and sex could not recognize it.
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 ":" Silent King II "," age ": 18," sex ":" male "}; line: 4, column: 12] (through reference chain: com.itwanger.jackson.Writer [" sex "])
What should I do? You can ignore these "unrecognized" fields through the configure () method.
Mapper.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
In addition, there are some other useful configuration information to learn about:
/ ignore the attribute mapper.setSerializationInclusion (JsonInclude.Include.NON_NULL) whose value is null when serializing; / / ignore the attribute mapper.setDefaultPropertyInclusion (JsonInclude.Include.NON_DEFAULT) whose value is the default value
04. Processing date format
For fields of date type, such as java.util.Date, if you do not specify a format, the serialized data will be displayed as data of type long, which is not readable by default.
{"age": 18, "birthday": 1606358621209}
What should I do?
The first scenario is to use the @ JsonFormat annotation on getter.
Private Date birthday; / / GMT+8 refers to Greenwich mean time, plus eight hours to indicate the time in your current time zone @ JsonFormat (timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") public Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this.birthday = birthday;}
Let's take another look at the results:
{"age": 18, "birthday": "2020-11-26 03:02:30"}
The specific code is as follows:
ObjectMapper mapper = new ObjectMapper (); Writer wanger = new Writer (Silence II, 18); wanger.setBirthday (new Date ()); String jsonString = mapper.writerWithDefaultPrettyPrinter () .writeValueAsString (wanger); System.out.println (jsonString)
The second scenario is to call the setDateFormat () method of ObjectMapper.
ObjectMapper mapper = new ObjectMapper (); mapper.setDateFormat (StdDateFormat.getDateTimeInstance ()); Writer wanger = new Writer (Silent King II, 18); wanger.setBirthday (new Date ()); String jsonString = mapper.writerWithDefaultPrettyPrinter () .writeValueAsString (wanger); System.out.println (jsonString)
The output is as follows:
{"name": "Silent King II", "age": 18, "birthday": "11:09:51 on November 26th, 2020"}
05. Field filtering
When serializing Java objects into JSON, some fields may need to be filtered and not displayed in JSON. Jackson has a relatively simple implementation.
@ JsonIgnore is used to filter individual fields.
@ JsonIgnore public String getName () {return name;}
@ JsonIgnoreProperties is used to filter multiple fields.
JsonIgnoreProperties (value = {"age", "birthday"}) class Writer {private String name; private int age; private Date birthday;}
06. Custom serialization and deserialization
When Jackson default serialization and deserialization do not meet the actual development needs, you can customize new serialization and deserialization classes.
The custom serialization class needs to inherit StdSerializer and override the serialize () method to generate JSON using JsonGenerator. The example is as follows:
Public class CustomSerializer extends StdSerializer {protected CustomSerializer (Class 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;}}
Once the custom serialization classes are defined, you need to register them in the Module of ObjectMapper in order to call them in the program, as shown in the following example:
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, "Silent King II"); String json = mapper.writeValueAsString (man); System.out.println (json)
The output of the program is as follows:
{"name": "Silent King II"}
The age field is not added to the custom serialization class CustomSerializer, so only the name field is output.
Let's take a look at the custom deserialization class, which inherits StdDeserializer and overrides the deserialize () method to read JSON using JsonGenerator. The example is as follows:
Public class CustomDeserializer extends StdDeserializer {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 +'\'+'}';}}
Read the JSON into a tree structure through JsonNode, then read the corresponding fields through the get method of JsonNode, and then generate a new Java object and return it.
Once the custom deserialization classes are defined, you also need to register them in the Module of ObjectMapper in order to call them in the program. The example is as follows:
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\ ":\" third sister\ ",\" age\ ": 18}"; Woman woman = mapper.readValue (json, Woman.class); System.out.println (woman)
The output of the program is as follows:
Woman {age=18, name=' third sister'} here, I believe you have a deeper understanding of "how to master the Java JSON parser in Jackson". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.