1

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.

rajish shakya
  • 21
  • 1
  • 5

2 Answers2

1

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();
         }
     }
Haroon
  • 1,052
  • 13
  • 28
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;
}
Community
  • 1
  • 1
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • Sorry I updated my question as I might have been unclear. I was looking for Avro ByteArray serialize technique. – rajish shakya May 01 '16 at 01:27
  • 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. – jegtugado May 02 '16 at 13:52