How To Use JsonMapper Replace ObjectMapper

How To Use JsonMapper Replace ObjectMapper

這篇介紹How To Use JsonMapper Replace ObjectMapper。

before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected final static ObjectMapper mapper = new ObjectMapper();
protected final static JsonFactory jsonFactory;

static {
jsonFactory = mapper.getFactory();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
mapper.configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false);
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
DateFormat DATE_FORMAT_ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DATE_FORMAT_ISO8601.setTimeZone(TimeZone.getTimeZone("Asia/Taipei"));
mapper.setDateFormat(DATE_FORMAT_ISO8601);
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
}

after

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected static JsonMapper mapper;
protected final static JsonFactory jsonFactory;

static {
DateFormat DATE_FORMAT_ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DATE_FORMAT_ISO8601.setTimeZone(TimeZone.getTimeZone("Asia/Taipei"));
mapper = JsonMapper.builder().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false)
.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false)
.configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false)
.serializationInclusion(Include.NON_NULL).defaultDateFormat(DATE_FORMAT_ISO8601).build();
jsonFactory = mapper.getFactory();
}