# How to dispatch this serializer function?

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-this-serializer-function/65981>\
**Category:** Performance\
**Tags:** dispatch\
**Created:** [August 7, 2021, 6:17am UTC](https://discourse.julialang.org/t/how-to-dispatch-this-serializer-function/65981 "2021-08-07T06:17:12Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 7, 2021, 9:38am UTC](https://discourse.julialang.org/t/how-to-dispatch-this-serializer-function/65981/12 "2021-08-07T09:38:52Z")

</div>

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:

```julia
# 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.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-dispatch-this-serializer-function/65981)._
