引用jar:jackson-core,jackson-databind,jackson-annotations
http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.9.9/jackson-core-2.9.9.jar
1, jackson basic use
1.1, create a Person object
public class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } }
1.2, Main method calls
NOTE: The object has properties need to turn json get methods (except annotation method)
import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] arges) throws Exception { ObjectMapper mapper = new ObjectMapper(); Person person = new Person("jackson",20); System.out.println(mapper.writeValueAsString(person)); } }
The output is:
{"name":"jackson"}
2, the use of annotations
2.1, @ JsonProperty use annotation
@JsonProperty annotations similar in sql alias fields, a sequence of using annotations field properties, replace the original field properties
@JsonProperty("userName") private String name;
Sequence of results: In the serialized json string, userName replace the name
{"userName":"jackson"}
2.2, @ JsonIgnore use annotation
@JsonIgnore comment is to ignore the field when serializing
@JsonIgnore @JsonProperty("userName") private String name; @JsonProperty("userAge") private Integer age;
Serialization result is: { "userAge": 20}
{"userAge":20}
2.3, @ JsonIgnoreProperties use annotation
2.3.1, with a sequence of similar @JsonIgnoreProperties @JsonIgnore, for the class, using the annotation field aliases
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(value={"name","userAge"}) public class Person { @JsonIgnore @JsonProperty("userName") private String name; @JsonProperty("userAge") private Integer age; @JsonProperty("userHeight") private Integer height; public Person(String name, Integer age, Integer height) { this.name = name; this.age = age; this.height = height; } }
ObjectMapper mapper = new ObjectMapper(); Person person = new Person("jackson",20,175); System.out.println(mapper.writeValueAsString(person));
Run results: { "userHeight": 175}
{"userHeight":175}
2.3.2, @ JsonIgnoreProperties (ignoreUnknown = true ) for ignoring fields do not match, the equivalent mapper.disable (DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES);
Note: deserialization need no-argument constructor
ObjectMapper mapper = new ObjectMapper(); Person person = new Person("jackson",20,175); System.out.println(mapper.writeValueAsString(person)); //mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); System.out.println(mapper.readValue("{\"sheight\":172}", Person.class).getHeight());
2.4 @ JsonTypeName @JsonTypeInfo
@JsonTypeName(value = "user")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
Layer is increased in the sequence of
Serialization result is: { "user": { "height": 175}}
{"user":{"height":175}}
2.5, @ JsonRootName comment
Composition equal to 2.4 based on annotations @JsonRootName ( "user") and mapper.enable (SerializationFeature.WRAP_ROOT_VALUE);
2.6, @ JsonFormat annotation format date format
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss:SSS",timezone="GMT+8")
private Date date;