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

gu.sql2java.excel.json.JacksonDeserializeHandler Maven / Gradle / Ivy

There is a newer version: 5.3.2
Show newest version
package gu.sql2java.excel.json;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.base.Throwables;
import net.gdface.cache.FunctionCached;
import net.gdface.utils.Pair;

/**
 * JSON类型字段使用jJackson自定义反序列化器实现的数据导入适配器实现
 * @author guyadong
 * @since 3.29.0
 */
@SuppressWarnings("rawtypes")
public class JacksonDeserializeHandler extends BaseJsonDeserializeHandler {
	public JacksonDeserializeHandler() {
		super();
	}
	public JacksonDeserializeHandler(JsonDeserializer initDeserializer, Class initType, T initEmptyValue) {
		super(initDeserializer, initType, initEmptyValue);
	}
	@SuppressWarnings({ "unchecked" })
	private static final
	FunctionCached,ObjectMapper> 
	MAPPER_CACHE = FunctionCached.of(c->{
		try {
			ObjectMapper mapper = new ObjectMapper();
	        //通过simpleModule进行注册
	        SimpleModule simpleModule=new SimpleModule();
	        simpleModule.addDeserializer(c.left, c.right);
	        //注册simpleModule
	        mapper.registerModule(simpleModule);
	        return mapper;
		} catch (Exception e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		} 
	});

	@SuppressWarnings({ "unchecked" })
	protected Object deserialize(String value,JsonDeserializer deserializer,Class type) {
		 try {
			return MAPPER_CACHE.get(Pair.of(type, deserializer)).readValue(value,type);
		} catch (Exception e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		} 
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy