forked from alibaba/fastjson2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor jsonb support for springframework.
- Loading branch information
1 parent
4ba0bac
commit 97c2f0d
Showing
8 changed files
with
115 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 8 additions & 38 deletions
46
...in/java/com/alibaba/fastjson2/support/spring/data/redis/FastJsonJSONBRedisSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 4 additions & 37 deletions
41
.../com/alibaba/fastjson2/support/spring/data/redis/GenericFastJsonJSONBRedisSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 11 additions & 29 deletions
40
...ba/fastjson2/support/spring/messaging/converter/MappingFastJsonJSONBMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.