Jackson使用指南

原文連接👉 Jackson使用指南java

從事JAVA開發工做以來,一直都離不開Jackson的序列化反序列化,對於Jackson的使用也一直處於夠用但不深刻的狀態,下面是平常使用過程當中對Jackson的總結。node

Jackson經常使用註解

序列化註解

@JsonAnyGetter

像普通屬性同樣序列化Mapgit

public class ExtendableBean {
    public String name;
    private Map<String, String> properties;
 
    @JsonAnyGetter
    public Map<String, String> getProperties() {
        return properties;
    }
}
複製代碼

序列化示例:github

{
    "name":"My bean",
    "attr2":"val2",
    "attr1":"val1"
}
複製代碼

@JsonGetter

將指定的方法標記爲getter方法。能夠用來代替@JsonPropertyjson

public class MyBean {
    public int id;
    private String name;
 
    @JsonGetter("name")
    public String getTheName() {
        return name;
    }
}
複製代碼

序列化示例:數組

{
    "id": 1,
    "name":"My bean"
}
複製代碼

@JsonPropertyOrder

用在類上,在序列化的時候自定義屬性輸出順序app

@JsonPropertyOrder({ "name", "id" })
public class MyBean {
    public int id;
    public String name;
}
複製代碼

序列化示例:ide

{
    "name":"My bean",
    "id": 1
}
複製代碼

@JsonRawValue

徹底按照原樣序列化屬性的值函數

public class RawBean {
    public String name;
 
    @JsonRawValue
    public String json;
}
複製代碼

例如:jsonp

RawBean bean = new RawBean("My bean", "{\"attr\":false}");
複製代碼

將序列化爲:

{
    "name":"My bean",
    "json":{
        "attr":false
    }
}
複製代碼

而不是:

{
    "name":"My bean",
    "json":"{\"attr\":false}"
}
複製代碼

@JsonValue

定義整個實體的序列化方法,Jackson將會使用該方法的輸出做爲序列化輸出。

public enum TypeEnumWithValue {
    TYPE1(1, "Type A"), TYPE2(2, "Type 2");
 
    private Integer id;
    private String name;
 
    // standard constructors
 
    @JsonValue
    public String getName() {
        return name;
    }
}
複製代碼

序列化示例:

{
  "name": "Type 2"
}
複製代碼

@JsonRootName

若是須要將實體包裝一層,可使用@JsonRootName來指定根包裝器的名稱

@JsonRootName(value = "user")
public class UserWithRoot {
    public int id;
    public String name;
}
複製代碼

序列化示例:

{
    "user": {
        "id": 1,
        "name": "John"
    }
}
複製代碼

若是不用該註解,將會序列化爲:

{
    "id": 1,
    "name": "John"
}
複製代碼

@JsonSerialize

用於指定自定義序列化器來序列化實體

public class Event {
    public String name;
 
    @JsonSerialize(using = CustomDateSerializer.class)
    public Date eventDate;
}
複製代碼

自定義序列化器以下:

public class CustomDateSerializer extends StdSerializer<Date> {
 
    private static SimpleDateFormat formatter 
      = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
 
    public CustomDateSerializer() { 
        this(null); 
    } 
 
    public CustomDateSerializer(Class<Date> t) {
        super(t); 
    }
 
    @Override
    public void serialize( Date value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {
        gen.writeString(formatter.format(value));
    }
}
複製代碼

輸出示例:

{
  "name": "test",
  "eventDate": "20-12-2014 02:30:00"
}
複製代碼

反序列化註解

@JsonCreator

指定反序列化使用的構造函數或方法

待反序列化Json示例:

{
    "id":1,
    "theName":"My bean"
}
複製代碼
public class BeanWithCreator {
    public int id;
    public String name;
 
    @JsonCreator
    public BeanWithCreator(@JsonProperty("id") int id, @JsonProperty("theName") String name) {
        this.id = id;
        this.name = name;
    }
}
複製代碼

@JacksonInject

指定某個字段從注入賦值,而不是從Json

public class BeanWithInject {
    @JacksonInject
    public int id;
     
    public String name;
}
複製代碼

示例用法:

String json = "{\"name\":\"My bean\"}";
 
InjectableValues inject = new InjectableValues.Std()
  .addValue(int.class, 1);
BeanWithInject bean = new ObjectMapper().reader(inject)
  .forType(BeanWithInject.class)
  .readValue(json);
複製代碼

@JsonAnySetter

在反序列化時,將Map當成普通屬性

待反序列化Json:

{
    "name":"My bean",
    "attr2":"val2",
    "attr1":"val1"
}
複製代碼
public class ExtendableBean {
    public String name;
    private Map<String, String> properties;
 
    @JsonAnySetter
    public void add(String key, String value) {
        properties.put(key, value);
    }
}
複製代碼

properties字段的值將會是由 attr2 -> val2,attr1 -> val1組成的鍵值對。

@JsonSetter

將方法標記爲setter方法,能夠指定屬性名稱

public class MyBean {
    public int id;
    private String name;
 
    @JsonSetter("name")
    public void setTheName(String name) {
        this.name = name;
    }
}
複製代碼

@JsonDeserialize

用於指定自定義反序列化器來反序列化實體

public class Event {
    public String name;
 
    @JsonDeserialize(using = CustomDateDeserializer.class)
    public Date eventDate;
}
複製代碼

對應的反序列化器:

public class CustomDateDeserializer extends StdDeserializer<Date> {
 
    private static SimpleDateFormat formatter
      = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
 
    public CustomDateDeserializer() { 
        this(null); 
    } 
 
    public CustomDateDeserializer(Class<?> vc) { 
        super(vc); 
    }
 
    @Override
    public Date deserialize( JsonParser jsonparser, DeserializationContext context) throws IOException {
         
        String date = jsonparser.getText();
        try {
            return formatter.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
複製代碼

Jackson設置屬性是否參與序列化

@JsonIgnoreProperties

在類上指定要忽略的屬性

@JsonIgnoreProperties({ "id" })
public class BeanWithIgnore {
    public int id;
    public String name;
}
複製代碼

@JsonIgnore

在具體屬性上忽略,使其不參與序列化過程

public class BeanWithIgnore {
    @JsonIgnore
    public int id;
 
    public String name;
}
複製代碼

@JsonIgnoreProperties是等效的。

@JsonIgnoreType

用在類上,將忽略該類全部屬性

public class User {
    public int id;
    public Name name;
 
    @JsonIgnoreType
    public static class Name {
        public String firstName;
        public String lastName;
    }
}
複製代碼

@JsonInclude

用於排除值爲empty/null/default的屬性

@JsonInclude(Include.NON_NULL)
public class MyBean {
    public int id;
    public String name;
}
複製代碼

@JsonAutoDetect

強制序列化私有屬性,無論它有沒有getter方法

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class PrivateBean {
    private int id;
    private String name;
}
複製代碼

Jackson處理多態

通常都是組合起來使用,有下面三個註解:

  • @JsonTypeInfo

指定序列化中包含的類型信息的詳細信息

  • @JsonSubTypes

指定帶註釋類型的子類型

  • @JsonTypeName

指定用於帶註釋的類的邏輯類型名稱

public class Zoo {
    public Animal animal;
 
    @JsonTypeInfo(
      use = JsonTypeInfo.Id.NAME, 
      include = As.PROPERTY, 
      property = "type")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = Dog.class, name = "dog"),
        @JsonSubTypes.Type(value = Cat.class, name = "cat")
    })
    public static class Animal {
        public String name;
    }
 
    @JsonTypeName("dog")
    public static class Dog extends Animal {
        public double barkVolume;
    }
 
    @JsonTypeName("cat")
    public static class Cat extends Animal {
        boolean likesCream;
        public int lives;
    }
}
複製代碼

上述例子中,指定屬性type爲判斷具體子類的依據,例如:type=dog,將被序列化爲Dog類型。

Jackson通用註解(序列化反序列化都生效)

@JsonProperty

指定JSON中的屬性名稱

public class MyBean {
    public int id;
    private String name;
 
    @JsonProperty("name")
    public void setTheName(String name) {
        this.name = name;
    }
 
    @JsonProperty("name")
    public String getTheName() {
        return name;
    }
}
複製代碼

@JsonFormat

用於在序列化日期/時間值時指定格式。

public class Event {
    public String name;
 
    @JsonFormat(
      shape = JsonFormat.Shape.STRING,
      pattern = "dd-MM-yyyy hh:mm:ss")
    public Date eventDate;
}
複製代碼

@JsonUnwrapped

將對象中全部的屬性與當前平級,不太好描述,簡單說就是拆開包裝。

public class UnwrappedUser {
    public int id;
 
    @JsonUnwrapped
    public Name name;
 
    public static class Name {
        public String firstName;
        public String lastName;
    }
}
複製代碼

序列化示例:

{
    "id":1,
    "firstName":"John",
    "lastName":"Doe"
}
複製代碼

若是不加@JsonUnwrapped註解,將被序列化爲:

{
    "id":1,
    "name": {
        "firstName":"John",
        "lastName":"Doe"
    }
}
複製代碼

@JsonView

指定視圖,相似分組進行序列化/反序列化

定義視圖:

public class Views {
    public static class Public {}
    public static class Internal extends Public {}
}
複製代碼

定義實體:

public class Item {
    @JsonView(Views.Public.class)
    public int id;
 
    @JsonView(Views.Public.class)
    public String itemName;
 
    @JsonView(Views.Internal.class)
    public String ownerName;
}
複製代碼

序列化示例:

String result = new ObjectMapper()
  .writerWithView(Views.Public.class)
  .writeValueAsString(item);
複製代碼

這時,將只會序列化iditemName字段

@JsonManagedReference, @JsonBackReference

@JsonManagedReference和@JsonBackReference註釋用於處理父/子關係並解決循環問題。

例如,有兩個相互引用的類:

public class ItemWithRef {
    public int id;
    public String itemName;
 
    @JsonManagedReference
    public UserWithRef owner;
}
複製代碼
public class UserWithRef {
    public int id;
    public String name;
 
    @JsonBackReference
    public List<ItemWithRef> userItems;
}
複製代碼

不加註解,會循環調用,致使內存溢出,這時候可使用@JsonManagedReference@JsonBackReference來避免內存溢出。

@JsonIdentityInfo

用於指定在序列化/反序列化值時使用對象標識,例如,處理無限遞歸類型的問題。

@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class,
  property = "id")
public class ItemWithIdentity {
    public int id;
    public String itemName;
    public UserWithIdentity owner;
}
複製代碼

@JsonFilter

指定序列化期間要使用的過濾器。

@JsonFilter("myFilter")
public class BeanWithFilter {
    public int id;
    public String name;
}
複製代碼

示例代碼:

BeanWithFilter bean = new BeanWithFilter(1, "My bean");

FilterProvider filters 
  = new SimpleFilterProvider().addFilter(
    "myFilter", 
    SimpleBeanPropertyFilter.filterOutAllExcept("name"));

String result = new ObjectMapper()
  .writer(filters)
  .writeValueAsString(bean);
複製代碼

自定義Jackson註解

可使用@JacksonAnnotationsInside來開發自定義註解

@Retention(RetentionPolicy.RUNTIME)
    @JacksonAnnotationsInside
    @JsonInclude(Include.NON_NULL)
    @JsonPropertyOrder({ "name", "id", "dateCreated" })
    public @interface CustomAnnotation {}
複製代碼

如何使用自定義註解:

@CustomAnnotation
public class BeanWithCustomAnnotation {
    public int id;
    public String name;
    public Date dateCreated;
}
複製代碼

自定義註解能夠加強代碼複用,把一些通用的Jackson註解組合起來,造成一個新註解,新註解能夠代替組合的註解。

Jackson MixIn 註解

動態地爲某些類型增長統一的Jackson註解

實體:

public class Item {
    public int id;
    public String itemName;
    public User owner;
}
複製代碼

MixIn類:

@JsonIgnoreType
public class MyMixInForIgnoreType {}
複製代碼

咱們能夠動態地讓User類型不參與序列化:

Item item = new Item(1, "book", null);
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(User.class, MyMixInForIgnoreType.class);
result = mapper.writeValueAsString(item);
複製代碼

禁用Jackson註解

假設咱們有一個帶Jackson註解的實體:

@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({ "name", "id" })
public class MyBean {
    public int id;
    public String name;
}
複製代碼

咱們能夠這樣來禁用該實體上的全部Jackson註解:

MyBean bean = new MyBean(1, null);
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_ANNOTATIONS);
複製代碼

Jackson的ObjectMapper用法

java類 轉換爲 json

能夠直接序列化爲Json字符串:

objectMapper.writeValueAsString(car);
複製代碼

或者,能夠序列化到文件,文件內容是Json字符串:

objectMapper.writeValue(new File("target/car.json"), car);
複製代碼

json 轉換爲 java類

從字符串:

String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
objectMapper.readValue(json, Car.class); 
複製代碼

從文件:

objectMapper.readValue(new File("target/json_car.json"), Car.class);
複製代碼

從URL:

objectMapper.readValue(new URL("target/json_car.json"), Car.class);
複製代碼

json轉換爲Jackson JsonNode

String json = "{ \"color\" : \"Black\", \"type\" : \"FIAT\" }";
JsonNode jsonNode = objectMapper.readTree(json);
String color = jsonNode.get("color").asText();
// Output: color -> Black
複製代碼

json 轉換爲 java集合

String jsonCarArray = 
  "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
複製代碼

json 轉換爲 Map

String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});
複製代碼

ObjectMapper的經常使用配置

忽略不識別的字段(json屬性與目標實體存在屬性上的差別):

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
複製代碼

容許原始值爲null:

objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
複製代碼

容許將枚舉序列化/反序列化爲數字:

objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
複製代碼

配置自定義序列化/反序列化器

假設有一個序列化器:

public class CustomCarSerializer extends StdSerializer<Car> {
     
    public CustomCarSerializer() {
        this(null);
    }
 
    public CustomCarSerializer(Class<Car> t) {
        super(t);
    }
 
    @Override
    public void serialize( Car car, JsonGenerator jsonGenerator, SerializerProvider serializer) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("car_brand", car.getType());
        jsonGenerator.writeEndObject();
    }
}
複製代碼

一個反序列化器:

public class CustomCarDeserializer extends StdDeserializer<Car> {
     
    public CustomCarDeserializer() {
        this(null);
    }
 
    public CustomCarDeserializer(Class<?> vc) {
        super(vc);
    }
 
    @Override
    public Car deserialize(JsonParser parser, DeserializationContext deserializer) {
        Car car = new Car();
        ObjectCodec codec = parser.getCodec();
        JsonNode node = codec.readTree(parser);
         
        // try catch block
        JsonNode colorNode = node.get("color");
        String color = colorNode.asText();
        car.setColor(color);
        return car;
    }
}
複製代碼

ObjectMapper使用他們:

//添加自定義序列化器
module.addSerializer(Car.class, new CustomCarSerializer());
//添加自定義反序列化器
module.addDeserializer(Car.class, new CustomCarDeserializer());
複製代碼

處理日期格式化

ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
objectMapper.setDateFormat(df);
複製代碼

處理集合

反序列化爲數組:

String jsonCarArray = 
  "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
Car[] cars = objectMapper.readValue(jsonCarArray, Car[].class);
複製代碼

反序列化爲集合:

String jsonCarArray = 
  "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
ObjectMapper objectMapper = new ObjectMapper();
List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>(){});
複製代碼

👉 代碼倉庫 👉 參考文章

相關文章
相關標籤/搜索