Lots of repetitions in the code. I think this covers Numbers, Characters and Strings, and arrays thereof.
It doesn’t do Arrays of Any
, because I have to leave the house. There probably needs to be some special handling of that, an extra _serialize
method. But there would be virtually no code repetition, I’ll wager.
Warning: Totally untested:
# Encode number types
classToByte(::Float64) = Int8(0)
classToByte(::Union{Float32, Float16}) = Int8(1)
classToByte(::Bool) = Int8(2)
classToByte(::Union{Char, String}) = Int8(3)
classToByte(::Int8) = Int8(4)
classToByte(::UInt8) = Int8(5)
classToByte(::Int16) = Int8(6)
classToByte(::UInt16) = Int8(7)
classToByte(::Int32) = Int8(8)
classToByte(::UInt32) = Int8(9)
classToByte(::Int64) = Int8(10)
classToByte(::UInt64) = Int8(11)
classToByte(x) = UInt8(254)
dimensions(x) = (UInt8(2), UInt32(1), UInt32(length(x))) # works for numbers, chars and strings
dimensions(x::AbstractArray) = (UInt8(max(2, ndims(x))), UInt32.(size(x))...)
prefix(v) = (classToByte(v), dimensions(v)...)
function serializej2m(v)
io = IOBuffer(UInt8[]; append = true)
_serialize(io, v)
return take!(io)
end
function _serialize(io, v)
# Meta information
write(io, prefix(v)...)
# Data
write(io, v)
end
My code may be buggy or even wrong, but I think this can be expressed extremely concisely with multiple dispatch, using functions like prefix
etc.
But frankly, I think this could have been expressed almost as concisely in Matlab, too. The original code was just not well organized.