1

In Python 3.7, I want to encode an Avro object to String.
I found examples converting to byte array but not to string.

Code to convert to byte array:

def serialize(mapper, schema):
    bytes_writer = io.BytesIO()
    encoder = avro.io.BinaryEncoder(bytes_writer)
    writer1 = avro.io.DatumWriter(schema)
    writer1.write(mapper, encoder)
    return bytes_writer.getvalue()

mapper is a dictionary which will populate the avro object.
io provides with StringIO which I assume will need to be used instead of BytesIO but then what encoder to use with that? How do we serialize this?

Sarthak Agrawal
  • 321
  • 4
  • 17

1 Answers1

0

if, for example, a is your Avro object, you can use a.to_json() method of Avro and then json.dumps(a)

Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42