Easy serialisation using SaveState

Gerry Quinn

Active Member
Joined
Jun 24, 2017
Somebody asked about this so I'll post what I use. The Test function at the end should show how to use it.

Code:
Strict


' Improved serialisation code
' For Android it seems I can't use 8-bit chars freely


Import mojo.app


Private
Global writeStack:Stack< Int >
Global readArray:Int[]
Global readLength:Int
Global index:Int
Global readError:Bool
Public


Function _____________________________________________Setup_________________________________________:Void()
End



Function StartWrite:Void( version:Int = 0 )
    If writeStack = Null
        writeStack = New Stack< Int >()
    End
    writeStack.Clear()
    WriteInt( version )
End


Function FinishWrite:Void()
    SaveState( MakeSaveString( writeStack.ToArray() ) )
End


Function StartRead:Int()
    readArray = MakeLoadArray( LoadState() )
    readLength = readArray.Length()
    index = 0
    readError = False   
    Return ReadInt()
End


Function WasReadError:Bool()
    Return readError
End


Function AllRead:Bool()
    Return index >= readLength
End


' Stretch string into half-bytes
Function MakeSaveString:String( data:Int[] )
    Local BASE:Int = "A"[ 0 ]
    Local len:Int = data.Length()
    Local stretch:Int[] = New Int[ len * 2 ]
    Local index:Int
    For Local i:Int = 0 Until len
        Local val:Int = data[ i ]
        stretch[ index ] = BASE + val Shr 4
        index += 1
        stretch[ index ] = BASE + ( val & 15 )
        index += 1
    Next
    Return String.FromChars( stretch )
End


' Compress back to bytes
Function MakeLoadArray:Int[]( str:String )
    Local BASE:Int = "A"[ 0 ]
    Local stretch:Int[] = str.ToChars()
    Local data:Int[] = New Int[ stretch.Length() / 2 ]
    Local len:Int = data.Length()
    Local index:Int
    For Local i:Int = 0 Until len
        Local xx:Int = stretch[ index ]
        Local val:Int = ( xx - BASE ) Shl 4
        index += 1
        xx = stretch[ index ]
        val |= xx - BASE
        index += 1
        data[ i ] = val
    Next
    Return data
End


Private
Function _____________________________________________Private_______________________________________:Void()
End


Function ReadVal:Int()
    If index >= readLength
        readError = True
    End
    If readError
        Return 0
    End
    Local val:Int = readArray[ index ]
    index += 1
    Return val
End


Function ReadVals:Int[]( nVals:Int )
    If index + nVals > readLength
        readError = True
    End
    If readError
        Return New Int[ nVals ]
    End
    Local vals:Int[] = readArray[ index .. index + nVals ]
    index += nVals
    Return vals
End


Public
Function _____________________________________________Types_________________________________________:Void()
End


Function WriteByte:Void( val:Int )
    writeStack.Push( val )
End


Function ReadByte:Int()
    Return ReadVal()
End


Function WriteShort:Void( val:Int )
    writeStack.Push( ( val Shr 8 ) & $FF )
    writeStack.Push( val & $FF )
End


Function ReadShort:Int()
    Local vals:Int[] = ReadVals( 2 )
    Return ( vals[ 0 ] Shl 8 ) | vals[ 1 ]
End


Function WriteInt:Void( val:Int )
    writeStack.Push( ( val Shr 24 ) & $FF )
    writeStack.Push( ( val Shr 16 ) & $FF )
    writeStack.Push( ( val Shr 8 ) & $FF )
    writeStack.Push( val & $FF )
End


Function ReadInt:Int()
    Local vals:Int[] = ReadVals( 4 )
    Return ( vals[ 0 ] Shl 24 ) | ( vals[ 1 ] Shl 16 ) | ( vals[ 2 ] Shl 8 ) | vals[ 3 ]
End


Function WriteBool:Void( val:Bool )
    If val
        writeStack.Push( 1 )
    Else
        writeStack.Push( 0 )
    End
End


Function ReadBool:Bool()
    Return ReadVal() <> 0
End


Function WriteString:Void( str:String )
    Local chars:Int[] = str.ToChars()
    Local len:Int = chars.Length()
    WriteInt( len )
    For Local i:Int = 0 Until len
        writeStack.Push( chars[ i ] )
    Next
End


Function ReadString:String()
    Local len:Int = ReadInt()
    If len > 0
        Return String.FromChars( ReadVals( len ) )
    Else
        Return ""
    End
End


Function _____________________________________________Test__________________________________________:Void()
End


Function TestReadWrite:Void()
    StartWrite( 10 )
    WriteInt( 100 )
    WriteInt( 1000000000 )
    WriteInt( -1 )
    WriteByte( 7 )
    WriteShort( 983 )
    WriteShort( -1 )
    WriteString( "string" )
    WriteBool( True )
    FinishWrite()
    Print StartRead()
    Print "------"
    Print ReadInt()
    Print ReadInt()
    Print ReadInt()
    Print ReadByte()
    Print ReadShort()
    Print ReadShort()
    Print ReadString()
    If ReadBool() Then Print "True" Else Print "False"
    If AllRead() And Not WasReadError() Then Print "Good" Else Print "Bad"
End
 
Top Bottom