All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.attemper.alarm.BeanUtil Maven / Gradle / Ivy

The newest version!
package com.github.attemper.alarm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BeanUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static Map bean2Map(Object obj) {
        if (obj == null) {
            return new HashMap();
        }
        try {
            return objectMapper.readValue(bean2JsonStr(obj), Map.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static  T map2Bean (Class t, Map map) {
        try {
            return objectMapper.readValue(bean2JsonStr(map), t);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String bean2JsonStr(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static  T jsonStr2Bean(String jsonStr, Class t) {
        try {
            return objectMapper.readValue(jsonStr, t);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy