此代码读取person.ser文件并使用readObject()方法反序列化对象。对象被强制转换为Person对象
通过序列化,一个对象或一组数据可以被转化成一种中间格式,比如二进制流。这种中间格式既可以被存储在磁盘上,也可以通过网络传输到另一台计算机。
用途:一是方便存储和传输数据,二是将数据转换为一种不同的格式。
反序列化(Deserialization)则是将序列化后的数据还原成原始的对象或数据结构的过程。
反序列化的过程涉及到将序列化后的数据按照一定的规则重新组装成原始的对象或数据结构。反序列化的过程一般也需要使用相应的编程语言中的反序列化库或函数来实现。
基于jackson将Java对象转为json,或者将json转为Java对象
将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private Integer age;
private String sex;
}
如果实现类当中的某个字段不可序列化操作,可以使用transient标识
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private transient Integer age;
private String sex;
}
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class SerializationExample {
public static void main(String[] args) {
User user = new User("phb", 20,"man");
try {
FileOutputStream fileOut = new FileOutputStream("user .ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(user );
out.close();
fileOut.close();
System.out.println("Serialized data is saved in user .ser");
} catch (Exception e) {
e.printStackTrace();
}
}
}
这段代码是将user对象序列化到名为user.ser的文件中。ObjectOutputStream类使用writeObject()方法将对象写入文件。
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class DeserializationExample {
public static void main(String[] args) {
try {
FileInputStream fileIn = new FileInputStream("user.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
User user = (User) in.readObject();
in.close();
fileIn.close();
System.out.println("Deserialized data:");
System.out.println("Name: " + user .getName());
System.out.println("Age: " + user .getAge());
System.out.println("Age: " + user .getSex());
} catch (Exception e) {
e.printStackTrace();
}
}
}
此代码读取user.ser文件并使用readObject()方法反序列化对象。对象被强制转换为User对象。
package com.islunatic.common;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//可以添加自定义序列化器和反序列化器
this.registerModule(simpleModule);
}
}