reader/writer design question for custom datatype

S

Sideswipe

I have a situation where I have several different input formats that
ultimately need to construct a List<Map<String,Object>> and at some
point I will need to write out the same datatype as well. Classic
serialization.

The input source could be a proprietary formatted config file, XML, a
Database, and so on.

I have in my mind that somehow implementing the Serializable interface
on my own subclasses of List an Map (implementing the writeObject()
and readObject()) respectively is what I am looking for.

Problem is though, it wants me to write to a stream -- in the case of
files, this isn't a problem but in the case of a Database it seems a
stretch to refer to a DB connection as some kinda stream (although not
an impossible stretch). And, I can't control what stream is passed to
those methods

Perhaps the better way would be to implement my own In/Out stream for
the different sources, and if it's and object of form List<map>, then
write out approrpriately. Then again, the writeInt/etc doesn't make
sense in the case of a DB because it is keyed. Only writeObject/
ReadObject has real meaning and only if it's the form I need

So, in summary: I need to load data from multiple sources and
reconstitute an object, then at some point turn it around and write it
back out.

I am looking for some ideas on this -- I would like to use the built
in Java facilities to do it


Christian Bongiorno
http://christian.bongiorno.org
 
D

Daniel Pitts

I have a situation where I have several different input formats that
ultimately need to construct a List<Map<String,Object>> and at some
point I will need to write out the same datatype as well. Classic
serialization.

The input source could be a proprietary formatted config file, XML, a
Database, and so on.

I have in my mind that somehow implementing the Serializable interface
on my own subclasses of List an Map (implementing the writeObject()
and readObject()) respectively is what I am looking for.

Problem is though, it wants me to write to a stream -- in the case of
files, this isn't a problem but in the case of a Database it seems a
stretch to refer to a DB connection as some kinda stream (although not
an impossible stretch). And, I can't control what stream is passed to
those methods

Perhaps the better way would be to implement my own In/Out stream for
the different sources, and if it's and object of form List<map>, then
write out approrpriately. Then again, the writeInt/etc doesn't make
sense in the case of a DB because it is keyed. Only writeObject/
ReadObject has real meaning and only if it's the form I need

So, in summary: I need to load data from multiple sources and
reconstitute an object, then at some point turn it around and write it
back out.

I am looking for some ideas on this -- I would like to use the built
in Java facilities to do it

Christian Bongiornohttp://christian.bongiorno.org

Serialization and databases don't mix well. Look into a ORM solution
if you want to persist your java objects into a database.

I suggest Hibernate or Ibatis.

There are also utilities that will help you save object graph to an
XML file, but I don't have as much experience with those.
 
L

Lew

Daniel said:
Serialization and databases don't mix well. Look into a ORM solution
if you want to persist your java objects into a database.

I suggest Hibernate or Ibatis.

There are also utilities that will help you save object graph to an
XML file, but I don't have as much experience with those.

The Java Persistence API would likely be useful, too.
 
S

Sideswipe

Yes, well I would love to go to Hibernate but unfortunately I am
dealing with legacy code and no one has any experience with hibernate
but me. It's frustrating. Also, the data doesn't necessarily come from
a database. It may come from a config file or XML
 
D

Daniel Pitts

Sideswipe said:
Yes, well I would love to go to Hibernate but unfortunately I am
dealing with legacy code and no one has any experience with hibernate
but me. It's frustrating. Also, the data doesn't necessarily come from
a database. It may come from a config file or XML
Well, Hibernate doesn't prevent you for loading for elsewhere.

In any case, I find it useful to separate out the reading/writing from
the type itself. There are JavaBean serializers that will write/read
XML (I think they're standard). Or you can write your own DAO (Data
Access Object). Many people associate DAO's with databases, but it is
just as appropriate for file-stored data. You would have a different
implementation of the DAO for DB vs Config vs XML.

Hope this helps,
Daniel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,007
Messages
2,570,266
Members
46,864
Latest member
DaniEbswor

Latest Threads

Top