canimus
6/15/2017 - 6:40 AM

Avro parsing in Javascript

Avro parsing in Javascript

// Import library
var avro = require('avro-js')

// Schema definition
var type = avro.parse({
  name: 'Pet',
  type: 'record',
  fields: [
    {name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
    {name: 'name', type: 'string'}
  ]
});

// Example document
var pet = {kind: 'CAT', name: 'Albert'};

// Buffer creation
var buf = type.toBuffer(pet);

// Reverse convertion
var obj = type.fromBuffer(buf);