This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Type Mismatch when Unserializing a TypedValue

Hey everyone!

I'm having trouble using the Unserialize function in a TypedValue object in VB Script.  I thought it might caused by the way I was moving the data to another function so I did a quick test by using the serialize function from the TypedValue of a property then trying to unserialize it right away into a new TypedValue object.

    Dim oTypedValue : Set oTypedValue = CreateObject("MFilesAPI.TypedValue")
    Dim aSerialized : aSerialized = oJobNumber.Serialize
    oTypedValue.Unserialize aSerialized, False

I'm getting a 'type mismatch' error on the unserialize function.  There's absolutely no examples of using these functions so I cannot tell why it's not working.  I'd appreciate any help anyone can provide!

Thank you,

Heather

Parents
  • Unfortunately, the code example I wrote in my previous post was not the correct solution.  That code will not throw errors but it creates a TypedValue object that does not contain the data that was in the source TypedValue.  In addition, using the UTF8Encoding to turn the binary array, from the serialize function, created a non-printable string that was not passing properly to the ExecuteVaultExtension function I was calling.

    The big issue here is the lack of binary array support in VBScript.  After a lot of research, and trial and error, yesterday, I figured out a working solution encoding the serialize binary array in a Base64 encoding.  Decoding the Base64 using an ADODB.Stream object creates a true binary array that can then be used to unserialize the original serialized data.

    All these problems are easily mitigated by using the Vault Application Framework, which I plan on migrating to, but I had to finish this up while I'm getting into that framework.

    Dim oJobNumber : Set oJobNumber = PropertyValues.SearchForProperty(JOB_NUMBER_ID).Value
    Dim oTypedValue : Set oTypedValue = CreateObject("MFilesAPI.TypedValue")
    Dim sSerialized : sSerialized = SerializeObjectToBase64Encode(oJobNumber)
    oTypedValue.Unserialize DeserializeObjectFromBase64Encode(sSerialized), False
    Err.Raise MFScriptCancel, oTypedValue.DisplayValue
    
    Function SerializeObjectToBase64Encode(ByRef oObject)
        Dim oByteStream : Set oByteStream = CreateObject("ADODB.Stream")
    	With oByteStream
    		.Open
    		.Type = 1 '# adTypeBinary #
    		.Write oObject.Serialize
    		.Position = 0
    	End With
    
    	Dim oNode : Set oNode = CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
    	oNode.dataType = "bin.base64"
    	oNode.nodeTypedValue = oByteStream.Read
    	SerializeObjectToBase64Encode = oNode.text
    	
    	Set oNode = Nothing
    	Set oByteStream = Nothing
    End Function
    
    Function DeserializeObjectFromBase64Encode(ByVal vCode)
    	Dim oNode : Set oNode = CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
    	oNode.dataType = "bin.base64"
    	oNode.text = vCode
    
        Dim oByteStream : Set oByteStream = CreateObject("ADODB.Stream")
    	With oByteStream
    		.Open
    		.Type = 1 '# adTypeBinary #
    		.Write oNode.nodeTypedValue
    		.Position = 0
    	End With
    
    	DeserializeObjectFromBase64Encode = oByteStream.Read
    
    	Set oNode = Nothing
    	Set oByteStream = Nothing
    End Function
    

    Maybe this will help someone in the future. Slight smile

    Heather

Reply
  • Unfortunately, the code example I wrote in my previous post was not the correct solution.  That code will not throw errors but it creates a TypedValue object that does not contain the data that was in the source TypedValue.  In addition, using the UTF8Encoding to turn the binary array, from the serialize function, created a non-printable string that was not passing properly to the ExecuteVaultExtension function I was calling.

    The big issue here is the lack of binary array support in VBScript.  After a lot of research, and trial and error, yesterday, I figured out a working solution encoding the serialize binary array in a Base64 encoding.  Decoding the Base64 using an ADODB.Stream object creates a true binary array that can then be used to unserialize the original serialized data.

    All these problems are easily mitigated by using the Vault Application Framework, which I plan on migrating to, but I had to finish this up while I'm getting into that framework.

    Dim oJobNumber : Set oJobNumber = PropertyValues.SearchForProperty(JOB_NUMBER_ID).Value
    Dim oTypedValue : Set oTypedValue = CreateObject("MFilesAPI.TypedValue")
    Dim sSerialized : sSerialized = SerializeObjectToBase64Encode(oJobNumber)
    oTypedValue.Unserialize DeserializeObjectFromBase64Encode(sSerialized), False
    Err.Raise MFScriptCancel, oTypedValue.DisplayValue
    
    Function SerializeObjectToBase64Encode(ByRef oObject)
        Dim oByteStream : Set oByteStream = CreateObject("ADODB.Stream")
    	With oByteStream
    		.Open
    		.Type = 1 '# adTypeBinary #
    		.Write oObject.Serialize
    		.Position = 0
    	End With
    
    	Dim oNode : Set oNode = CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
    	oNode.dataType = "bin.base64"
    	oNode.nodeTypedValue = oByteStream.Read
    	SerializeObjectToBase64Encode = oNode.text
    	
    	Set oNode = Nothing
    	Set oByteStream = Nothing
    End Function
    
    Function DeserializeObjectFromBase64Encode(ByVal vCode)
    	Dim oNode : Set oNode = CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
    	oNode.dataType = "bin.base64"
    	oNode.text = vCode
    
        Dim oByteStream : Set oByteStream = CreateObject("ADODB.Stream")
    	With oByteStream
    		.Open
    		.Type = 1 '# adTypeBinary #
    		.Write oNode.nodeTypedValue
    		.Position = 0
    	End With
    
    	DeserializeObjectFromBase64Encode = oByteStream.Read
    
    	Set oNode = Nothing
    	Set oByteStream = Nothing
    End Function
    

    Maybe this will help someone in the future. Slight smile

    Heather

Children
No Data