(一) 原生序列化方式-实现 Serializable 接口 ( 隐式序列化 )
通过实现 Serializable 接口,这种是隐式序列化 ( 不需要手动 ) ,这种是最简单的序列化方式,会自动序列化所有非 static 和 transient 关键字修饰的成员变量;
代码语言:javascript复制class Student implements Serializable{
private String name;
private int age;
public static int QQ = 1234;
private transient String address = "CHINA";
Student(String name, int age ){
this.name = name;
this.age = age;
}
public String toString() {
return "name: " name "n" "age: " age "n" "QQ: "
QQ "n" "address: " address;
}
public void SetAge(int age) {
this.age = age;
}
}
public class SerializableDemo {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
//创建可序列化对象 System.out.println("原来的对象:");
Student stu = new Student("Ming", 16);
System.out.println(stu); //创建序列化输出流
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buff);
//将序列化对象存入缓冲区
out.writeObject(stu);
//修改相关值
Student.QQ = 6666;
// 发现打印结果QQ的值被改变
stu.SetAge(18);
//发现值没有被改变
//从缓冲区取回被序列化的对象
ObjectInputStream in = new ObjectInputStream(new
ByteArrayInputStream(buff.toByteArray()));
Student newStu = (Student) in.readObject();
System.out.println("序列化后取出的对象:");
System.out.println(newStu);
}
}
(二) 实现Externalizable接口
Externalizable 接口继承自 Serializable,在实现该接口时,必须实现 writeExternal() 和readExternal() 方法,而且只能通过手动进行序列化,并且两个方法是自动调用的,因此这个序列化过程是可控的,可以自己选择哪些部分序列化;
代码语言:javascript复制public class Blip implements Externalizable{
private int i ;
private String s;
public Blip() {}
public Blip(String x, int a) {
System.out.println("Blip (String x, int a)");
s = x; i = a;
}
public String toString() { return s i; }
@Override
public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
System.out.println("Blip.writeExternal");
out.writeObject(s);
out.writeInt(i);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
System.out.println("Blip.readExternal");
s = (String)in.readObject();
i = in.readInt();
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
System.out.println("Constructing objects");
Blip b = new Blip("A Stirng", 47);
System.out.println(b);
ObjectOutputStream o = new
ObjectOutputStream(new FileOutputStream("F://Demo//file1.txt"));
System.out.println("保存对象");
o.writeObject(b);
o.close();
//获得对象
System.out.println("获取对象");
ObjectInputStream in = new
ObjectInputStream(new FileInputStream("F://Demo//file1.txt"));
System.out.println("Recovering b");
b = (Blip)in.readObject();
System.out.println(b);
}
}
(三) 实现Serializable接口 添加writeObject()和readObject()方法(显 隐序列化)
代码语言:javascript复制public class SerDemo implements Serializable{
public transient int age = 23;
public String name ;
public SerDemo(){
System.out.println("默认构造器。。。");
}
public SerDemo(String name) {
this.name = name;
}
private void writeObject(ObjectOutputStream stream) throws
IOException {
stream.defaultWriteObject();
stream.writeInt(age);
}
private void readObject(ObjectInputStream stream) throws
ClassNotFoundException, IOException {
stream.defaultReadObject();
age = stream.readInt();
}
public String toString() { return "年龄" age " " name; }
public static void main(String[] args) throws IOException,
ClassNotFoundException {
SerDemo stu = new SerDemo("Ming");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(stu);
ObjectInputStream in = new ObjectInputStream(new
ByteArrayInputStream(bout.toByteArray()));
SerDemo stu1 = (SerDemo) in.readObject();
System.out.println(stu1);
}
}
(四)json序列化
代码语言:javascript复制public class UserBase {
private String userName;
private Integer age;
private Date addTime;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
}
public class TestMain {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
String json1 = "{"userName":"小李飞刀","age":18,"addTime":1591851786568}";
String json2 = "[{"userName":"小李飞刀","age":18,"addTime":123}, {"userName":"小李飞刀2","age":182,"addTime":1234}]";
try {
//1、json字符串转为对象
UserBase userBase1 = objectMapper.readValue(json1,
UserBase.class);
UserBase userBase2 = objectMapper.readValue(json1,
new TypeReference<UserBase>(){});
System.out.println(userBase1.getUserName());
System.out.println(userBase2.getUserName());
//2、json转为map
Map<String,Object> map = objectMapper.readValue(json1,
new TypeReference<Map<String,Object>>(){});
//map:{userName=小李飞刀, age=18, addTime=1591851786568}
System.out.println("map:" map);
//3、json转为list<bean>
//使用json1报错,此时需要数组/集合类型: Can not deserialize instance of TEst.UserBase[] out of START_OBJECT token
List<UserBase> lists = objectMapper.readValue(json2,
new TypeReference<List<UserBase>>(){});
System.out.println(lists.get(0).getUserName());
//4、json转为数组
UserBase[] userBases = objectMapper.readValue(json1,
new TypeReference<UserBase[]>(){});
System.out.println(userBases[0].getUserName());
//序列化
Map<String,String> map1 = Maps.newHashMap();
map1.put("name", "小李飞刀");
map1.put("sex", "男");
String json = objectMapper.writeValueAsString(map1);
System.out.println(json);
//反序列化
Map<String,String> maps = objectMapper.readValue(json,
new TypeReference<Map<String,String>>(){});
System.out.println(maps.get("name"));
//反序列化
List<UserBase> listes = objectMapper.readValue(json,
new TypeReference<List<UserBase>>(){});
System.out.println(listes.get(0).getUserName());
//反序列化
UserBase[] userBasess = objectMapper.readValue(json,
new TypeReference<UserBase[]>(){});
System.out.println(userBasess[0].getUserName());
objectMapper.configure(DeserializationFeature.
FAIL_ON_UNKNOWN_PROPERTIES, false);
//法一
List<UserBase> userBases1 = objectMapper.readValue(json2,
new TypeReference<List<UserBase>>(){});
//法二
JavaType javaType= objectMapper.getTypeFactory().
constructCollectionType(List.class, UserBase.class);
List<UserBase> list2 = objectMapper.readValue(json1, javaType);
} catch (Exception e) {
e.printStackTrace();
}
}
}