Perspective: setting session custom objects with scripting

I'm having trouble with setting session custom objects and their values from within a dataset using scripting. I've tested that copying the object, pasting into Notepad, making changes, and right-click "Pasting" back into the object does work as intended. I've also tried pasting that into the script itself and then moving that into the session custom object and it also works (see below).

Script:
snipOfScriptToWriteToTestObj

Session object:
snipOfTestObj

The overall idea is to be able to save the session custom object within a dataset as a string, then have other users be able to load that on their session like a bookmark i.e., create their own bookmarks, load others, and select them from a dropdown like a bookmark. I've tested this by Copying the session object, Pasting into a cell within a dataset memory tag (string column), then scripting a button to move that dataset cell value into the session custom object.

The script overwrites the object and turns it into a session value instead (with the string as its value).

Is there a conversion that needs to happen to that string before it gets assigned to the session custom object when the string is stored within a dataset as a string? Or must this be done in a different way?

I know this can also be done by assigning values to each of the elements of the session custom object within the script and having a column for each value in the dataset, but the end result that this will be used for will have many objects and values. If this could be done by only having to copy the root object itself, it would be a lot simpler.

If anyone has gotten something like this to work and could share their insight, I would greatly appreciate it. Thanks in advance.

Try casting the string as a dictionary: self.session.custom.TestObject = dict(dataset.getValueAt(0, 'WhateverColumnWithObjectDataString))

What is the data type of your dataset field that stores the dictionary of session props? I suspect it's a string, in which case, if you write this into the root session prop, you're writing a literal string to its value, and converting it from a structure to a string.

@ryan.white is on the right track, but you'll need to use system.util.jsonDecode, not dict, to convert your string into py objects.

You should, however, also be converting the string into json format before writing it into the dataset field with system.util.jsonEncode

2 Likes

I think the system util JSON encode/decode functions have some issues with deep Perspective trees, so you might want to search through the forums to see where others/myself have contributed solutions to marshal Perspective objects to clean Python objects and/or JSON strings.

1 Like

Our access to the gateway is down right now, but I will try this and see how it goes. Thank you and Ryan very much