Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #28537 > unrolled thread

store / load a non-serializable interface

Started byPhilipp Kraus <philipp.kraus@flashpixx.de>
First post2014-05-19 22:01 +0200
Last post2014-05-20 07:01 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  store / load a non-serializable interface Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-05-19 22:01 +0200
    Re: store / load a non-serializable interface markspace <markspace@nospam.nospam> - 2014-05-19 16:36 -0700
    Re: store / load a non-serializable interface Arne Vajhøj <arne@vajhoej.dk> - 2014-05-19 19:54 -0400
      Re: store / load a non-serializable interface Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-05-20 07:01 +0200

#28537 — store / load a non-serializable interface

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2014-05-19 22:01 +0200
Subjectstore / load a non-serializable interface
Message-ID<lldnvh$e29$1@online.de>
Hello,

I have got a non-serializable class and I would like to store / load 
these objects. So my idea
is creating a encapsulate the class:

class myClass implements Serializable
{
      add some properties

     public myClass( myNonSerializableInterface data )
     {
             add members from data to properties
    }

    public myNonSerializableInterface create()
    {
          return ??
    }
}

My problem is, that I don't know in which way I can restore the 
interface object?
So I need to store the object name. Can anybody help me to create a working
serializable structure? I'm a little bit confused at the moment to 
solve the problem

Thanks a lot

Phil

[toc] | [next] | [standalone]


#28538

Frommarkspace <markspace@nospam.nospam>
Date2014-05-19 16:36 -0700
Message-ID<lle4he$ed5$1@dont-email.me>
In reply to#28537
On 5/19/2014 1:01 PM, Philipp Kraus wrote:
> Hello,
>
> I have got a non-serializable class and I would like to store / load
> these objects.

Extend it.

public interface MyNewIFace extends myNonSerializableInterface, 
Serializable {}


> So my idea
> is creating a encapsulate the class:
>

I don't think this will work.


> My problem is, that I don't know in which way I can restore the
> interface object?

You can if you extend it.  This works too:

public class MyClass implements myNonSerializeableInterface, 
Serializable { ... }

But then you have to work with classes, not interfaces.

> So I need to store the object name.

No.

> Can anybody help me to create a working
> serializable structure? I'm a little bit confused at the moment to solve
> the problem

Google is your friend.

[toc] | [prev] | [next] | [standalone]


#28539

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-05-19 19:54 -0400
Message-ID<537a99a8$0$304$14726298@news.sunsite.dk>
In reply to#28537
On 5/19/2014 4:01 PM, Philipp Kraus wrote:
> I have got a non-serializable class and I would like to store / load
> these objects. So my idea
> is creating a encapsulate the class:
>
> class myClass implements Serializable
> {
>       add some properties
>
>      public myClass( myNonSerializableInterface data )
>      {
>              add members from data to properties
>     }
>
>     public myNonSerializableInterface create()
>     {
>           return ??
>     }
> }
>
> My problem is, that I don't know in which way I can restore the
> interface object?
> So I need to store the object name. Can anybody help me to create a working
> serializable structure? I'm a little bit confused at the moment to solve
> the problem

Ted Neward once came up with a pattern Serializable Adapter to solve
that problem.

Here is an example of how it can be done (I am the author of the
example so give Ted Neward credit for the idea and blame me for
bugs in implementation).

We have a data class that are not serializable:

public class Data {
     private int iv;

     private String sv;

     public int getIv() {
         return iv;
     }

     public void setIv(int iv) {
         this.iv = iv;
     }

     public String getSv() {
         return sv;
     }

     public void setSv(String sv) {
         this.sv = sv;
     }

     public String toString() {
         return ("[" + iv + ":" + sv + "]");
     }
}

We have a little piece of convenience code:

import java.io.Serializable;

public class SerializableAdapter implements Serializable {
     protected transient Object object;

     public Object getObject() {
         return object;
     }
}

And now we create the specific adapter class:

public class DataSerializable extends SerializableAdapter {
     public DataSerializable(Data o) {
         object = o;
     }
     private void writeObject(ObjectOutputStream oos) throws IOException {
         oos.writeInt(((Data)object).getIv());
         oos.writeUTF(((Data)object).getSv());
     }
     private void readObject(ObjectInputStream ois) throws IOException, 
ClassNotFoundException {
         object = new Data();
         ((Data)object).setIv(ois.readInt());
         ((Data)object).setSv(ois.readUTF());
     }
}

Which is perfectly serializable and deserializable.

Arne



[toc] | [prev] | [next] | [standalone]


#28540

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2014-05-20 07:01 +0200
Message-ID<llenk4$m9l$1@ariadne.rz.tu-clausthal.de>
In reply to#28539
Thanks, this is a great solution and very short

Phil

On 2014-05-19 23:54:16 +0000, Arne Vajhøj said:

> On 5/19/2014 4:01 PM, Philipp Kraus wrote:
>> I have got a non-serializable class and I would like to store / load
>> these objects. So my idea
>> is creating a encapsulate the class:
>> 
>> class myClass implements Serializable
>> {
>> add some properties
>> 
>> public myClass( myNonSerializableInterface data )
>> {
>> add members from data to properties
>> }
>> 
>> public myNonSerializableInterface create()
>> {
>> return ??
>> }
>> }
>> 
>> My problem is, that I don't know in which way I can restore the
>> interface object?
>> So I need to store the object name. Can anybody help me to create a working
>> serializable structure? I'm a little bit confused at the moment to solve
>> the problem
> 
> Ted Neward once came up with a pattern Serializable Adapter to solve
> that problem.
> 
> Here is an example of how it can be done (I am the author of the
> example so give Ted Neward credit for the idea and blame me for
> bugs in implementation).
> 
> We have a data class that are not serializable:
> 
> public class Data {
>      private int iv;
> 
>      private String sv;
> 
>      public int getIv() {
>          return iv;
>      }
> 
>      public void setIv(int iv) {
>          this.iv = iv;
>      }
> 
>      public String getSv() {
>          return sv;
>      }
> 
>      public void setSv(String sv) {
>          this.sv = sv;
>      }
> 
>      public String toString() {
>          return ("[" + iv + ":" + sv + "]");
>      }
> }
> 
> We have a little piece of convenience code:
> 
> import java.io.Serializable;
> 
> public class SerializableAdapter implements Serializable {
>      protected transient Object object;
> 
>      public Object getObject() {
>          return object;
>      }
> }
> 
> And now we create the specific adapter class:
> 
> public class DataSerializable extends SerializableAdapter {
>      public DataSerializable(Data o) {
>          object = o;
>      }
>      private void writeObject(ObjectOutputStream oos) throws IOException {
>          oos.writeInt(((Data)object).getIv());
>          oos.writeUTF(((Data)object).getSv());
>      }
>      private void readObject(ObjectInputStream ois) throws IOException, 
> ClassNotFoundException {
>          object = new Data();
>          ((Data)object).setIv(ois.readInt());
>          ((Data)object).setSv(ois.readUTF());
>      }
> }
> 
> Which is perfectly serializable and deserializable.
> 
> Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web