- 1 对象----byte[] 字节数组 之间的转换
- 2 序列化方式有哪些:jdk(stream),string(string),jason(ObjectMapper)
- 必须注意地是,对象序列化保存的是对象的"状态",即它的成员变量。由此可知,对象序列化不会关注类中的静态变量
1、如果一个类想被序列化,需要实现Serializable接口。否则将抛出NotSerializableException
异常,这是因为,在序列化操作过程中会对类型进行检查,要求被序列化的类必须属于Enum、Array和Serializable类型其中的任何一种。
2、在变量声明前加上该关键字,可以阻止该变量被序列化到文件中。
3、在类中增加writeObject 和 readObject 方法可以实现自定义序列化策略
- 3先说第一种 jdk序列化方式:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SerializeUtils {
private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class);
public SerializeUtils() {
}
public static Object deserialize(byte[] bytes) {
Object result = null;
if (isEmpty(bytes)) {
return null;
} else {
try {
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
try {
result = objectInputStream.readObject();
} catch (ClassNotFoundException var5) {
throw new Exception("Failed to deserialize object type", var5);
}
} catch (Throwable var6) {
throw new Exception("Failed to deserialize", var6);
}
} catch (Exception var7) {
logger.error("Failed to deserialize", var7);
}
return result;
}
}
public static boolean isEmpty(byte[] data) {
return data == null || data.length == 0;
}
public static byte[] serialize(Object object) {
byte[] result = null;
if (object == null) {
return new byte[0];
} else {
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
try {
if (!(object instanceof Serializable)) {
throw new IllegalArgumentException(SerializeUtils.class.getSimpleName() + " requires a Serializable payload " + "but received an object of type [" + object.getClass().getName() + "]");
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
result = byteStream.toByteArray();
} catch (Throwable var4) {
throw new Exception("Failed to serialize", var4);
}
} catch (Exception var5) {
logger.error("Failed to serialize", var5);
}
return result;
}
}
}
第二种:string序列化方式: string-----byte[]
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* @Description
* @Author xxx
* @Date 2020/4/21
* @menu string 序列化方式
*/
public class StringRedisSerializer {
private final Charset charset;
public static final StringRedisSerializer US_ASCII;
public static final StringRedisSerializer ISO_8859_1;
public static final StringRedisSerializer UTF_8;
public StringRedisSerializer() {
this(StandardCharsets.UTF_8);
}
public StringRedisSerializer(Charset charset) {
Assert.notNull(charset, "Charset must not be null!");
this.charset = charset;
}
public String deserialize(@Nullable byte[] bytes) {
return bytes == null ? null : new String(bytes, this.charset);
}
public byte[] serialize(@Nullable String string) {
return string == null ? null : string.getBytes(this.charset);
}
static {
US_ASCII = new StringRedisSerializer(StandardCharsets.US_ASCII);
ISO_8859_1 = new StringRedisSerializer(StandardCharsets.ISO_8859_1);
UTF_8 = new StringRedisSerializer(StandardCharsets.UTF_8);
}
}
3 jason序列化方式
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.apache.commons.lang.SerializationException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* @Description
* @Author xxx
* @Date 2020/4/21
* @menu jason序列化
*/
public class Jackson2JsonRedisSerializer<T> {
public static final Charset DEFAULT_CHARSET;
private final JavaType javaType;
private ObjectMapper objectMapper = new ObjectMapper();
public Jackson2JsonRedisSerializer(Class<T> type) {
this.javaType = this.getJavaType(type);
}
public Jackson2JsonRedisSerializer(JavaType javaType) {
this.javaType = javaType;
}
public T deserialize(@Nullable byte[] bytes) throws SerializationException {
if (bytes==null||bytes.length==0) {
return null;
} else {
try {
return this.objectMapper.readValue(bytes, 0, bytes.length, this.javaType);
} catch (Exception var3) {
throw new SerializationException("Could not read JSON: " + var3.getMessage(), var3);
}
}
}
public byte[] serialize(@Nullable Object t) throws SerializationException {
if (t == null) {
return new byte[0];
} else {
try {
return this.objectMapper.writeValueAsBytes(t);
} catch (Exception var3) {
throw new SerializationException("Could not write JSON: " + var3.getMessage(), var3);
}
}
}
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}
protected JavaType getJavaType(Class<?> clazz) {
return TypeFactory.defaultInstance().constructType(clazz);
}
static {
DEFAULT_CHARSET = StandardCharsets.UTF_8;
}
}