/**
* json string to Object
*
* @throws JsonProcessingException
*/
@Test
public void t1() throws JsonProcessingException {
String carJson = "{ " +
"\"brand\" : \"Mercedes\"," +
"\"doors\" : 5 " +
"}";
Car car = objectMapper.readValue(carJson, Car.class);
System.out.println("car brand = " + car.getBrand());
System.out.println("car doors = " + car.getDoors());
System.out.println(objectMapper.writeValueAsString(car));
}
/**
* Car{brand='ford', doors=0}
* Car{brand='Fiat', doors=0}
*
* @throws IOException
*/
@Test
public void t2_2() throws IOException {
String jsonArray = "[{\"brand\":\"ford\"}, {\"brand\":\"Fiat\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Car> cars1 = objectMapper.readValue(jsonArray, new TypeReference<List<Car>>() {
});
cars1.forEach(System.out::println);
}
/**
* json File to Object
*
* {"brand":"Mercedes","doors":5}
*
* @throws JsonProcessingException
*/
@Test
public void t2() throws IOException {
File file = new File("D:\\GitRepository\\multiProject\\Jackson\\src\\test\\resources\\json\\car.json");
Car car = objectMapper.readValue(file, Car.class);
System.out.println(objectMapper.writeValueAsString(car));
}
/**
* {
* "brand": "Mercedes",
* "doors": 5
* }
* tonels.domian.Car@78e67e0a
*
* @throws IOException
*/
@Test
public void t2_1() throws IOException {
String resource = "json/car.json";
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
String text = IOUtils.toString(is);
System.out.println(text);
final Car car = objectMapper.readValue(text, Car.class);
System.out.println(car.toString());
}
/**
* {brand=ford, doors=5}
*
* @throws IOException
*/
@Test
public void t2_3() throws IOException {
String jsonObject = "{\"brand\":\"ford\", \"doors\":5}";
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> jsonMap = objectMapper.readValue(jsonObject,
new TypeReference<Map<String, Object>>() {
});
System.out.println(jsonMap);
}
/**
* 写测试
* @throws IOException
*/
@Test
public void t3() throws IOException {
Car car = new Car();
car.setBrand("BMW" );
car.setDoors(4) ;
objectMapper.writeValue(
new FileOutputStream("json/output-2.json"), car);
}
/**
* {"brand":"BMW","doors":4}
* @throws IOException
*/
@Test
public void t3_1() throws IOException {
Car car = new Car();
car.setBrand("BMW" );
car.setDoors(4) ;
String json = objectMapper.writeValueAsString(car);
System.out.println(json);
}
/**
* {"type":"transfer","date":1591519578900}
* @throws IOException
*/
@Test
public void t4() throws IOException {
Transaction transaction = new Transaction("transfer", new Date());
String output = objectMapper.writeValueAsString(transaction);
System.out.println(output);
}
/**
* {"type":"transfer","date":"2020-06-07"}
* @throws IOException
*/
@Test
public void t4_1() throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
objectMapper.setDateFormat(dateFormat);
Transaction transaction = new Transaction("transfer", new Date());
String output2 = objectMapper.writeValueAsString(transaction);
System.out.println(output2);
}
/**
*
* brand = Mercedes
* doors = 5
* john =
* field = value
* @throws IOException
*/
@Test
public void t5_1() throws IOException {
String carJson =
"{ \"brand\" : \"Mercedes\", \"doors\" : 5," +
" \"owners\" : [\"John\", \"Jack\", \"Jill\"]," +
" \"nestedObject\" : { \"field\" : \"value\" } }";
try {
JsonNode jsonNode = objectMapper.readValue(carJson, JsonNode.class);
JsonNode brandNode = jsonNode.get("brand");
String brand = brandNode.asText();
System.out.println("brand = " + brand);
JsonNode doorsNode = jsonNode.get("doors");
int doors = doorsNode.asInt();
System.out.println("doors = " + doors);
JsonNode array = jsonNode.get("owners");
JsonNode jsonNode1 = array.get(0);
String john = jsonNode.asText();
System.out.println("john = " + john);
JsonNode child = jsonNode.get("nestedObject");
JsonNode childField = child.get("field");
String field = childField.asText();
System.out.println("field = " + field);
} catch (IOException e) {
e.printStackTrace();
}
}
这里主要包括 SerializationFeature 和 DeserializationFeature 两个枚举对象
待补充
待补充