【fasterxml】json 工具类

直接贴代码,方便下次使用。

/**
 * json 工具类
 * 

* Created by xlch at 2018/6/26 */ public class JsonUtil { private JsonUtil() { } private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class); private static final ObjectMapper mapper = new ObjectMapper(); static { // 序列化时 是否包含 null 字段:不包含 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //反序列化时是否忽略 不存在的属性值:忽略 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 下划线转为驼峰 mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE); } public static T json2Object(String source, Class classType) { try { return mapper.readValue(source, classType); } catch (IOException e) { LOGGER.debug("exception is ", e); throw new VastioException("json 读取出错"); } } public static List json2ObjectList(String source, Class classType) { JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, classType); try { return mapper.readValue(source, javaType); } catch (IOException e) { LOGGER.debug("exception is ", e); throw new VastioException("json list 读取出错"); } } public static String objects2Json(T source) { try { return mapper.writeValueAsString(source); } catch (JsonProcessingException e) { LOGGER.debug("exception is {}", e); throw new VastioException("json 写入出错"); } } }

 

你可能感兴趣的:(类库,joda,/guawa...)