
com.xxl.rpc.serialize.impl.JacksonSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xxl-rpc-core Show documentation
Show all versions of xxl-rpc-core Show documentation
A high performance, distributed RPC framework.
package com.xxl.rpc.serialize.impl;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxl.rpc.serialize.Serializer;
import com.xxl.rpc.util.XxlRpcException;
import java.io.IOException;
/**
* Jackson工具类
* 1、obj need private and set/get;2、do not support inner class;
* @author xuxueli 2015-9-25 18:02:56
*/
public class JacksonSerializer extends Serializer {
private final static ObjectMapper objectMapper = new ObjectMapper();
/** bean、array、List、Map --> json
* @param */
@Override
public byte[] serialize(T obj) {
try {
return objectMapper.writeValueAsBytes(obj);
} catch (JsonProcessingException e) {
throw new XxlRpcException(e);
}
}
/** string --> bean、Map、List(array) */
@Override
public Object deserialize(byte[] bytes, Class clazz) {
try {
return objectMapper.readValue(bytes, clazz);
} catch (JsonParseException e) {
throw new XxlRpcException(e);
} catch (JsonMappingException e) {
throw new XxlRpcException(e);
} catch (IOException e) {
throw new XxlRpcException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy