json parsing tool jackson

The Jackson library ( http://jackson.codehaus.org ) is an open source json format parsing tool based on the Java language. The entire library contains three jar packages:

  1. jackson-core.jar - core package (required) that provides an API based on "streaming mode" parsing.
  2. jackson-databind - data binding package (optional), providing APIs based on "object binding" and "tree model".
  3. jackson-annotations - annotation package (optional), providing annotation function.
  • Jackson's strengths

         Compared with other libraries for Java  json parsing, such as json-lib and gson packages, Jackson has the following advantages:

  1. It has comprehensive functions, provides a variety of json parsing methods, and "object binding" is easy to use. Using annotation packages can provide a lot of convenience for our development.
  2. High performance, the parsing efficiency of "stream mode" exceeds most similar json packages.
  • Important API
  1. Core package: JsonPaser (json stream read), JsonGenerator (json stream output).
  2. Data binding packages: ObjectMapper (build tree mode and object binding mode), JsonNode (tree node).

Jackson provides us with a series of annotations in practical applications, which improves the flexibility of development. Here are some of the most commonly used annotations.

  • @JsonIgnoreProperties

         This annotation is a class annotation. Its function is to ignore some properties in the Java  bean during json serialization, and both serialization and deserialization are affected.

  • @JsonIgnore

         This annotation is used on properties or methods (preferably on properties), and has the same effect as @JsonIgnoreProperties above.

  • @JsonFormat

        This annotation is used on properties or methods (preferably on properties), which can easily convert the Date type directly into the pattern we want, such as @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")

  • @JsonSerialize

        This annotation is used on properties or getter methods to embed our custom code during serialization, such as limiting two decimal points after serializing a double.

public class CustomDoubleSerialize extends JsonSerializer<Double> {  
    private DecimalFormat df = new DecimalFormat("##.00");  
    @Override  
    public void serialize(Double value, JsonGenerator jgen,  
            SerializerProvider provider) throws IOException,  
            JsonProcessingException {  
  
        jgen.writeString(df.format(value));  
    }  
}  
  • @JsonDeserialize

         This annotation is used on properties or setter methods to embed our custom code during deserialization, similar to @JsonSerialize above

public class CustomDateDeserialize extends JsonDeserializer<Date> {  
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    @Override  
    public Date deserialize(JsonParser jp, DeserializationContext ctxt)  
            throws IOException, JsonProcessingException {  
        Date date = null;  
        try {  
            date = sdf.parse(jp.getText());  
        } catch (ParseException e) {  
            e.printStackTrace ();  
        }  
        return date;  
    }  
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487550&siteId=291194637