



package lambda.netty.loadbalancer.core.loadbalance;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;

import java.io.IOException;

public class JsonHelper<T> {
    final static Logger logger = Logger.getLogger(JsonHelper.class);


    private static ObjectMapper objectMapper = new ObjectMapper();
    private Class<T> t;

    protected JsonHelper(Class<T> t) {

        this.t = t;
    }


    public String ObjToJson(Object obj) {
        String tmp = null;
        try {
            tmp = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            logger.error("Cannot process the Object !", e);
        }
        return tmp;
    }

    public T jsonToObj(String str) {
        T tmp = null;
        try {
            tmp = objectMapper.readValue(str, t);
        } catch (IOException e) {
            logger.error("Cannot parse the String !", e);
        }
        return tmp;
    }

}
