3

this is my first post here, but I'm really needing help on this one because I'm just starting to learn about Serialization/Deserialization in Java:

I have a String that looks something like this:

String str = "ExampleClass[id=123,date=2009-07-12,state=OPEN]";

My question is: can I deserialize this so I can access the information inside this String? I've only seen examples where they use ObjectOutputStream and the method .writeObject() to first create a file and then later read from it.

I also have a class that looks like this:

public class ExampleClass implements Serializable { 
    protected String id;
    protected Date startDate;
    protected String state;

    //other code...

    public String toString(){
        return "ExampleClass[id="+id+",date="+startDate+",status="state"]";
    }
}

Maybe I haven't fully understood the concept of deserialization yet, but hopefully I could get an input here and find out if what I'm trying to do won't work or makes no sense. Thanks in advance!

lilisyn
  • 63
  • 1
  • 7
  • The following might help http://stackoverflow.com/questions/12963445/serialization-readobject-writeobject-overides – Oleg Sklyar Nov 01 '15 at 22:12
  • 1
    What you "seem" to want to do is parse the `String` value back to a `Object` representation, this isn't really what serialization does, it takes a object and generates a binary representation, which can be stored to a file or transmitted over the wire to another computer. For your problem, you'll need to write some code which can extract the values from `String` you need to then generate a new instance of the class – MadProgrammer Nov 01 '15 at 22:15
  • Implementing toString is not serializing either – Lluis Martinez Nov 02 '15 at 15:09

2 Answers2

0

With strings it is quite simple.

The object can be serialized because it implements the Serializable interface.

String properties will also be serialized because they are also serializable.

You can serialize any serializable object by invoking its writeObject method.

You can deserialize any object by invoking its readObject method.

You can send the serializable object through any communication channel. Usually that process involves writing it on some kind of OutputStream extension instance using its writeObject method and then reading it from some kind of InputStream extension instance using its readObject method.

REally simple, really straightforward, specially if transient properties are not involved and you don't even need to implement your own particular serialization/deserialization methods.

If you have any further doubts, the official Oracle documentation on serializable objects can help you a lot. For practical examples, check out other answers such as How to send and receive serialized object in socket channel

The topic has been discussed a lot over the years and you can find really good sources online. If you have any specific questions on your particular case of use do not hesitate to ask them.

Community
  • 1
  • 1
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
0

Unclear what you're asking. What you describe constitutes parsing a String, not deserializing it, which is trivial.

The problem with your second example seems to start with the toString() method. Why have it at all? Just serialize the actual object, not the result of its toString() method, and deserialize it directly. Strings don't enter into it at all.

user207421
  • 305,947
  • 44
  • 307
  • 483