求高手支招如何对一个为实现序列化的对象进行序列化?

jkvast 2014-02-25 10:11:42
有个类 Person 未实现Serializable接口,且又不能修改Person类

现在已经拿到Person的实例对象,

求教有什么方法让这个对象,能够序列化?

求大神指导!
...全文
653 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jkvast 2014-03-12
  • 打赏
  • 举报
回复
需要序列化的对象太复杂了,里面不止有字符窜属性 还有各种没实现序列化的对象属性 所以 没得能解决 结贴
chlb-icoc-cc 2014-02-26
  • 打赏
  • 举报
回复
第二种解法,OK了!!!

public class Person {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
import java.io.IOException;
import java.io.Serializable;

public class Person_Serible extends Person implements Serializable {
	private static final long serialVersionUID = 1L;

	private void writeObject(java.io.ObjectOutputStream out) throws IOException {
		out.defaultWriteObject();// 先序列化对象
		out.writeUTF(super.getName());// 再序列化父类的域
		out.writeInt(super.getAge());// 再序列化父类的域
	}

	private void readObject(java.io.ObjectInputStream in) throws IOException,
			ClassNotFoundException {
		in.defaultReadObject();// 先反序列化对象
		super.setName(in.readUTF());// 再反序列化父类的域
		super.setAge(in.readInt());// 再反序列化父类的域
	}
}
import java.io.*;
/**
 * 测试类
 */
public class Test {

	//对person实例序列化
	public static void serilizable(Person person, String writePath) throws IOException{
		FileOutputStream fileOutputStream = new FileOutputStream(new File(writePath));
		ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
		outputStream.writeObject(person);
		outputStream.close();
	}
	
	//反序列化
	public static Person unSerilizable(String filePath) throws ClassNotFoundException, IOException{
		FileInputStream fileInputStream = new FileInputStream(new File(filePath));
		ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
		Person person = (Person_Serible) inputStream.readObject();
		inputStream.close();
		return person;
	}
	
	public static void main(String[] args) {
		Person person = new Person_Serible();
		person.setName("zhangsan");
		person.setAge(15);
		
		try {
			Test.serilizable(person, "D:\\person.txt");//序列化为磁盘文件
			Person person2 = Test.unSerilizable("D:\\person.txt");//从磁盘文件读出来序列化文件
			System.out.println(person2.getName() + "\t" + person2.getAge());
		}  catch (ClassNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
}
参见:http://www.yesky.com/376/1908876.shtml
晓风吹雾 2014-02-26
  • 打赏
  • 举报
回复
JsonMapper 序列化为json格式

JsonMapper.nonEmptyMapper().toJson(object)
JsonMapper.nonEmptyMapper().fromJson(jsonString, clazz)

<dependency>
	<groupId>com.fasterxml.jackson.jaxrs</groupId>
	<artifactId>jackson-jaxrs-json-provider</artifactId>
	<version>${jackson.version}</version>
</dependency>
jkvast 2014-02-26
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
序列化是将对象写入到输出流中,用流存储或者传输。没实现哪个接口,你看看能不能把这个对象写入到流中。ObjectOutputStream和ObjectInputStream
不能的,没实现序列化接口是不能直接写入到流的
jkvast 2014-02-26
  • 打赏
  • 举报
回复
引用 4 楼 cheng19900917 的回复:
忘记继承了

public class Person_Serible extends Person implements Serializable{}
你说这个 我知道,但是怎么把 Person 和 Person_Serible 怎么转换是我头疼的地方
jkvast 2014-02-26
  • 打赏
  • 举报
回复
引用 6 楼 lxbccsu 的回复:
若类 Person 未实现Serializable接口,可以肯定的说无法序列化Person的实例对象; 不过你可以自己定义一个类实现Serializable接口, 并把新类的字段定义成和Person的实例字段完全一样,这样先序列化新类的实例,反序列化时再赋值给Person实例; 示例:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class SerializableDemo {

	public static void main(String[] args) throws Exception {
		Person pers = new Person();
		pers.setName("Lee");
		pers.setAge(20);
		pers.setBirthday(new Date());
		
		PersonSerializable perSeri = new PersonSerializable();
		perSeri.copy(pers);
		
		ByteArrayOutputStream bou = new ByteArrayOutputStream();
		ObjectOutputStream oou = new ObjectOutputStream(bou);
		oou.writeObject(perSeri);
		
		ByteArrayInputStream bin = new ByteArrayInputStream(bou.toByteArray());
		ObjectInputStream oin = new ObjectInputStream(bin);
		PersonSerializable perSeriClone = (PersonSerializable)oin.readObject();
		
		System.out.println(perSeriClone.getName());
		System.out.println(perSeriClone.getAge());
		System.out.println(perSeriClone.getBirthday());
		
		
		System.out.println("======================");
		Person persClone = perSeriClone.copyToPerson();
		System.out.println(persClone.getName());
		System.out.println(persClone.getAge());
		System.out.println(persClone.getBirthday());
	}

}

class PersonSerializable implements Serializable {
	
	private static final long serialVersionUID = -8564152324839732089L;
	
	private String name;
	private int age;
	private Date birthday;
	
	public void copy(Person person){
		if (person == null) return;
		this.setName(person.getName());
		this.setBirthday(person.getBirthday());
		this.setAge(person.getAge());
	}
	
	public Person copyToPerson(){
		Person person = new Person();
		person.setName(this.getName());
		person.setBirthday(this.getBirthday());
		person.setAge(this.getAge());
		
		return person;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}

class Person {
	private String name;
	private int age;
	private Date birthday;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}
这个看起来挺靠谱,我试下
chlb-icoc-cc 2014-02-25
  • 打赏
  • 举报
回复
忘记继承了

public class Person_Serible extends Person implements Serializable{}
chlb-icoc-cc 2014-02-25
  • 打赏
  • 举报
回复
你可以重新定义个新类,继承Person并且实现Serializable接口就可以了,之后使用新类就可以了,功能一点都不影响 例如:

public class Person_Serible implements Serializable{}
然后使用Person_Serible就可以了
etnet 2014-02-25
  • 打赏
  • 举报
回复
没有实现Serializable接口的话,你只能自己去完成这个动作了. 你可以试试利用反射去读取实例中的字段,然后不管你是保存成XML还是JSON..还是二进制自定义格式都可以了.
  • 打赏
  • 举报
回复
序列化是将对象写入到输出流中,用流存储或者传输。没实现哪个接口,你看看能不能把这个对象写入到流中。ObjectOutputStream和ObjectInputStream
lxbccsu 2014-02-25
  • 打赏
  • 举报
回复
若类 Person 未实现Serializable接口,可以肯定的说无法序列化Person的实例对象; 不过你可以自己定义一个类实现Serializable接口, 并把新类的字段定义成和Person的实例字段完全一样,这样先序列化新类的实例,反序列化时再赋值给Person实例; 示例:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class SerializableDemo {

	public static void main(String[] args) throws Exception {
		Person pers = new Person();
		pers.setName("Lee");
		pers.setAge(20);
		pers.setBirthday(new Date());
		
		PersonSerializable perSeri = new PersonSerializable();
		perSeri.copy(pers);
		
		ByteArrayOutputStream bou = new ByteArrayOutputStream();
		ObjectOutputStream oou = new ObjectOutputStream(bou);
		oou.writeObject(perSeri);
		
		ByteArrayInputStream bin = new ByteArrayInputStream(bou.toByteArray());
		ObjectInputStream oin = new ObjectInputStream(bin);
		PersonSerializable perSeriClone = (PersonSerializable)oin.readObject();
		
		System.out.println(perSeriClone.getName());
		System.out.println(perSeriClone.getAge());
		System.out.println(perSeriClone.getBirthday());
		
		
		System.out.println("======================");
		Person persClone = perSeriClone.copyToPerson();
		System.out.println(persClone.getName());
		System.out.println(persClone.getAge());
		System.out.println(persClone.getBirthday());
	}

}

class PersonSerializable implements Serializable {
	
	private static final long serialVersionUID = -8564152324839732089L;
	
	private String name;
	private int age;
	private Date birthday;
	
	public void copy(Person person){
		if (person == null) return;
		this.setName(person.getName());
		this.setBirthday(person.getBirthday());
		this.setAge(person.getAge());
	}
	
	public Person copyToPerson(){
		Person person = new Person();
		person.setName(this.getName());
		person.setBirthday(this.getBirthday());
		person.setAge(this.getAge());
		
		return person;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}

class Person {
	private String name;
	private int age;
	private Date birthday;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧