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 <T>*/
    @Override
	public <T> byte[] serialize(T obj) {
		try {
			return objectMapper.writeValueAsBytes(obj);
		} catch (JsonProcessingException e) {
			throw new XxlRpcException(e);
		}
	}

    /** string --> bean、Map、List(array) */
    @Override
	public <T> Object deserialize(byte[] bytes, Class<T> 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);
		}
	}

}

@@ -397,16 +397,30 @@
 kson
-%E5%B7%A5%E5%85%B7%E7%B1%BB
+ serializer%0D%0A *
 %0D%0A * 
+%09%09
 1%E3%80%81ob
@@ -446,16 +446,23 @@
 set/get%EF%BC%9B
+%0D%0A * %09%09
 2%E3%80%81do not
@@ -484,16 +484,20 @@
 class%EF%BC%9B%0D%0A
+ *%0D%0A
  * @auth

update document