com.feingto.iot.common.json.JSON Maven / Gradle / Ivy
package com.feingto.iot.common.json;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.util.StringUtils;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
/**
* json字符串、对象、List转换工具类
*
* @author longfei
*/
@Slf4j
@SuppressWarnings("unchecked")
public class JSON {
private static JSON instance;
private JSONObjectMapper objectMapper = new JSONObjectMapper();
private XmlMapper xmlMapper = new XmlMapper();
public static JSON getInstance() {
if (null == instance) {
instance = new JSON();
}
return instance;
}
public static GenericJackson2JsonRedisSerializer Jackson2JsonRedisSerializer() {
//return Jackson2JsonRedisSerializer(new JSONObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL));
return Jackson2JsonRedisSerializer(new JSONObjectMapper());
}
public static GenericJackson2JsonRedisSerializer Jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
public JSON objectMapper(JSONObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
public JSON xmlMapper(XmlMapper xmlMapper) {
this.xmlMapper = xmlMapper;
return this;
}
/**
* 过滤属性,配置@JsonFilter(filterName)使用
*/
public JSON filter(String filterName, String... properties) {
FilterProvider filterProvider = new SimpleFilterProvider().addFilter(filterName,
SimpleBeanPropertyFilter.serializeAllExcept(properties));
objectMapper.setFilterProvider(filterProvider);
return this;
}
public ObjectNode JSONObject() {
return objectMapper.createObjectNode();
}
public ArrayNode JSONArray() {
return objectMapper.createArrayNode();
}
@SneakyThrows
public JsonNode read(String json) {
return StringUtils.hasText(json) ? objectMapper.readTree(json) : JSONObject();
}
@SneakyThrows
public JsonNode readXml(String json) {
return StringUtils.hasText(json) ? xmlMapper.readTree(json) : JSONObject();
}
/**
* byte[] read to JsonNode
*/
@SneakyThrows
public JsonNode read(byte[] bytes) {
return objectMapper.readTree(bytes);
}
/**
* inputStream read to JsonNode
*/
@SneakyThrows
public JsonNode read(InputStream inputStream) {
return objectMapper.readTree(inputStream);
}
/**
* json string convert to xml string
*/
@SneakyThrows
public String json2xml(String json) {
return StringUtils.hasText(json) ? xmlMapper.writeValueAsString(read(json)) : "";
}
/**
* pojo convert to json string
*/
@SneakyThrows
public String obj2json(Object pojo) {
return pojo != null ? objectMapper.writeValueAsString(pojo) : "";
}
/**
* json string convert to javaBean
*/
@SneakyThrows
public T json2pojo(String json, Class clazz) {
return StringUtils.hasText(json) ? objectMapper.readValue(json, clazz) : null;
}
/**
* json string convert to map with javaBean
*/
public Map json2map(String json, Class clazz)
throws Exception {
Map> map = objectMapper.readValue(json,
new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy