Visual Basic 6 function "serialize"

Go back

Below you'll find the source for the Visual Basic 6 function serialize.

Attribute VB_Name = "modSerialize"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Generates a storable representation of a value
' Same syntax as the PHP function 'serialize'
' See also: http://www.php.net/manual/en/function.serialize.php
' @param    Variant arrdata         The array which must be serialized
' @return   String                  A PHP-style serialized value
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function serialize(arrdata As Variant) As String
    Dim retval As String, i As Integer
    
    Select Case TypeName(arrdata)
        Case "Decimal"
            retval = retval & "d:" & Replace(CStr(arrdata), ",", ".") & ";"
        Case "Integer"
            retval = retval & "i:" & CStr(arrdata) & ";"
        Case "Null"
            retval = retval & "N;"
        Case "Boolean"
            If arrdata Then
                retval = retval & "b:1;"
            Else
                retval = retval & "b:0;"
            End If
        Case "Variant()"
            retval = retval & "a:" & CStr(UBound(arrdata) - LBound(arrdata) + 1) & ":{"
            For i = LBound(arrdata) To UBound(arrdata)
                retval = retval & serialize(arrdata(i))
            Next i
            retval = retval & "}"
        Case Else ' String, but also all other types
            retval = retval & "s:" & CInt(Len(CStr(arrdata))) & ":" & Chr(34) & CStr(arrdata) & Chr(34) & ";"
    End Select
    
    serialize = retval
End Function