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

com.xxl.rpc.serialize.impl.JacksonSerializer Maven / Gradle / Ivy

There is a newer version: 1.8.1
Show newest version
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