1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.joda.JodaModule; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.util.List; import java.util.Map;
public class JsonUtil {
private final static Logger logger = LoggerFactory.getLogger(JsonUtil.class);
private final static ObjectMapper objectMapper = new ObjectMapper(); static {
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true); objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); objectMapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true); objectMapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.registerModule(new JodaModule()); }
public static String encode(Object object) { try { return objectMapper.writeValueAsString(object); } catch (IOException e) { logger.error("jackson encode error, obj = {}", object, e); return "jackson encode error"; } }
public static <T> T decode(String json, Class<T> valueType) { try { return objectMapper.readValue(json, valueType); } catch (Exception e) { logger.error("jackson decode error, json = {}, class = {}", json, valueType.getName(), e); return null; } }
public static JsonNode readTree(String json) throws IOException { try { return objectMapper.readTree(json); } catch (IOException e) { logger.error("jackson readTree error, json = {}", json, e); return null; } }
public static <T> List<T> decodeList(String json) throws IOException { try { return objectMapper.readValue(json, new TypeReference<List<T>>() { }); } catch (IOException e) { logger.error("jackson decodeList(String) error, json = {}", json, e); return null; } }
public static <T> List<T> decodeList(String json, Class<T> clazz) throws IOException { try { return objectMapper.readValue(json, objectMapper.getTypeFactory() .constructCollectionType(List.class, clazz)); } catch (IOException e) { logger.error("jackson decodeList(String, Class<T>) error, json = {}, class = {}", json, clazz.getName(), e); return null; } }
public static <K, V> Map<K, V> decodeMap(String json) throws IOException { try { return objectMapper.readValue(json, new TypeReference<Map<K, V>>() { }); } catch (IOException e) { logger.error("jackson decodeMap(String) error, json = {}", json, e); return null; } }
public static <K, V> Map<K, V> decodeMap(String json, Class<K> key, Class<V> value) throws IOException { try { return objectMapper.readValue(json, objectMapper.getTypeFactory() .constructMapType(Map.class, key, value)); } catch (IOException e) { logger.error("jackson decodeMap(String, Class<K>, Class<V>) error, json = {}, key = {}, " + "value = {}", json, key.getName(), value.getName(), e); return null; } }
}
|