Json 工具类

一、使用方法

(1/4)普通对象转Json

        DodoFile file = new DodoFile();
        file.setFileName("test.file");
        // 普通对象转Json
        String s = JacksonUtil.toJackson(file);
        System.err.println(s);

(2/4)Json转普通对象

        DodoFile file = new DodoFile();
        file.setFileName("test.file");
        String s = JacksonUtil.toJackson(file);
        System.err.println(s);
        // Json转普通对象
        DodoFile file2 = JacksonUtil.toObject(s, DodoFile.class);
        System.err.println(file2);

(3/4)集合对象转Json

        Map<String, DodoFile> files = new HashMap<String, DodoFile>();
        DodoFile file = new DodoFile();
        file.setFileName("test.file");
        files.put("key1", file);
        // 集合对象转Json
        String s = JacksonUtil.toJackson(files);
        System.err.println(s);

(4/4)Json转集合对象

        Map<String, DodoFile> files = new HashMap<String, DodoFile>();
        DodoFile file = new DodoFile();
        file.setFileName("test.file");
        files.put("key1", file);
        String s = JacksonUtil.toJackson(files);
        System.err.println(s);

        // Json转集合对象,方式1
        Map<String, DodoFile> files2 = JacksonUtil.toObject(s, new TypeReference<Map<String, DodoFile>>() {});
        System.err.println(files2);

        // Json转集合对象,方式2
        Map<String, DodoFile> files3 = JacksonUtil.toCollectionObject(s, Map.class, String.class, DodoFile.class);
        System.err.println(files3);

二、注释说明

public class JacksonUtil {
    private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    // Java对象转Json
    public static String toJackson(Object object) throws JsonProcessingException {
        return object == null ? null : OBJECT_MAPPER.writeValueAsString(object);
    }

    // Json转Java对象
    public static <T> T toObject(String jsonStr, Class<T> clazz) throws IOException {
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        return OBJECT_MAPPER.readValue(jsonStr, clazz);
    }

    // Json转Java对象,类型安全
    public static <T> T toObject(String jsonStr, TypeReference<?> typeReference) throws IOException,
            JsonParseException, JsonMappingException {
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        return OBJECT_MAPPER.readValue(jsonStr, typeReference);
    }
    // Json转Java集合对象,类型安全,可使用toObject(String jsonStr, TypeReference<?> typeReference) 替代
    public static <T> T toCollectionObject(String jsonStr, Class<?> collectionClass, Class<?>... elementClasses)
            throws IOException, JsonParseException, JsonMappingException {
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        return OBJECT_MAPPER.readValue(jsonStr, getCollectionType(collectionClass, elementClasses));
    }

    private static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

}
Copyright © DodoFramework 2020 all right reservedModify At: 2019-12-12 15:37:18