How to serialize a dataset?

Is there a slick way to serialize a DataSet for storage in a DB blob or bytea field? I’m trying to provide users the ability to save trend pages by saving the EasyChart ChartConfiguration datasets to the database. Python’s Pickle class won’t serialize the PyDataset object, and json encoding isn’t available until Jython 2.6 (and might have the same trouble). At this point the only thing I can think of is to loop through the PyDataset and dump the values to a Python dict structure, but that seems rather cumbersome… is there any built in serialization or storage for a dataset?

Java’s native serialization works just fine with regular datasets – that’s how datasets get from gateway to clients. Wrap a ByteArrayOutputStream with an ObjectOutputStream, put the object into it, then grab the raw bytes from the BAOS.

Well, I had just figured out how to do it in pure Python by converting the dataset to a list of lists, using the python pickle module to serialize it, base64 encoding it and putting it in the database… but thanks to pturmel I was able to get the Java version working in just a couple minutes. I also referred to this blog post: joeygibson.com/2003/05/22/jytho … oo-useful/ . So here’s how I got it to work.

Database (PSQL) has some character varying fields for identifying the save, and a bytea field for the actual data.

In the save script, use the bytearrayoutputstream and objectoutputstream to save the Tag Pens dataset (will be extending to save all the runtime configurable datasets):

[code]import java.io

baos = java.io.ByteArrayOutputStream()
oos = java.io.ObjectOutputStream(baos)
oos.writeObject(event.source.parent.getComponent(‘Easy Chart’).tagPens)
oos.close()
bytesToWrite = baos.toByteArray()
system.db.runPrepUpdate(
‘’‘INSERT INTO settings (project, system, username, bin_tag_pens)
VALUES (‘projectname’, ‘systemname’, ?, ?);’’’,
[system.tag.read("[System]Client/User/Username").value,bytesToWrite],
‘trendsettings’)[/code]

To retrieve the data:

[code]queriedData = system.db.runScalarPrepQuery(
‘’‘SELECT bin_tag_pens FROM settings
WHERE
project = ‘projectname’ AND
system = ‘systemname’
AND username = ?;’’’,
[system.tag.read("[System]Client/User/Username").value],
‘trendsettings’)

if queriedData != None:
import java.io
bais = java.io.ByteArrayInputStream(queriedData)
ois = java.io.ObjectInputStream(bais)
retrievedData = ois.readObject()
event.source.parent.getComponent(‘Easy Chart’).tagPens = retrievedData
else:
system.gui.messageBox(‘No results’)

[/code]

Another good thing about this is that I can just write all 5 datasets for the EasyChart directly into the same BAOS / OOS object and as long as I retrieve them in order, I won’t need separate columns for each.

Thanks pturmel! That’s a lot easier than the pickle method…

1 Like