alwayscurious
alwayscurious

Reputation: 1165

How to de-serialize on object with two references?

Following on from this question;

Please give a short example of how one would de-serialize an object with two references.

I've included the below code so all answers refer to the same object names.

public class Person implements Serializable{
    private String name;
    private int age;
    private boolean single;
    private int numKids;
    private final static long serialVersionUID = 1L;

    public Person(String name, int age, boolean single, int numKids) {
        this.name = name;
        this.age = age;
        this.single = single;
        this.numKids = numKids;
        this.surname = surname;
    }
}

Assuming the following:

  1. A text file has already been created
  2. We created a Person object jim
  3. There is another Person bob that references the jimobject

The question already referenced describes how only jim is written to the file. The bob reference is not. How then do we read the values to jim and bob if the file might contain another few objects of the Person class? How can we ensure that bob has the correct value?

Upvotes: 2

Views: 73

Answers (2)

THM
THM

Reputation: 671

I am little confused your question - maybe because I do not see any issue with serialization and deserialization of the same object and/or multiple objects with or without references to any objects.

The point is that serialization is like creating copy of object (in file system or somewhere). This copy can be recreated in memory (deserialization). You may create object in memory (deserialization) once or more times.

It something like:

object A --serialization--> file A
file A --deserialization--> object A'
file A --deserialization--> object A"

object A, A' and A" are different objects - but all fields will be have the same values.

If object A contains an sophisticated structure (which can be serialized/deserialized) it can be also another objects then the same mechanism are working also for these objects.

All fields will have the same values but object will be different.

see sample code:

package test;

import java.io.Serializable;

public class Person  implements Serializable {
    int id;
    String name;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

and kind of test

package test;

import java.io.*;

public class Main {
    public static void main(String... args) {
        Person p1 = new Person(1, "aaa");
        Person p1a = null;
        Person p1b = null;
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.data"))) {
            oos.writeObject(p1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (ObjectInputStream oos = new ObjectInputStream(new FileInputStream("test.data"))) {
            p1a = (Person) oos.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try (ObjectInputStream oos = new ObjectInputStream(new FileInputStream("test.data"))) {
            p1b = (Person) oos.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        assert p1a != null && p1b != null;
        assert p1a.id == p1b.id;
        assert p1.id == p1b.id;
        assert p1.name.equals(p1a.name);
        assert p1a.name.equals(p1b.name);

        System.out.println(String.format("Three different object: %s, %s, %s", p1, p1a, p1b));


    }
}

PS. How java.lang.String objects are copied/stored/managed in memory it is different story.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140525

You seem to be confused a bit. Your Person class does not allow for referencing another Person object. You would need a Person field within the Person class to get there! From that perspective, your question doesn't make sense: jim and bob would be two completely independent objects, and serializing one of them will not at all serialize the other one!

But beyond that: typically, when you think in plurals of things (like multiple persons) then you would have an enclosing "container" object (like a List<Person>) that you think about. You don't deal with single Person objects then - but with such containers when thinking about serialization!

You are trying to build an artificial example that simply doesn't work out. Rather think of a situation where the Person class has maybe a field Person spouse. Then alice could be referenced by bob. And when you now have those Person objects, and some more in a list, and you serialize that list - then the system will make sure that alice and bob are serialized just once.

Upvotes: 2

Related Questions