jsonトランスオブジェクト、オブジェクトトランスjson列

3919 ワード

/**
 * json     
 */
public class JsonUtils {


    public static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    private static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
        mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
        mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    public static ObjectMapper getMapper() {
        return mapper;
    }

    public static void setMapper(ObjectMapper mapper) {
        JsonUtils.mapper = mapper;
    }

    
    public static String returnJsonString(Object pojo) {
        String rs = null;
        try {
            rs = mapper.writeValueAsString(pojo);
        } catch (JsonProcessingException e) {
            logger.error("error in returnJsonString due to {}" + e);
        }
        return rs;
    }

    
    public static Object returnObject(String jsonStr, Class objClass) {
        if (jsonStr == null) {
            return null;
        }
        Object obj = null;
        try {
            obj = mapper.readValue(jsonStr, objClass);
        } catch (JsonParseException e) {
            logger.info(logger.getName() + "-->returnObject exception:" + e);
        } catch (JsonMappingException e) {
            logger.info(logger.getName() + "-->returnObject exception:" + e);
        } catch (IOException e) {
            logger.info(logger.getName() + "-->returnObject exception:" + e);
        } finally {
            return obj;
        }
    }

   
    public static JsonNode returnJsonNode(String jsonString) {
        JsonNode jsonNode = null;
        try {
            jsonNode = mapper.readTree(jsonString);
        } catch (IOException e) {
            logger.error("error in returnJsonNode due to {}" + e);
        }
        return jsonNode;
    }

   
    public static T returnType(String jsonStr, TypeReference valueTypeRef) {
        if (jsonStr == null) {
            return null;
        }
        T t = null;
        try {
            t = mapper.readValue(jsonStr, valueTypeRef);
        } catch (JsonParseException e) {
            logger.info(logger.getName() + "-->returnType:" + e);
        } catch (JsonMappingException e) {
            logger.info(logger.getName() + "-->returnType:" + e);
        } catch (IOException e) {
            logger.info(logger.getName() + "-->returnType:" + e);
        } finally {
            return t;
        }
    }

    public static  T readValue(JsonNode rootJsonNode, List fieldNames, Class valueType) {

        JsonNode jsonNode = getJsonNode(rootJsonNode, fieldNames);

        if (jsonNode == null || valueType == null) {
            return null;
        }

        try {
            return mapper.readerFor(valueType).readValue(jsonNode);
        } catch (IOException e) {
            logger.info(logger.getName() + "-->readValue:" + e.getMessage());
        }

        return null;
    }

  
    public static JsonNode getJsonNode(JsonNode rootJsonNode, List fieldNames) {
        JsonNode jsonNode = rootJsonNode;

        for (int i = 0; i < fieldNames.size(); i++) {
            if (jsonNode.has(fieldNames.get(i))) {
                jsonNode = jsonNode.get(fieldNames.get(i));
            } else {
                return null;
            }
        }

        return jsonNode;
    }


}