package com.bonus.hn.bearing.capacity.backstage.manager.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author bonus
 */
public class JacksonHelper implements JsonHelper {

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

	@Override
	public String jsonSerialize(Object value) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.writeValueAsString(value);
		} catch (IOException e) {
			logger.error("："+e.toString());
			e.printStackTrace();
			return null;
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T jsonDeserialize(String value, Class<?> tClass) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return (T) objectMapper.readValue(value, tClass);
		} catch (IOException e) {
			logger.error("："+e.toString());
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T jsonDeserialize(String value,
			TypeReference<T> typeReference) {
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return (T) objectMapper.readValue(value, typeReference);
		} catch (IOException e) {
			logger.error("："+e.toString());
			e.printStackTrace();
			return null;
		}
	}

}
