File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Adventures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adventures" Watch "Adventures" New topic
Author

Adventures

durgs prasad
Greenhorn

Joined: May 28, 2012
Posts: 5
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: May 28, 2012
Posts: 5
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
 
subject: Adventures