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

com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer Maven / Gradle / Ivy

Go to download

Fastjson is a JSON processor (JSON parser + JSON generator) written in Java

There is a newer version: 2.0.53
Show newest version
package com.alibaba.fastjson.support.spring;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.ContextAutoTypeBeforeHandler;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * Fastjson for Spring Data Redis Serializer(Generic implement).
 * 

* Compatible fastjson 1.2.x * * @author lihengming * @author Victor.Zxy * @see RedisSerializer * @since 2.0.2 */ public class GenericFastJsonRedisSerializer implements RedisSerializer { ContextAutoTypeBeforeHandler contextFilter; public GenericFastJsonRedisSerializer() { this.contextFilter = null; } public GenericFastJsonRedisSerializer(String[] acceptNames) { contextFilter = new ContextAutoTypeBeforeHandler(acceptNames); } @Override public byte[] serialize(Object object) throws SerializationException { if (object == null) { return new byte[0]; } try { return JSON.toJSONBytes(object, SerializerFeature.WriteClassName); } catch (Exception ex) { throw new SerializationException("Could not serialize: " + ex.getMessage(), ex); } } @Override public Object deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length == 0) { return null; } try { return JSON.parseObject(bytes, Object.class, contextFilter, Feature.SupportAutoType); } catch (Exception ex) { throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex); } } }