-
Encode and decode objects:
var type = avro.parse({
name: 'Pet',
type: 'record',
fields: [
{name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
{name: 'name', type: 'string'}
]
});
var pet = {kind: 'CAT', name: 'Albert'};
var buf = type.toBuffer(pet);
var obj = type.fromBuffer(buf);
-
Generate random instances of a schema:
var type = avro.parse('{"type": "fixed", "name": "Id", "size": 4}');
var id = type.random();
-
Check whether an object fits a given schema:
var type = avro.parse('./Person.avsc');
var person = {name: 'Bob', address: {city: 'Cambridge', zip: '02139'}};
var status = type.isValid(person);
-
Get a readable stream of decoded records from an Avro
container file (not in the browser):
avro.createFileDecoder('./records.avro')
.on('metadata', function (type) { })
.on('data', function (record) { });
-
Implement recursive schemata (due to lack of duck-typing):
const recursiveRecordType = avro.parse({
"type": "record",
"name": "LongList",
"fields" : [
{"name": "value", "type": "long"},
{"name": "next", "type": ["null", "LongList"]}
]
});
const validRecursiveRecordDTO = {
value: 1,
next: {
LongList: {
value: 2,
next: null
}
}
};
const serializedValid = recursiveRecordType.parse(validRecursiveRecordDTO);
const invalidRecursiveRecordDTO = {
value: 1,
next: {
value: 2,
next: null
}
};
const serializedInvalid = recursiveRecordType.parse(invalidRecursiveRecordDTO);