123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.inet.ailink.receiver.common.utils;
-
- import com.fasterxml.jackson.annotation.JsonInclude.Include;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.DeserializationFeature;
- import com.fasterxml.jackson.databind.JavaType;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializationFeature;
- import com.fasterxml.jackson.databind.util.JSONPObject;
-
- import java.io.IOException;
-
-
- public class JsonMapper {
-
- private ObjectMapper mapper;
-
- public JsonMapper() {
- this(null);
- }
-
- public JsonMapper(Include include) {
- mapper = new ObjectMapper();
- if (include != null) {
- mapper.setSerializationInclusion(include);
- }
- mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
- }
-
-
- public static JsonMapper nonEmptyMapper() {
- return new JsonMapper(Include.NON_EMPTY);
- }
-
-
- public static JsonMapper nonDefaultMapper() {
- return new JsonMapper(Include.NON_DEFAULT);
- }
-
-
- public static JsonMapper nonAlwaysMapper() {
- return new JsonMapper(Include.ALWAYS);
- }
-
-
- public String toJson(Object object) {
-
- try {
- return mapper.writeValueAsString(object);
- } catch (IOException e) {
- return null;
- }
- }
-
- public <T> T fromMapToObject(Object map, Class<T> cls) {
- return this.fromJson(this.toJson(map), cls);
- }
- public <T> T fromMapToObject(Object map, JavaType cls) {
- return this.fromJson(this.toJson(map), cls);
- }
-
-
- public <T> T fromJson(String jsonString, Class<T> clazz) {
- if (isEmpty(jsonString)) {
- return null;
- }
-
- try {
- return mapper.readValue(jsonString, clazz);
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
-
-
- public <T> T fromJson(String jsonString, JavaType javaType) {
- if (isEmpty(jsonString)) {
- return null;
- }
-
- try {
- return (T) mapper.readValue(jsonString, javaType);
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
-
-
- public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
- return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
- }
-
-
- public <T> T update(String jsonString, T object) {
- try {
- return (T) mapper.readerForUpdating(object).readValue(jsonString);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
-
- public String toJsonP(String functionName, Object object) {
- return toJson(new JSONPObject(functionName, object));
- }
-
-
- public void enableEnumUseToString() {
- mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
- mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
- }
-
-
- public ObjectMapper getMapper() {
- return mapper;
- }
-
- public static boolean isEmpty(String str) {
- return str == null || str.length() == 0;
- }
- }
|