Skip to content

Commit

Permalink
refactor jsonb support for springframework.
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorZeng authored and wenshao committed May 24, 2022
1 parent 4ba0bac commit 97c2f0d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class FastJsonConfig {
*/
private boolean writeContentLength;

/**
* JSONB flag.
*/
private boolean jsonb;

/**
* JSONB symbol table.
*/
Expand Down Expand Up @@ -173,11 +178,31 @@ public void setWriteContentLength(boolean writeContentLength) {
this.writeContentLength = writeContentLength;
}

/**
* Is jsonb boolean.
*
* @return the boolean
* @since 2.0.5
*/
public boolean isJsonb() {
return jsonb;
}

/**
* Sets jsonb flag.
*
* @param jsonb the jsonb
* @since 2.0.5
*/
public void setJsonb(boolean jsonb) {
this.jsonb = jsonb;
}

/**
* Gets symbol table.
*
* @return the symbol table
* @since 2.0.3
* @since 2.0.5
*/
public JSONB.SymbolTable getSymbolTable() {
return symbolTable;
Expand All @@ -187,7 +212,7 @@ public JSONB.SymbolTable getSymbolTable() {
* Sets symbol table.
*
* @param names the names
* @since 2.0.3
* @since 2.0.5
*/
public void setSymbolTable(String... names) {
this.symbolTable = JSONB.symbolTable(names);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,25 @@
package com.alibaba.fastjson2.support.spring.data.redis;

import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
* Fastjson(JSONB) for Spring Data Redis Serializer.
*
* @author Victor.Zxy
* @see RedisSerializer
* @see FastJsonRedisSerializer
* @since 2.0.3
*/
@Deprecated
public class FastJsonJSONBRedisSerializer<T>
implements RedisSerializer<T> {
private FastJsonConfig fastJsonConfig = new FastJsonConfig();
private final Class<T> type;

extends FastJsonRedisSerializer<T> {
public FastJsonJSONBRedisSerializer(Class<T> type) {
this.type = type;
}

public FastJsonConfig getFastJsonConfig() {
return fastJsonConfig;
}

public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
this.fastJsonConfig = fastJsonConfig;
super(type);
super.getFastJsonConfig().setJsonb(true);
}

@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
try {
return JSONB.toBytes(t, fastJsonConfig.getSymbolTable(), fastJsonConfig.getWriterFeatures());
} catch (Exception ex) {
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
}
}

@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
try {
return JSONB.parseObject(bytes, type, fastJsonConfig.getSymbolTable(), fastJsonConfig.getReaderFeatures());
} catch (Exception ex) {
throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
}
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
fastJsonConfig.setJsonb(true);
super.setFastJsonConfig(fastJsonConfig);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.fastjson2.support.spring.data.redis;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
Expand Down Expand Up @@ -36,8 +37,12 @@ public byte[] serialize(T t) throws SerializationException {
return new byte[0];
}
try {
return JSON.toJSONBytes(t, fastJsonConfig.getDateFormat(),
fastJsonConfig.getWriterFilters(), fastJsonConfig.getWriterFeatures());
if (fastJsonConfig.isJsonb()) {
return JSONB.toBytes(t, fastJsonConfig.getSymbolTable(), fastJsonConfig.getWriterFeatures());
} else {
return JSON.toJSONBytes(t, fastJsonConfig.getDateFormat(),
fastJsonConfig.getWriterFilters(), fastJsonConfig.getWriterFeatures());
}
} catch (Exception ex) {
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
}
Expand All @@ -49,8 +54,12 @@ public T deserialize(byte[] bytes) throws SerializationException {
return null;
}
try {
return JSON.parseObject(bytes, type,
fastJsonConfig.getDateFormat(), fastJsonConfig.getReaderFeatures());
if (fastJsonConfig.isJsonb()) {
return JSONB.parseObject(bytes, type, fastJsonConfig.getSymbolTable(), fastJsonConfig.getReaderFeatures());
} else {
return JSON.parseObject(bytes, type,
fastJsonConfig.getDateFormat(), fastJsonConfig.getReaderFeatures());
}
} catch (Exception ex) {
throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
package com.alibaba.fastjson2.support.spring.data.redis;

import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
* Fastjson(JSONB) for Spring Data Redis Serializer(Generic implement).
*
* @author Victor.Zxy
* @see RedisSerializer
* @see GenericFastJsonRedisSerializer
* @since 2.0.3
*/
@Deprecated
public class GenericFastJsonJSONBRedisSerializer
implements RedisSerializer<Object> {
private final FastJsonConfig fastJsonConfig = new FastJsonConfig();

extends GenericFastJsonRedisSerializer {
public GenericFastJsonJSONBRedisSerializer() {
fastJsonConfig.setReaderFeatures(JSONReader.Feature.SupportAutoType);
fastJsonConfig.setWriterFeatures(JSONWriter.Feature.WriteClassName);
}

@Override
public byte[] serialize(Object object) throws SerializationException {
if (object == null) {
return new byte[0];
}
try {
return JSONB.toBytes(object, fastJsonConfig.getWriterFeatures());
} 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 JSONB.parseObject(bytes, Object.class, fastJsonConfig.getReaderFeatures());
} catch (Exception ex) {
throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
}
super(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.fastjson2.support.spring.data.redis;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
Expand All @@ -24,13 +25,22 @@ public GenericFastJsonRedisSerializer() {
fastJsonConfig.setWriterFeatures(JSONWriter.Feature.WriteClassName);
}

public GenericFastJsonRedisSerializer(boolean jsonb) {
this();
fastJsonConfig.setJsonb(jsonb);
}

@Override
public byte[] serialize(Object object) throws SerializationException {
if (object == null) {
return new byte[0];
}
try {
return JSON.toJSONBytes(object, fastJsonConfig.getWriterFeatures());
if (fastJsonConfig.isJsonb()) {
return JSONB.toBytes(object, fastJsonConfig.getWriterFeatures());
} else {
return JSON.toJSONBytes(object, fastJsonConfig.getWriterFeatures());
}
} catch (Exception ex) {
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
}
Expand All @@ -42,7 +52,11 @@ public Object deserialize(byte[] bytes) throws SerializationException {
return null;
}
try {
return JSON.parseObject(bytes, Object.class, fastJsonConfig.getReaderFeatures());
if (fastJsonConfig.isJsonb()) {
return JSONB.parseObject(bytes, Object.class, fastJsonConfig.getReaderFeatures());
} else {
return JSON.parseObject(bytes, Object.class, fastJsonConfig.getReaderFeatures());
}
} catch (Exception ex) {
throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
package com.alibaba.fastjson2.support.spring.messaging.converter;

import com.alibaba.fastjson2.JSONB;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.springframework.util.Assert;

import java.lang.reflect.Type;

/**
* Fastjson(JSONB) for Spring Messaging Json Converter.
*
* @author Victor.Zxy
* @see MappingFastJsonMessageConverter
* @since 2.0.5
*/
@Deprecated
public class MappingFastJsonJSONBMessageConverter
extends MappingFastJsonMessageConverter {
@Override
public void setSerializedPayloadClass(Class<?> payloadClass) {
Assert.isTrue(byte[].class == payloadClass,
() -> "Payload class must be byte[] : " + payloadClass);
public MappingFastJsonJSONBMessageConverter() {
super.getFastJsonConfig().setJsonb(true);
}

@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
// parse byte[] payload to Java Object
Object obj = null;
Type type = getResolvedType(targetClass, conversionHint);
Object payload = message.getPayload();
if (payload instanceof byte[]) {
obj = JSONB.parseObject((byte[]) payload, type, fastJsonConfig.getSymbolTable(), fastJsonConfig.getReaderFeatures());
}
return obj;
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
fastJsonConfig.setJsonb(true);
super.setFastJsonConfig(fastJsonConfig);
}

@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
// encode payload to json byte[]
Object obj = null;
if (byte[].class == getSerializedPayloadClass()) {
if (payload instanceof String) {
obj = JSONB.fromJSONString((String) payload);
} else {
obj = JSONB.toBytes(payload, fastJsonConfig.getSymbolTable(), fastJsonConfig.getWriterFeatures());
}
}
return obj;
public void setSerializedPayloadClass(Class<?> payloadClass) {
Assert.isTrue(byte[].class == payloadClass,
() -> "Payload class must be byte[] : " + payloadClass);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.fastjson2.support.spring.messaging.converter;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
Expand All @@ -25,7 +26,7 @@ public class MappingFastJsonMessageConverter
/**
* with fastJson config
*/
protected FastJsonConfig fastJsonConfig = new FastJsonConfig();
private FastJsonConfig fastJsonConfig = new FastJsonConfig();

/**
* default support application/json
Expand Down Expand Up @@ -59,9 +60,17 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, O
Object obj = null;
Type type = getResolvedType(targetClass, conversionHint);
Object payload = message.getPayload();
if (payload instanceof byte[]) {

if (payload instanceof byte[] && fastJsonConfig.isJsonb()) {

obj = JSONB.parseObject((byte[]) payload, type, fastJsonConfig.getSymbolTable(), fastJsonConfig.getReaderFeatures());

} else if (payload instanceof byte[] && !fastJsonConfig.isJsonb()) {

obj = JSON.parseObject((byte[]) payload, type, fastJsonConfig.getDateFormat(), fastJsonConfig.getReaderFeatures());
} else if (payload instanceof String) {

} else if (payload instanceof String && JSON.isValid((String) payload)) {

obj = JSON.parseObject((String) payload, type, fastJsonConfig.getDateFormat(), fastJsonConfig.getReaderFeatures());
}

Expand All @@ -71,17 +80,34 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, O
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
// encode payload to json string or byte[]
Object obj;
Object obj = null;

if (byte[].class == getSerializedPayloadClass()) {
if (payload instanceof String && JSON.isValid((String) payload)) {

if (payload instanceof String && fastJsonConfig.isJsonb()) {

obj = JSONB.fromJSONString((String) payload);

} else if (payload instanceof String && !fastJsonConfig.isJsonb()) {

obj = ((String) payload).getBytes(fastJsonConfig.getCharset());

} else if (fastJsonConfig.isJsonb()) {

obj = JSONB.toBytes(payload, fastJsonConfig.getSymbolTable(), fastJsonConfig.getWriterFeatures());

} else {

obj = JSON.toJSONBytes(payload, fastJsonConfig.getDateFormat(), fastJsonConfig.getWriterFilters(), fastJsonConfig.getWriterFeatures());
}
} else {
} else if (String.class == getSerializedPayloadClass()) {

if (payload instanceof String && JSON.isValid((String) payload)) {

obj = payload;

} else {

obj = JSON.toJSONString(payload, fastJsonConfig.getDateFormat(), fastJsonConfig.getWriterFilters(), fastJsonConfig.getWriterFeatures());
}
}
Expand Down
Loading

0 comments on commit 97c2f0d

Please sign in to comment.