Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adventures

 
durgs prasad
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a concatenated String separated by a delimiter. Am planning to develop a generic method. Say for example, I have 2 different classes [class A and class B] as follows:

Say now I’ve 2 lists of objects, list<A> and list<B>, By using ‘#’ as delimiter and ‘,’ as a sub delimiter I have strings like:
String a = 11,abc,05/29/2012,11.12#12,xyz,05/29/2012,12.11 --> serialized from list<A>
String b = 12,x#11,y#1112,z --> serialized from list<B>
And I want to have a method where It takes this serialized string as an argument and returns me the same List of appropriate class.
The method am planning to develop has the following signature:
Public static List<?> deserailizeList(Class className, String serializedStr, String delim, String subDelim) {
//here am making use of reflection API , any better options we may go?
}
Now if I pass className as ‘A’ and serializedStr as the serialized String as mentioned above ‘a’ , now I want the method to return the list<A> by deserializing the string. And the same for Class B.
While developing I got stuck in how to set the values to appropriate datamembers. Need some assistance.
 
Winston Gutkowski
Bartender
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
durgs prasad wrote:And I want to have a method where It takes this serialized string as an argument and returns me the same List of appropriate class.

First, please UseCodeTags (←click).

The method am planning to develop has the following signature:
Public static List<?> deserailizeList(Class className, String serializedStr, String delim, String subDelim) {
//here am making use of reflection API , any better options we may go?...

Yes, lots.
1. Have your classes implement the java.io.Serializable interface. Then you (probably) won't need any methods at all.

2. If this is simply an exercise:
(a) Create a 'MySerializable' interface, and put your deserializeList() method signature in it (except without the Class). My suggestion would be something like:(b) Implement deserializeList() for each of your classes specifically, eg:Also: if you output your data as one object per line, you don't have to worry about an object delimiter. It'll also make the data easier to read if you need to.

What you are doing currently is called 'dispatch' code, viz:and if you find yourself doing it you should stop and think of a class-based alternative (usually polymorphism).

HIH

Winston

[EDIT:] Thinking about it a bit further, I wouldn't return Lists. Just have each object know how to serialize and deserialize itself.
Then you can create static methods to take a file and return a List, or vice-versa.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47261
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags, since you are new; it would have looked so much better with fuller indentation.
 
durgs prasad
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions. Java Serialization seems to be the solution. Now, I have a scenario -


It is decided @ the run-time how many datamembers to be serialized. In that case, what should I do?
 
Winston Gutkowski
Bartender
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
durgs prasad wrote:It is decided @ the run-time how many datamembers to be serialized. In that case, what should I do?

I don't understand the question.
What do you mean by "It is decided @ the run-time how many datamembers to be serialized"?
More specifically: On what basis does that decision take place?

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic