Perspective properties export/import to/from database

Great news!

Although, as far as I could tell, this is completely undocumented, it nonetheless works for pulling out properties as JSON objects without the need for re-creating them from scratch:

from com.inductiveautomation.ignition.common import TypeUtilities
jsonObj = TypeUtilities.pyToGson(self.props.labels) # (or w/e your prop is)

EDIT: pyToJson --> pyToGson as of Ignition release version 8.0.5. Thanks, @PGriffith!

From here, you can serialize the jsonObject for storage and deserialize as needed. This obviates the need for importing Python’s json module.

For completeness’s sake, here’s an updated example (without copying the previous serialization code, since that’s the same):

from com.inductiveautomation.ignition.common import TypeUtilities

# Grab the property you want to save as a JSON object
jsonObj = TypeUtilities.pyToGson(self.props.style)

# (optional) serialize data for easier storage
serializedJson = serialization.serialize(jsonObj)

# (From here, you would run an update query to store either 
# the raw 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/except/finally block)
queryResults = system.db.runNamedQuery(...) 

# deserialize to get the json object back
retrievedJsonObj = serialization.deserialize(queryResults.getValueAt(0,0) )

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

Credit to @grietveld for his solution on the post here. Huge timesaver! I only wish it was documented somewhere. I scoured docs for TypeUtilities and found nothing, which makes me wonder where this module was found…lol.

4 Likes