mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
feat: 为序列化器新增 objectToBytes
、bytesToObject
方法
This commit is contained in:
parent
d551066238
commit
048dadaff7
@ -39,4 +39,20 @@ public interface SaSerializerTemplate {
|
||||
*/
|
||||
Object stringToObject(String str);
|
||||
|
||||
/**
|
||||
* 序列化:对象 -> 字节数组
|
||||
*
|
||||
* @param obj /
|
||||
* @return /
|
||||
*/
|
||||
byte[] objectToBytes(Object obj);
|
||||
|
||||
/**
|
||||
* 反序列化:字节数组 → 对象
|
||||
*
|
||||
* @param bytes /
|
||||
* @return /
|
||||
*/
|
||||
Object bytesToObject(byte[] bytes);
|
||||
|
||||
}
|
||||
|
@ -30,6 +30,24 @@ public interface SaSerializerTemplateForJdk extends SaSerializerTemplate {
|
||||
|
||||
@Override
|
||||
default String objectToString(Object obj) {
|
||||
byte[] bytes = objectToBytes(obj);
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
return bytesToString(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
default Object stringToObject(String str) {
|
||||
if(str == null) {
|
||||
return null;
|
||||
}
|
||||
byte[] bytes = stringToBytes(str);
|
||||
return bytesToObject(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
default byte[] objectToBytes(Object obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
@ -38,28 +56,26 @@ public interface SaSerializerTemplateForJdk extends SaSerializerTemplate {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos)
|
||||
) {
|
||||
oos.writeObject(obj);
|
||||
byte[] bytes = baos.toByteArray();
|
||||
return bytesToString(bytes);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e) {
|
||||
throw new SaTokenException(e);
|
||||
}
|
||||
}
|
||||
throw new SaTokenException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
default Object stringToObject(String str) {
|
||||
if(str == null) {
|
||||
default Object bytesToObject(byte[] bytes) {
|
||||
if(bytes == null) {
|
||||
return null;
|
||||
}
|
||||
byte[] data = stringToBytes(str);
|
||||
try (
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(bais)
|
||||
) {
|
||||
return ois.readObject();
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
throw new SaTokenException(e);
|
||||
}
|
||||
}
|
||||
throw new SaTokenException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* byte[] 转换为 String
|
||||
|
@ -16,6 +16,7 @@
|
||||
package cn.dev33.satoken.serializer.impl;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.exception.ApiDisabledException;
|
||||
import cn.dev33.satoken.serializer.SaSerializerTemplate;
|
||||
|
||||
/**
|
||||
@ -36,4 +37,14 @@ public class SaSerializerTemplateForJson implements SaSerializerTemplate {
|
||||
return SaManager.getSaJsonTemplate().jsonToObject(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] objectToBytes(Object obj) {
|
||||
throw new ApiDisabledException("json 序列化器不支持 Object -> byte[]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object bytesToObject(byte[] bytes) {
|
||||
throw new ApiDisabledException("json 序列化器不支持 byte[] -> Object");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user