Json序列化一般为实体转化生成的 JSON 数据中直接包含嵌套对象的属性
ObjectMapper mapper = new ObjectMapper();
Book book = new Book("Learning Java", "Java");
Writer writer = new Writer(110, "Mohit", book);
String jsonWriter = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(writer);
Json反序列化一般为JSON 数据映射到外层实体对象javabean的属性上
String jsonData = "{" +"\"writerId\" : 111," +"\"mywname\" : \"Mahesh\"," +"\"writerBook\" : {" +"\"bookName\" : \"Learning Spring\"," +"\"bkcat\" : \"Spring\"" +"}" +"}";
ObjectMapper mapper = new ObjectMapper();
Writer writer = mapper.readValue(jsonData, Writer.class);
@JsonProperty是用于定义逻辑属性的标记注释。一般被用于JSON的序列化和反序列化。
@JsonAlias为反序列化期间接受的属性定义一个或多个别名,即将JSON数据设置为Java对象。但是在序列化时,即从Java对象获取JSON时,只使用实际的逻辑属性名而不是别名。
@JsonDeserialize ;JSON字符串转JavaBean反序列化时, 将其他类型字段转化为BigDecimal类型
@JsonAlias({"passengerName", "newName"})
private String name;
@JsonProperty("type")
private String specification;
@JsonProperty("count")
@JsonDeserialize(using = BigDecimalDeSerialize.class)
private Bigdicimal amount;
标签:JsonProperty,JsonAlias,Java,JSON,序列化,ObjectMapper
From: https://www.cnblogs.com/chillymint/p/18202679