• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adventures

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic