Ah, I should have been more explicit, sorry. This is a protocol for 
existing service (Apache Kafka, if it matters), so I can't actually choose 
a format. 

Anyway, the protocol turned to be quite specific, so I had to craft my own 
serialization routines. And, by the way, it turned to be quite easy: I just 
had to implement serialization for 4 types of things: 

 1. Integers - as simple as encoding number to network encoding and writing 
to stream.
 2. ASCII string - writing length + string data itself.
 3. Arrays - same as strings.
 4. Immutable types - iterate over fields and write them one by one. 

Here's a code snippet for the last point (which seems to be the only 
interesting part): 

function writeobj(io::IO, o)
    for f in fieldnames(o)
        writeobj(io, getfield(o, f))
    end
end
function readobj{T}(io::IO, ::Type{T})
    vals = Array(Any, length(T.types))
    for (i, t) in enumerate(T.types)
        vals[i] = readobj(io, t)
    end
    return T(vals...)
end


On Tuesday, May 10, 2016 at 11:42:23 PM UTC+3, Jameson wrote:
>
> I recommend going with a standard format such as ProtoBuf.jl, JSON.jl, or 
> other similar product.
>
> On Saturday, May 7, 2016 at 2:31:59 PM UTC-4, Andrei Zh wrote:
>>
>> I work on implementing a binary protocol for a service. This protocol is 
>> based on messages - structs that support integer numbers, variable-length 
>> arrays and other structs, i.e. something we could implement in Julia as:
>>
>> immutable Message1
>>     version::Int16
>>     length::Int32
>>     payload::Array{Message2,1}
>> end
>>
>> Creating a serializer for any such message is trivial, but I have about 
>> 50 of them and would like to automate it, i.e. I'd like a generic way to 
>> write an immutable structure to a stream. So far the closest candidate is 
>> StrPack.jl <https://strpackjl.readthedocs.io/en/latest/>, but it doesn't 
>> support variable-length array, which is the must for me. 
>>
>> Do we have something for this or I should come up with my own 
>> function/macro? 
>>
>

Reply via email to