views:

87

answers:

1

Given that Java ME allows for no reflection and is quite stripped down, what would a good approach to storing data in a RecordStore be? I thought of devising a JSON-like syntax adapter class which would probably require every class whose values are to be stored to implement a Hashtable + might probably require an additional regex library as even that's been taken away.

If you're wondering why on earth I need it, it's for an assignment. So the dilemma is really between writing a rather unnecessarily large chunk of proper code as a matter of principle or hardcoding everything in knowing nobody has to suffer through maintenance of this junk down the line. But, the good principles person in me is leaning towards the former.

EDIT: I should have elaborated — I'd like to store object's data within the RecordStore, so I'm trying to devise a method to represent an object as a string which can then be converted into a byte array.

+2  A: 

For every object you want to save in the RecordStore, pare it down to its component Strings and primitives, then serialise it to a byte array, using a ByteArrayOutputStream wrapped in a DataOutputStream. Then write this byte array to RMS.

Reverse the process using a ByteArrayInputStream wrapped in a DataInputStream to get the object back.

funkybro
That's basically what I have to do, indeed. The problem is, and the reason I wanted a piece of your mind on this, is that I was trying to come up with a more automated way of serializing an object. Aside from hardcoding the names of each variable and hence making a fairly rigid design, the only other idea I came up with is storing all object's data in a Hashtable which can then be serialized and deserialized when the time comes to load the object back up.
Check out J2ME Polish. I believe they have their own version of the Serializable interface. Implementing this will automatically create the serialization code for you, using pre-processing.
funkybro