Jackson应用指北
1. 概述
Jackson是目前在web开发中应用最多,速度最快的一种json序列化和反序列化框架。本文次要联合Jackson在我的项目中的理论应用进行介绍。
2. 字段疏忽
2.1 json转POJO时,疏忽某些字段
只须要在实体类上加上@JsonIgnoreProperties(ignoreUnknown = true)
注解就能够。
2.2 POJO转json时,疏忽某些字段
@JsonIgnore
注解用来疏忽某些字段,能够用在Field或者Getter办法上,用在Setter办法时,和Filed成果一样
3. json转换为POJO
3.1 json字符串间接转POJO
objectMapper.readValue(json, xxx.class)
实用于json字符串与POJO间接一一对应。
3.2 json字符串转JsonNode
JsonNode rootNode = objectMapper.readTree(json);
// 获取其中某个字段的数据
JsonNode dataNode = rootNode.findValue("xxx");
// 将数据反序列化为POJO
objectMapper.treeToValue(dataNode, xxx.class);
实用于Json字符串的某一部分与JsonNode对应。先获取json响应中的某一部分数据,再将该数据转换为POJO
3.3 json转泛型类
AiReply<SensitiveImageReply> reply = objectMapper.readValue(json, new TypeReference<AiReply<SensitiveImageReply>>() {});
3.4 json转数组和列表
List<Person> personList = new ArrayList<Person>() {
{
add(new Person("yangjian", 22));
add(new Person("zhanghaoman", 22));
}
};
String personJson = this.objectMapper.writeValueAsString(personList);
List<Person> persons = this.objectMapper.readValue(personJson, List.class);
System.out.println("persons = " + persons);
3.5 类拷贝
// 拷贝成map
Person person = new Person("yangjian", 23);
String json = this.objectMapper.writeValueAsString(person);
Map<String, Object> map = this.objectMapper.convertValue(person, Map.class);
// 链表到数组拷贝
List<String> list = new ArrayList<String>() {
{
add("one");
add("two");
}
};
String[] strings = this.objectMapper.convertValue(list, String[].class);
System.out.println("this.objectMapper.writeValueAsString(strings) = " + this.objectMapper.writeValueAsString(strings));
4. POJO转换为json
4.1 设置视图
@JsonView(xxx.class)
注解能够实现视图显示,序列化的json只会蕴含有雷同视图润饰的字段。并且,视图能够继承。此注解,POJO类上和controller的办法上都须要加。
4.2 自定义某个字段的序列化/反序列化形式
应用SpringMVC的@RestController
时,咱们都晓得如果返回的是一个POJO,那么SpringMVC将会主动进行POJO的序列化。但有些时候咱们往往须要对该POJO某个字段的序列化和反序列化的形式进行一些批改,以满足咱们的业务需要。这时候就能够用到@JsonSerialize
和@JsonDeserialize
用法:
-
POJO
/**
*
- @author rainbow
- @since 2020/3/20 10:56
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@NoArgsConstructor
public class IdCardOcrData {
private String name;
private String sex;
private String nation;
@JsonDeserialize(using = IdCardLocalDateDeserializer.class)
@JsonSerialize(using = IdCardLocalDateSerializer.class)
private LocalDate birth;
private String address;
private String id;
private String authority;
/**
* 无效时限
*/
private String valid_date;
}
- IdCardLocalDateSerializer类
/**
- @author rainbow
- @since 2020/3/20 16:25
*/
public class IdCardLocalDateSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
gen.writeString(timeFormatter.format(value));
}
}
- IdCardLocalDateDeserializer类
/**
- @author rainbow
- @since 2020/3/20 13:05
*/
public class IdCardLocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String dateString = p.getText();
if (StringUtils.isNotBlank(dateString)) {
String[] split = dateString.split("/");
return LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
}
return null;
}
}
以上的例子能够将 “2020/2/1” 格局的字符串反序列化为LocalDate类,并将其序列化为 “2020-02-02” 格局的json数据。
### 4.3 自定义字段序列化形式@JsonFormat
此注解用于属性或者办法上(最好是属性上),能够不便的把Date类型间接转化为咱们想要的模式,比方`@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss", timezone = "GMT+8")`
## 5. 自定义objectMapper
应用SpringBoot主动拆卸的ObjectMapper在某些状况不太实用,比方它会将值为null的字段也进行序列化返回。因而,咱们对ObjectMapper进行设置,达到本人想要的成果。
具体配置类为:
@Configuration
public class JacksonConfig {
private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
builder.locale(Locale.CHINA);
builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
builder.simpleDateFormat(FORMAT);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(
DateTimeFormatter.ofPattern(FORMAT)));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.modules(javaTimeModule);
};
}
@Bean
@Primary
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
{
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 通过该办法对mapper对象进行设置,所有序列化的对象都将按改规定进行系列化
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的
// Include.NON_NULL 属性为NULL 不序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 容许呈现特殊字符和本义符
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
// 容许呈现单引号
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
/**
* 将Long,BigInteger序列化的时候,转化为String
*/
// SimpleModule simpleModule = new SimpleModule();
//
// simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
// simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
// simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
//
// objectMapper.registerModule(simpleModule);
return objectMapper;
}
}
该配置类能对类的`LocalDateTime`等新工夫类进行序列化,也能对序列化和反序列化解析形式进行设置。
发表回复