Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,871 people online right now. Registration is fast and FREE... Join Now!




Testing serializable

 
Reply to this topicStart new topic

Testing serializable, How do I know it works?

NickDMax
17 Sep, 2007 - 10:26 AM
Post #1

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
I have a class XMLMessage that is pretty simple. It holds a private StringWriter to build up a XML message. Now I am assuming that since StringWriter is not Serializable that it will not be serialized with my object so I will need to customize the read/writeObject functions to capture the StringWriter variable. I am not however looking forward to writing these functions (lets face it, it just doesn't seems fun and besides the XMLMessage's whole purpose was to write XML messages like the kind I would use to encode an object in the writeObject function. It seems silly to instantiate another copy of the vary object that is being serialized just to serialize it).

The XMLMessage is serializable so that it can be part of another object that is serializable... its all very confusing and I would like to be able to test things out to help me find my way.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Testing Serializable
17 Sep, 2007 - 10:42 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(NickDMax @ 17 Sep, 2007 - 11:26 AM) *

I have a class XMLMessage that is pretty simple. It holds a private StringWriter to build up a XML message. Now I am assuming that since StringWriter is not Serializable that it will not be serialized with my object so I will need to customize the read/writeObject functions to capture the StringWriter variable. I am not however looking forward to writing these functions (lets face it, it just doesn't seems fun and besides the XMLMessage's whole purpose was to write XML messages like the kind I would use to encode an object in the writeObject function. It seems silly to instantiate another copy of the vary object that is being serialized just to serialize it).

The XMLMessage is serializable so that it can be part of another object that is serializable... its all very confusing and I would like to be able to test things out to help me find my way.

So, what you're looking for is a way to serialize and de-serialize to test? I would just serialize your object to a binary file and then de-serialize it back into a class. So, something like a FileOutputStream and FileInputStream wrapped by a ObjectOutputStream and ObjectInputStream (respectively). Or were you looking for something else?

Something you might consider is having another String variable that you can use to transfer the value of the StringWriter to before serialization (Strings are serializable). That should be relatively painless. Also, you will have to mark your StringWriter as transient or you'll get an exception.

This post has been edited by Programmist: 17 Sep, 2007 - 10:46 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Testing Serializable
17 Sep, 2007 - 10:46 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
You know... I had though about just using a string to hold the contents of the StringWriter during serialization, but I got hung up on this idea of having to generate my own serialization that I more or less forgot about the workaround.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Testing Serializable
17 Sep, 2007 - 01:13 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well the answer to my initial question can be found here a simple example of serializing and de-serializing an object so you can test how well your objects serialize.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Testing Serializable
17 Sep, 2007 - 01:21 PM
Post #5

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(NickDMax @ 17 Sep, 2007 - 02:13 PM) *

Well the answer to my initial question can be found here a simple example of serializing and de-serializing an object so you can test how well your objects serialize.

I completely forgot I'd written that blog entry or I would have linked you to it. Maybe I should review my recent entries. smile.gif

This post has been edited by Programmist: 17 Sep, 2007 - 01:22 PM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Testing Serializable
17 Sep, 2007 - 09:01 PM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I think you don't know the transient Java keyword.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Testing Serializable
18 Sep, 2007 - 02:32 AM
Post #7

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(1lacca @ 17 Sep, 2007 - 10:01 PM) *

I think you don't know the transient Java keyword.

Who are you talking to?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Testing Serializable
18 Sep, 2007 - 02:59 AM
Post #8

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE(Programmist @ 18 Sep, 2007 - 12:32 PM) *

QUOTE(1lacca @ 17 Sep, 2007 - 10:01 PM) *

I think you don't know the transient Java keyword.

Who are you talking to?

To NickDax regarding his Stringwriter problem. Well, maybe it won't help him anyway.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month