Template HasJsonAttributes

This template can be used to add functionality to a class by inserting class members and functions so that the object can represent an arbitrary list of JSON attributes.

template HasJsonAttributes ;

To use this mixing, simply call it by name in your class:

class MyNode {
  mixin HasJsonAttributes;
}

Contained Functions

NameDescription
addAttribute Adds a property with the given name [name] and value [value]. Neither [name] nor [value] can be [null]. It is illegal to add a property if another with the same name but different value already exists in this schema.
getAttributes Retrieve a map from JSON attribute names to their JSONValues.
hasAttributes Returns true if there is at least one attribute.

Example

import std.exception : assertThrown;
import std.json : JSONValue;
import avro.exception : AvroRuntimeException;

class Thing {
  mixin HasJsonAttributes;
}

Thing thing = new Thing();
thing.addAttribute("abe", 3);
thing.addAttribute("bob", "ham");

assert(thing.getAttributes().length == 2);
assert(thing.getAttributes()["bob"] == JSONValue("ham"));
assert(thing.getAttributes().orderedKeys == ["abe", "bob"]);

assertThrown!AvroRuntimeException(thing.addAttribute("abe", 4));