<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-dataformat-xmlartifactId>
<version>2.14.2version>
dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String color;
private String type;
}
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("D:/projects/tutorails_kanomoku/business-prac/src/main/java/atomicitybusiness/method/output/car.json"), car);
以下内容输出到文件
{"color":"yellow","type":"renault"}
ObjectMapper objectMapper = new ObjectMapper();
Car car = new Car("yellow", "renault");
String carAsString = objectMapper.writeValueAsString(car);
// {"color":"yellow","type":"renault"}
ObjectMapper objectMapper = new ObjectMapper();
Car car = objectMapper.readValue(new URL("file:D:/projects/tutorails_kanomoku/business-prac/src/main/java/atomicitybusiness/method/output/car.json"), Car.class);
System.out.println(car);
// Car(color=Black, type=BMW)
ObjectMapper objectMapper = new ObjectMapper();
Car car = objectMapper.readValue(new File("D:/projects/tutorails_kanomoku/business-prac/src/main/java/atomicitybusiness/method/output/car.json"), Car.class);
System.out.println(car);
// Car(color=Black, type=BMW)
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Car car = objectMapper.readValue(json, Car.class);
System.out.println(car); // Car(color=Black, type=BMW)
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"FIAT\" }";
JsonNode jsonNode = objectMapper.readTree(json);
String color = jsonNode.get("color").asText();
System.out.println(color); // Black
ObjectMapper objectMapper = new ObjectMapper();
String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]";
List<Car> listCar = objectMapper.readValue(jsonCarArray, new TypeReference<List<Car>>() {});
System.out.println(listCar);
// [Car(color=Black, type=BMW), Car(color=Red, type=FIAT)]
ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
System.out.println(map);
// {color=Black, type=BMW}
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Car car = objectMapper.readValue(jsonString, Car.class);
System.out.println(car); // Car(color=Black, type=Fiat)
JsonNode jsonNodeRoot = objectMapper.readTree(jsonString);
JsonNode jsonNodeYear = jsonNodeRoot.get("year");
String year = jsonNodeYear.asText();
System.out.println(year); // 1970
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false);
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();
}
}
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);
Car car = new Car("yellow", "renault");
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)
Read More Jackson Date
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);
// output: {"car":{"color":"yellow","type":"renault"},"datePurchased":"2016-07-03 11:43 AM CEST"}
-----------------------------------------------------------------------------读书笔记摘自 文章:Intro to the Jackson ObjectMapper