2

I am looking for a way to serialize Avro to a byte array in Avro C# library. There is a link to do for Avro Java library as described in following link from Avro documentation: https://cwiki.apache.org/confluence/display/AVRO/FAQ#FAQ-Serializingtoabytearray

Code copied from above link:

ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);

DatumWriter<User> writer = new SpecificDatumWriter<User>(User.getClassSchema());

writer.write(user, encoder);
encoder.flush();
out.close();
byte[] serializedBytes = out.toByteArray();

But I have not found a way to do in Avro c# library. I am basically looking for c# equivalent of above code.

2 Answers 2

2

Maybe something as follows, I used the following code to write to a Kineses Stream

public async Task RecordAsync(ISpecificRecord record, string partitionKey)
    {
        using (var ms = new MemoryStream())
        {
            var encoder = new BinaryEncoder(ms);
            var writer = new SpecificDefaultWriter(record.Schema);
            writer.Write(record, encoder);
            // AWS Kineses 
            var putRecordRequest = new PutRecordRequest
            {
                StreamName = _streamName,
                Data = ms,
                PartitionKey = partitionKey
            };
            await _kinesis.PutRecordAsync(putRecordRequest);
        }
    }

or

public byte[] Serialize(ISpecificRecord record) 
    {
        using (var ms = new MemoryStream())
        {
                var encoder = new BinaryEncoder(ms);
                var writer = new SpecificDefaultWriter(record.Schema);
                writer.Write(record, encoder);
                return ms.ToArray();
         }
     }
Sign up to request clarification or add additional context in comments.

Comments

0

You can use these methods to convert to and from an object to a byte array or vice-versa. Code extracted from https://stackoverflow.com/a/18205093/6138713

// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
    if(obj == null)
        return null;

    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);

    return ms.ToArray();
}

// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
    MemoryStream memStream = new MemoryStream();
    BinaryFormatter binForm = new BinaryFormatter();
    memStream.Write(arrBytes, 0, arrBytes.Length);
    memStream.Seek(0, SeekOrigin.Begin);
    Object obj = (Object) binForm.Deserialize(memStream);

    return obj;
}

2 Comments

Sorry I updated my question as I might have been unclear. I was looking for Avro ByteArray serialize technique.
Your avro object is still considered as a type of object in c# .net thus you can still use this method. Just pass whatever it is your object to turn it into a byte array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.