0

I have a class Foo in which reside these 2 methods:

save() and load()

void save() { ... }

static final Foo load() { ... }

The purpose of these methods are to save and load Foo objects via Java Serialization

However, I've realized now that they are fundamentally flawed for what I need to do - namely because in order to load data, I must create a brand-spanking new object of class Foo.

What I need to do instead is to call load from within Foo sometimes... to be able to deserialize straight into the existing Foo object (replacing the existing data present)

How can I do this?

All I can think of is to move all the fields that exist in Foo into an inner class

class Foo {
    String field1;
    int field2;
}

new way:

class Foo {
    class Bar {
        String field1;
        int field2;
    }
    Bar data;

    void load() { data = deserlializeBar(); }
}

are there any simpler ways?

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • What do you mean, you have to create a new object of `Foo`? `load()` is static, so call it: `Foo.load()`. – Andreas Oct 04 '15 at 23:03
  • yes exactly I do not want a new object, I just want the data to replace the existing object's data - currently, it works the way you wrote - that is what I'm trying to "re-do" – ycomp Oct 04 '15 at 23:04
  • So not following you. You have an object, you save to somewhere, and throw it away. Now you don't have an object. Later, you want to load the object from the saved location, so you call `Foo.load()` and it restores the object and returns it to you. – Andreas Oct 04 '15 at 23:14
  • maybe using an inner class is the way to do it, I was just asking because I'm not really well-versed in serialization – ycomp Oct 04 '15 at 23:14
  • hmm, yeah I can see it can be confusing as to why I would need to do this - well I have multiple objects of the same class writing to the same file and I want to make sure the data is refreshed before the other objects access it.. however it is not multithreaded, so no threading issues arise. So they are all actually loading data somewhat regularly - just FYI, I don't think this actually matters to the question. The question is pretty straightforward - how to load data into an existing object through serialization, but I guess inner classes are the way to do it - since nobody posted answers. – ycomp Oct 04 '15 at 23:16
  • Maybe you should clarify the question. – Andreas Oct 04 '15 at 23:18

3 Answers3

0

The question is pretty straightforward - how to load data into an existing object through serialization

How are you accessing the existing objects? If you have a map from where you are accessing the existing objects, then here is a way to reload data : Have all your objects an unique id. When you want to refresh data from the De-serialized data, re-populate all fields of the existing object from the de-serialized object. Here is an example :

public class RelaodFromDeSerialidedData
{

    HashMap<Integer, Foo>   inMemoryObjects = new HashMap<Integer, Foo>();

    public void loadData()
    {
        for (Foo deSerializedFoo : getDeSerializedFoo())
        {
            if (inMemoryObjects.containsKey(deSerializedFoo.id))
            {
                // now reload the fileld(s) you want from deSerialized object
                Foo inMemoryFoo = inMemoryObjects.get(deSerializedFoo.id);
                inMemoryFoo.name = deSerializedFoo.name;
                // ..... 
                inMemoryObjects.remove(deSerializedFoo.id);
                inMemoryObjects.put(deSerializedFoo.id, inMemoryFoo);
            }
        }
    }

    public static ArrayList<Foo> getDeSerializedFoo()
    {

        ArrayList<Foo> f = new ArrayList<Foo>();// logic to deserialize and return foo object List
        return f;
    }

}

class Foo
{
    int     id;
    String  name;
    String  otherData;

}
App Work
  • 21,899
  • 5
  • 25
  • 38
0

are there any simpler ways?

I think there are:

// Serializing:
Foo foo1=new Foo();
foo1.set(...)
foo1.save();

// Deserializing:
Foo foo2=Foo.load();

However, serializing/deserializing methods usually accept a stream object as input parameter. Does have anyone your save and load ? Then, where the data is physically recorded/loaded?

Little Santi
  • 8,563
  • 2
  • 18
  • 46
0

It is a duplicate of Deserialize JSON into existing object (Java)

Use the following code:

ObjectMapper mapper = new ObjectMapper();
mapper.readerForUpdating(object).readValue(json);
Community
  • 1
  • 1
gauti
  • 527
  • 1
  • 6
  • 16