jsonschema.adapter

The JSON-type adapter trait.

Instance validation is templated over a small adapter so any JSON document type can be validated without conversion. An adapter is a struct with the following compile-time interface (all functions static):

struct MyAdapter
{
    alias Value = <your JSON type>;

    // Classify a value. `integer` is for integral representations only;
    // a float that happens to hold a whole number reports `floating`
    // (the validator itself implements JSON Schema's "integer means
    // zero fractional part" rule).
    static JsonKind kindOf(in Value v);

    // Wrap a string as a Value (used by propertyNames, which validates
    // object keys as string instances).
    static Value ofString(string s);

    // Scalar extraction; only called when kindOf reports the matching kind.
    static bool getBoolean(in Value v);
    static JsonNumber getNumber(in Value v);    // for integer and floating
    static string getString(in Value v);

    // Arrays; only called when kindOf reports array.
    static size_t arrayLength(in Value v);
    static const(Value) arrayAt(in Value v, size_t index);

    // Objects; only called when kindOf reports object. Iteration order may
    // be arbitrary — validation outcomes never depend on it.
    static size_t objectLength(in Value v);
    static const(Value)* objectGet(in Value v, string key); // null if absent
    static int objectEach(in Value v, scope int delegate(string key, in Value val) @safe dg);
}

Two adapters ship with the library: StdJsonAdapter here (std.json, dependency-free) and VibeJsonAdapter in the jsonschema:vibe subpackage (vibe.data.json). JsonNodeAdapter adapts the library's own internal representation, which is how generated schemas are meta-validated. To adapt another JSON library (asdf, mir-ion, …), implement the interface above and pass the adapter to Validator.validateWith!MyAdapter(value).

Members

Enums

JsonKind
enum JsonKind

Classification of a JSON value as seen by the validator.

Structs

JsonNodeAdapter
struct JsonNodeAdapter

Adapter for the library's own JsonNode (used internally for meta-schema validation of generated schemas, and available to callers).

JsonNumber
struct JsonNumber

A JSON number in its exact source representation. Integral values keep full 64-bit fidelity; asDouble is the (possibly lossy) numeric view.

StdJsonAdapter
struct StdJsonAdapter

Adapter for std.json.JSONValue.

Templates

isJsonAdapter
template isJsonAdapter(A)

True when A satisfies the adapter interface documented at module level.