import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Test;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class ObjectMapperReadDemo {
* Read Object From JSON String
* 从 JSON 字符串读取对象
* @throws JsonProcessingException
@Test
public void test1() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
System.out.println("************ Read Object From JSON String ************");
Car car = objectMapper.readValue(carJson, Car.class);
System.out.println(car);
* Read Object From JSON Reader
* 从 JSON Reader 读取对象
* @throws JsonProcessingException
@Test
public void test2() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
Reader reader = new StringReader(carJson);
Car car = objectMapper.readValue(reader, Car.class);
System.out.println(car);
* Read Object From JSON File
* 从 JSON 文件读取对象
* @throws JsonProcessingException
@Test
public void test3() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
File file = new File("src/data/car.json");
Car car = objectMapper.readValue(file, Car.class);
System.out.println(car);
* Read Object From JSON via URL
* 通过 URL 从 JSON 读取对象
* @throws JsonProcessingException
@Test
public void test4() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
URL url = new URL("file:src/data/car.json");
Car car = objectMapper.readValue(url, Car.class);
System.out.println(car);
* Read Object From JSON InputStream
* 从 JSON InputStream 读取对象
* @throws JsonProcessingException
@Test
public void test5() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
InputStream input = new FileInputStream("src/data/car.json");
Car car = objectMapper.readValue(input, Car.class);
System.out.println(car);
* Read Object From JSON Byte Array
* 从 JSON 字节数组读取对象
* @throws JsonProcessingException
@Test
public void test6() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
byte[] bytes = carJson.getBytes(StandardCharsets.UTF_8);
Car car = objectMapper.readValue(bytes, Car.class);
System.out.println(car);
* Read Object Array From JSON Array String
* 从 JSON 数组字符串读取对象数组
* @throws JsonProcessingException
@Test
public void test7() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonArray = "[{\"brand\":\"ford\"}, {\"brand\":\"Fiat\"}]";
Car[] cars = objectMapper.readValue(jsonArray, Car[].class);
System.out.println(Arrays.toString(cars));
* Read Object List From JSON Array String
* 从 JSON 数组字符串读取对象列表
* @throws JsonProcessingException
@Test
public void test8() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonArray = "[{\"brand\":\"ford\"}, {\"brand\":\"Fiat\"}]";
List<Car> cars = objectMapper.readValue(jsonArray, new TypeReference<List<Car>>() {
System.out.println(cars);
* Read Map from JSON String
* 从 JSON 字符串读取 Map
* @throws JsonProcessingException
@Test
public void test9() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonObject = "{\"brand\":\"ford\", \"doors\":5}";
Map<String, Object> jsonMap = objectMapper.readValue(jsonObject, new TypeReference<Map<String, Object>>() {
System.out.println(jsonMap);
* Ignore Unknown JSON Fields
* 忽略未知的 JSON 字段
* @throws JsonProcessingException
@Test
public void test10() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : null, \"test\" : 10 }";
* 确定遇到未知属性(未映射到属性的属性,并且没有“任何设置器”或可以处理它的处理程序)是否会导致失败(通过抛出JsonMappingException )
* 的功能。
* 此设置仅在尝试了所有其他未知属性处理方法后生效,并且属性保持未处理状态。
* 默认情况下启用功能(意味着如果遇到未知属性将抛出JsonMappingException )
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Car car = objectMapper.readValue(carJson, Car.class);
System.out.println(car);
* Fail on Null JSON Values for Primitive Types
* 原始类型的空 JSON 值失败
* @throws JsonProcessingException
@Test
public void test11() throws JsonProcessingException {
System.out.println("************ Ignore Unknown JSON Fields ************");
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : null}";
ObjectMapper objectMapper = new ObjectMapper();
* 确定在反序列化为 Java 基本类型(如“int”或“double”)时遇到 JSON null 是否为错误的功能。
* 如果是,则抛出 JsonProcessingException 以指示这一点;
* 如果不是,则使用默认值('int' 为 0,double 为 0.0,与 JVM 使用的默认值相同)。
* 默认情况下禁用该功能。
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
Car car = objectMapper.readValue(carJson, Car.class);
System.out.println(car);
* Custom Deserializer
* 自定义反序列号器
@Test
public void test12() throws JsonProcessingException {
String json = "{ \"brand\" : \"Ford\", \"doors\" : 6 }";
SimpleModule module = new SimpleModule("CarDeserializer",
new Version(3, 1, 8, null, null, null));
module.addDeserializer(Car.class, new StdDeserializer<Car>(Car.class){
@Override
public Car deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException, JsonProcessingException {
Car car = new Car();
while(!parser.isClosed()){
JsonToken jsonToken = parser.nextToken();
if(JsonToken.FIELD_NAME.equals(jsonToken)){
String fieldName = parser.getCurrentName();
System.out.println(fieldName);
jsonToken = parser.nextToken();
if("brand".equals(fieldName)){
car.setBrand(parser.getValueAsString());
} else if ("doors".equals(fieldName)){
car.setDoors(parser.getValueAsInt());
return car;
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
Car car = mapper.readValue(json, Car.class);
System.out.println(car);