Class Parser

A parser for JSON-format schemas. Eached named schema parsed with a parser is added to the names known to the parser so that subsequently parsed schemas may refer to it by name.

class Parser ;

Methods

NameDescription
addSchemas (types) Adds the provided types to the set of defined and named types known to this parser.
parseAliases (node) Extracts and validates the "aliases" field for a schema.
parseFile (fileName) Builds a [Schema] using a path to a ".avsc" file.
parseJson (jsonSchema) Builds a [Schema] from a JSON parse tree.
parseText (text) Builds a [Schema] from JSON text.

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");

assert(schema.getFullname() == "example.avro.User");
assert(schema.getType() == Type.RECORD);
assert(schema.getFields().length == 3);
assert(schema.getField("name").getSchema().getType() == Type.STRING);

const Schema schema2 = schema.getField("favorite_number").getSchema();
assert(schema2.getType() == Type.UNION);
assert(schema2.getTypes().length == 2);
assert(schema2.getTypes()[0].getType() == Type.INT);
assert(schema2.getTypes()[1].getType() == Type.NULL);