添加maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency>
|
常用注解
- @JsonIgnore 作用在字段或方法上,用来完全忽略被注解的字段和方法对应的属性.
- @JsonProperty 作用在字段或方法上,用来对属性的序列化/反序列化,可以用来避免遗漏属性,同时提供对属性名称重命名.
- @JsonPropertyOrder 作用在类上,用来指明当序列化时需要对属性做排序.
- @JsonFormat 作用在属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = “yyyy-MM-dd HH-mm-ss”)。
NULL或者为空不参加序列化
可以通过两种方式实现
1.使用@JsonInclude
注解
1
| @JsonInclude(Include.NON_NULL)
|
将该标记放在属性上,如果该属性为NULL则不参与序列化;如果放在类上边,那对这个类的全部属性起作用。
Include.Include.ALWAYS 默认
Include.NON_DEFAULT 属性为默认值不序列化
Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
Include.NON_NULL 属性为NULL 不序列化
2.设置ObjectMapper
序列化参数
代码示例如下:
1 2
| ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL);
|
自定义序列化与反序列化
先定义实体类
1 2 3 4 5 6 7 8 9 10 11 12
| public class User { public int id; public String name; ... }
public class Item { public int id; public String itemName; public User owner; ... }
|
使用jackson进行序列化:
1 2
| Item item = new Item(1, "itemName", new User(2, "userName")); String serialized = new ObjectMapper().writeValueAsString(item);
|
正常序列化的json为:
1 2 3 4 5 6 7 8
| { "id": 1, "itemName": "theItem", "owner": { "id": 2, "name": "theUser" } }
|
如果想要改变序列化的json结构,则需要进行自定义。继承JsonSerializer类。
1 2 3 4 5 6 7 8 9 10 11
| public class ItemSerializer extends JsonSerializer<Item> { @Override public void serialize(Item value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); jgen.writeNumberField("id", value.id); jgen.writeStringField("itemName", value.itemName); jgen.writeNumberField("owner", value.owner.id); jgen.writeEndObject(); } }
|
使用jackson进行序列化:
1 2 3 4 5 6 7 8
| Item item = new Item(1, "itemName", new User(2, "userName")); ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Item.class, new ItemSerializer()); mapper.registerModule(module);
String serialized = mapper.writeValueAsString(item);
|
也可以通过在实体类上使用@JsonSerialize
注解实现:
1 2 3 4
| @JsonSerialize(using = ItemSerializer.class) public class Item { ... }
|
使用jackson进行序列化:
1 2
| Item item = new Item(1, "itemName", new User(2, "userName")); String serialized = new ObjectMapper().writeValueAsString(item);
|
使用jackson进行反序列化:
1
| Item item = new ObjectMapper().readValue(json, Item.class);
|
如果是自定义过的json串需要自定义。继承JsonDeserializer类。
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class ItemDeserializer extends JsonDeserializer<Item> { @Override public Item deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.getCodec().readTree(jp); int id = (Integer) ((IntNode) node.get("id")).numberValue(); String itemName = node.get("itemName").asText(); int userId = (Integer) ((IntNode) node.get("id")).numberValue(); return new Item(id, itemName, new User(userId, null)); } }
|
使用jackson反序列化
1 2 3 4 5 6
| ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Item.class, new ItemDeserializer()); mapper.registerModule(module); Item readValue = mapper.readValue(json, Item.class);
|
同理,也可以使用@JsonDeserialize
注解实现:
1 2 3 4
| @JsonDeserialize(using = ItemDeserializer.class) public class Item { ... }
|
序列化与反序列化工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.io.IOException; import java.util.List;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
private static ObjectMapper objMapper = new ObjectMapper(); static { objMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); }
public static String toJson(Object obj) throws IOException { return objMapper.writeValueAsString(obj); }
public static <T> T fromJson(Class<T> clazz, String json) throws IOException { return objMapper.readValue(json, clazz); }
public static <T> List<T> fromJsonList(Class<T> clazz, String json) throws IOException { return objMapper.readValue(json, objMapper.getTypeFactory().constructCollectionType(List.class, clazz)); } }
|