X
147

How to pass an IDL structure into (or out of) an IDL-IDL bridge child process

The IDL-IDL bridge can be useful for parallel processing, among many other things. Variable data from your parent IDL session can be copied into the child (bridge) process using the SetVar function of the IDL_IDLBridge object. However, this function only supports scalar and array variables of numeric and string type. Complex datatypes, such as structures, are not supported using this method.

However, to work around this limitation, you can convert the structure into a string with IDLDehydrate and JSON_SERIALIZE. The resulting string can be copied to the bridge. Then, inside the bridge, use JSON_PARSE and IDLHydrate to bring it back to its original, identical structure form. 

Below is an IDL code example of this process in action:
 

PRO test_bridgeStructurePass

;Create a structure, convert it to a hash, then to a string
struct = {name:'NameOne', data1:50, data2:['a','b','c'], data3:FLTARR(12)}
h = hash(struct,/extract)
str = json_serialize(h.dehydrate())

;Spawn an IDL-IDL bridge and copy the string variable into the bridge session
oBridge = IDL_IDLBridge()
oBridge.SetVar,'j',str

;Inside the bridge, convert the string to a hash, and then the hash back to a structure
oBridge.EXECUTE, 'd =JSON_PARSE(j)'
oBridge.EXECUTE, "h = IDLHydrate(d,TYPE='ORDEREDHASH')"
oBridge.EXECUTE, 's = h.ToStruct()'

;Modify the structure elements inside the bridge
oBridge.EXECUTE, 's.data1=s.data1*3'
oBridge.EXECUTE, 's.data2=["g","e","f"]'
oBridge.EXECUTE, 's.data3=s.data3+5'

;Convert the structure back to a string inside the bridge
oBridge.EXECUTE, 'h = hash(s,/extract)'
oBridge.EXECUTE, 'str = json_serialize(h.dehydrate())'

;Copy the new string back into the parent IDL session from the bridge and convert it back into a structure
j=oBridge.GetVar('str')
d =JSON_PARSE(j)
h2 = IDLHydrate(d,TYPE='ORDEREDHASH')
s = h2.ToStruct()

;Print the updated structure elements
print,s.data1
print,s.data2
print,s.data3

;Terminate the bridge
oBridge.Cleanup


END

 

Note: The Hydrate and Dehydrate functionality was added in IDL 8.5.2. You must use at least that version for this trick to be possible.

 

 

Created: BC-US 12/23/204
Reviewed: BC-EU 1/25/2025