1.Jackson的导入
com.fasterxml.jackson.core
jackson-databind
2.5.3
2.核心对象是ObejecjMapper
TestBean testBean = new TestBean("haha");
TestBean testBean1 = new TestBean("haha1");
System.out.println("testbean1"+testBean);
System.out.println("testbean2"+testBean1);
l.add(testBean1);
l.add(testBean);
ObjectMapper objectMapper = new ObjectMapper();
String ja = "{\"name\":\"haha\"}";
String json = objectMapper.writeValueAsString(l);
System.out.println(json);
TestBean testBean2 = objectMapper.readValue(ja, TestBean.class);
System.out.println(testBean2);
List beanList = objectMapper.readValue(json, new TypeReference>() {});
System.out.println(beanList);
将对象转换成json字符串
String json = objectMapper.writeValueAsString(l);
输出
[{
"name":"haha"},{
"name":"haha1"}]
将json字符串转换为对象
TestBean testBean2 = objectMapper.readValue(ja, TestBean.class);
输出
TestBean(name=haha)
嵌套对象的反序列化
List beanList = objectMapper.readValue(json, new TypeReference>() {});
输出
[TestBean(name=haha), TestBean(name=haha1)]
Jackson 具有从多种数据源读取数据并且转换为json字符串的功能,如文件,流,字符串。
3.注解方式
@JsonProperty注解指定一个属性用于JSON映射,默认情况下映射的JSON属性与注解的属性名称相同,不过可以使用该注解的value值修改JSON属性名,该注解还有一个index属性指定生成JSON属性的顺序,如果有必要的话。可以将属性与json对应起来,专门用于某些接口返回的字符串不符合驼峰命名方式
@JsonIgnore 属性注解,无视某些属性的序列化与反序列化
@JsonIgnoreProperties注解是类注解。在序列化为JSON的时候,@JsonIgnoreProperties({“prop1”, “prop2”})会忽略pro1和pro2两个属性。在从JSON反序列化为Java类的时候,@JsonIgnoreProperties(ignoreUnknown=true)会忽略所有没有Getter和Setter的属性。该注解在Java类和JSON不完全匹配的时候很有用。
@JsonIgnoreType也是类注解,会排除所有指定类型的属性。
@JsonProperty("na")
private String name;
[{
"na":"haha"},{
"na":"haha1"}]
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module());
mapper.findAndRegisterModules();
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonRootName("Person")
public class Person {
@JsonProperty("Name")
private String name;
@JsonProperty("NickName")
private String nickname;
@JsonProperty("Age")
private int age;
@JsonProperty("IdentityCode")
private String identityCode;
@JsonProperty
@JsonFormat(pattern = "yyyy-MM-DD")
private LocalDate birthday;
}
注册了时间模块之后看看代码中如何写
static void java8DateTime() throws IOException {
Person p1 = new Person("yitian", "易天", 25, "10000", LocalDate.of(1994, 1, 1));
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
//mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String text = mapper.writeValueAsString(p1);
System.out.println(text);
Person p2 = mapper.readValue(text, Person.class);
System.out.println(p2);
}
这时输出的是
{
"birthday":[1994,1,1],"Name":"yitian","NickName":"易天","Age":25,"IdentityCode":"10000"}
Person(name=yitian, nickname=易天, age=25, identityCode=10000, birthday=1994-01-01)
不符合规范,取消注释的代码
{
"birthday":"1994-01-01","Name":"yitian","NickName":"易天","Age":25,"IdentityCode":"10000"}
Person(name=yitian, nickname=易天, age=25, identityCode=10000, birthday=1994-01-01)
自动配置
Spring Boot对Jackson的支持非常完善,只要我们引入相应类库,Spring Boot就可以自动配置开箱即用的Bean。Spring自动配置的ObjectMapper(或者XmlMapper)作了如下配置,基本上可以适应大部分情况。
Spring Boot自动配置非常方便,但不是万能的。在必要的时候,我们需要手动配置Bean来替代自动配置的Bean
@Configuration
public class JacksonConfig {
@Bean
@Primary
@Qualifier("xml")
public XmlMapper xmlMapper(Jackson2ObjectMapperBuilder builder) {
XmlMapper mapper = builder.createXmlMapper(true)
.build();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
@Bean
@Qualifier("json")
public ObjectMapper jsonMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper mapper = builder.createXmlMapper(false)
.build();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
}
然后在需要的地方进行依赖注入。需要注意为了区分ObjectMapper和XmlMapper,需要使用@Qualifier注解进行标记。
@Controller
public class MainController {
private ObjectMapper jsonMapper;
private XmlMapper xmlMapper;
private Person person = new Person("yitian", 10000, LocalDate.of(1994, 1, 1));
public MainController(@Autowired @Qualifier("json") ObjectMapper jsonMapper, @Autowired @Qualifier("xml") XmlMapper xmlMapper) {
this.jsonMapper = jsonMapper;
this.xmlMapper = xmlMapper;
}