Perspective properties export/import to/from database

EDIT: Save yourself from some unnecessary reading and see my next response in this thread. The only thing worth noting in this post is the first block of code for serialization.

I had similar questions. As of a forum post from August, it seems they’re still working on being able to cast Perspective properties as python objects. The short of it is that you can import JSON objects as the properties, and there’s not an easy way (as far as I know) to retrieve the properties in a JSON format.

My workaround until that’s available is to re-create the property you’re trying to save as a python dictionary, do a json.dumps on that python object, then put it into the database (I’ve also been serializing my json dump strings for easier storage as byte arrays rather than trying to store potentially huge VARCHARs). Then, when you want to load it, do a json.loads on the data from your query. You can then directly set the property you loaded to the now-decoded JSON object.

Here’s code for serialization/deserialization:

# I named this file serialization, you can call it whatever
import java.io
import collections

# serializes a given object into a byte array, 
# usually to store in the database to retrieve later.
def serialize(object):
	baos = java.io.ByteArrayOutputStream()
	oos = java.io.ObjectOutputStream(baos)
	oos.writeObject(object)
	oos.close()
	return baos.toByteArray()

# deserializes a byte array object.
def deserialize(object):
	bais = java.io.ByteArrayInputStream(object)
	ois = java.io.ObjectInputStream(bais)
	return ois.readObject()

And here’s a simplified example:

import json

# create a dict matching the prop you want 
# to save, say, a style property.
# It's usually pretty trivial to recreate the structure of
# the property from within one of the parent object's events--
# I usually do loading and saving within onStartup and 
# onShutdown event scripts for example
my_prop = {"classes": "Menu Bold_Text", "backgroundColor" : "#DADDE0"}

# make a JSON string
prop_json_data = json.dumps(my_prop)

# (optional) serialize data for easier storage
serialized_data = serialization.serialize(prop_json_data)

# (From here, you would run an update query to store either 
# the JSON string or serialized string into your database)

# Then, to retrieve the object, run your retrieval 
# query and get the object(s) from your dataset 
# (best done in try/catch/finally in a transaction, 
# I'm just being lazy in this example here)
query_results = system.db.runNamedQuery(...) 

# (deserialize if you serialized)
json_string = serialization.deserialize(query_results.getValueAt(0,0) )

# load the string so we get the JSON object back
retrieved_prop = json.loads(json_string)

# finally, simply set the property to the retrieved property
self.props.style = retrieved_prop

Kind of a rough outline, but I hope this helps. There might even be a better way to do it, but this is what I’ve been doing since early release without any issues.

2 Likes