package com.huimv.admin.converter;

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

import javax.persistence.AttributeConverter;

public abstract class JpaJsonConverter<T> implements AttributeConverter<T, String> {

	
	protected final static ObjectMapper objectMapper = new ObjectMapper();

	@Override
	public String convertToDatabaseColumn(T meta) {
		try {
			if(meta == null) return null;
			return objectMapper.writeValueAsString(meta);
		} catch (JsonProcessingException ex) {
			ex.printStackTrace();
			return null;
			// or throw an error
		}
	}

}
