Class GenericReader

A utility class to read GenericDatum from decoders.

class GenericReader ;

Constructors

NameDescription
this (s, decoder) Constructs a reader for the given schema using the given decoder.

Methods

NameDescription
read (d, isResolving, datum) Uses a given decoder to read [GenericDatum] from its serialized format.
read (datum) Reads a value off the decoder.

Example

import avro.parser : Parser;
import avro.codec.binarydecoder;

auto parser = new Parser();
Schema schema = parser.parseText(q"EOS
{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
   {"name": "name", "type": "string"},
   {"name": "favorite_number", "type": ["int", "null"]},
   {"name": "favorite_color", "type": ["string", "null"]}
]
}
EOS");

ubyte[] data = [
// Field: name
// len=3     b     o     b
    0x06, 0x62, 0x6F, 0x62,
// Field: favorite_number
// idx=0     8
    0x00, 0x10,
// Field: favorite_color
// idx=0 len=4     b     l     u     e
    0x00, 0x08, 0x62, 0x6C, 0x75, 0x65
];
auto decoder = binaryDecoder(data);
GenericReader reader = new GenericReader(schema, decoder);
GenericDatum datum;
reader.read(datum);

assert(datum["name"].getValue!string() == "bob");
assert(datum["favorite_number"].getValue!int() == 8);
assert(datum["favorite_color"].getValue!string() == "blue");

Example

import std.stdio;
import avro.parser : Parser;
import avro.codec.jsondecoder;

auto parser = new Parser();
Schema schema = parser.parseText(q"EOS
{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
   {"name": "name", "type": "string"},
   {"name": "favorite_number", "type": ["int", "null"]},
   {"name": "favorite_color", "type": ["string", "null"]}
]
}
EOS");

string data = q"EOS
{
"name": "bob",
"favorite_number": {"int": 8},
"favorite_color": null
}
EOS";
auto decoder = jsonDecoder(data);
GenericReader reader = new GenericReader(schema, decoder);
GenericDatum datum;
reader.read(datum);

assert(datum["name"].getValue!string() == "bob");
assert(datum["favorite_number"].getValue!int() == 8);
assert(datum["favorite_color"].getType() == Type.NULL);

Example

import std.stdio;
import avro.parser : Parser;
import std.exception : assertThrown;
import avro.codec.jsondecoder;

auto parser = new Parser();
Schema schema = parser.parseText(q"EOS
{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
   {"name": "name", "type": "string"},
   {"name": "favorite_number", "type": ["int", "null"]},
   {"name": "favorite_color", "type": ["string", "null"]}
]
}
EOS");

string data = q"EOS
{
"nam": "bob",
"favorite_numbero": {"int": 8},
"favorite_color": null
}
EOS";
auto decoder = jsonDecoder(data);
GenericReader reader = new GenericReader(schema, decoder);
GenericDatum datum;
assertThrown!AvroRuntimeException(reader.read(datum));