maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.14.2</version>
</dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String color;
private String type;
}
Java Object to JSON
Json JavaObject 互相转换 之 序列化(jackson Java Object to JSON)
JSON to Java Object
Json JavaObject 互相转换 之 反序列化(jackson Json to Java Object)
JSON to Jackson JsonNode
Json JavaObject 互相转换 之 jackson json to JsonNode
Creating a Java List From a JSON Array String
Json JavaObject 互相转换 之 jackson Json to List
Creating Java Map From JSON String
Json JavaObject 互相转换 之 jackson Json to Map
Configuring Serialization or Deserialization Feature
Json JavaObject 互相转换 之 序列化定制化用法
Json JavaObject 互相转换 之 反序列化定制化用法
Creating Custom Serializer or Deserializer
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) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("car_brand", car.getType());
jsonGenerator.writeEndObject();
}
}
Car car = new Car("yellow", "renault");
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("CustomCarSerializer", new Version(1, 0, 0, null, null, null));
module.addSerializer(Car.class, new CustomCarSerializer());
mapper.registerModule(module);
String carJson = mapper.writeValueAsString(car);
System.out.println(carJson);
// {"car_brand":"renault"}
public class CustomCarDeserializer extends StdDeserializer<Car> {
public CustomCarDeserializer() {
this(null);
}
public CustomCarDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Car deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException {
Car car = new Car();
ObjectCodec codec = parser.getCodec();
JsonNode node = codec.readTree(parser);
JsonNode colorNode = node.get("color");
String color = colorNode.asText();
car.setColor(color);
return car;
}
}
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null));
module.addDeserializer(Car.class, new CustomCarDeserializer());
mapper.registerModule(module);
Car car = mapper.readValue(json, Car.class);
System.out.println(car); // Car(color=Black, type=null)
Handling Date Formats
Read More Jackson Date文章来源:https://www.toymoban.com/news/detail-628428.html
public class Request
{
private Car car;
private Date datePurchased;
// standard getters setters
}
ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
objectMapper.setDateFormat(df);
String carAsString = objectMapper.writeValueAsString(request);
// {"car":{"color":"yellow","type":"renault"},"datePurchased":"2016-07-03 11:43 AM CEST"}
-----------------------------------------------------------------------------读书笔记摘自 文章:Intro to the Jackson ObjectMapper文章来源地址https://www.toymoban.com/news/detail-628428.html
到了这里,关于ObjectMapper常见用法(Intro to the Jackson ObjectMapper)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!