mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-05-04 12:47:55 +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);
|
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
|
@Override
|
||||||
default String objectToString(Object obj) {
|
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) {
|
if (obj == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -38,28 +56,26 @@ public interface SaSerializerTemplateForJdk extends SaSerializerTemplate {
|
|||||||
ObjectOutputStream oos = new ObjectOutputStream(baos)
|
ObjectOutputStream oos = new ObjectOutputStream(baos)
|
||||||
) {
|
) {
|
||||||
oos.writeObject(obj);
|
oos.writeObject(obj);
|
||||||
byte[] bytes = baos.toByteArray();
|
return baos.toByteArray();
|
||||||
return bytesToString(bytes);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new SaTokenException(e);
|
throw new SaTokenException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default Object stringToObject(String str) {
|
default Object bytesToObject(byte[] bytes) {
|
||||||
if(str == null) {
|
if(bytes == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
byte[] data = stringToBytes(str);
|
|
||||||
try (
|
try (
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
||||||
ObjectInputStream ois = new ObjectInputStream(bais)
|
ObjectInputStream ois = new ObjectInputStream(bais)
|
||||||
) {
|
) {
|
||||||
return ois.readObject();
|
return ois.readObject();
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
throw new SaTokenException(e);
|
throw new SaTokenException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* byte[] 转换为 String
|
* byte[] 转换为 String
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package cn.dev33.satoken.serializer.impl;
|
package cn.dev33.satoken.serializer.impl;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.dev33.satoken.exception.ApiDisabledException;
|
||||||
import cn.dev33.satoken.serializer.SaSerializerTemplate;
|
import cn.dev33.satoken.serializer.SaSerializerTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,4 +37,14 @@ public class SaSerializerTemplateForJson implements SaSerializerTemplate {
|
|||||||
return SaManager.getSaJsonTemplate().jsonToObject(str);
|
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