Skip to content

Custom Node Types

Jan Bernitt edited this page Jan 25, 2024 · 2 revisions

Warning

The documentation below has not been updated to the newest version

The JsonValue API can easily be extended with user defined types by simply declaring an interface with default methods.

public interface JsonPasswordValidation extends JsonObject
{
    default boolean isValidPassword()
    {
        return getBoolean( "isValidPassword" ).booleanValue();
    }

    default String getErrorMessage()
    {
        return getString( "errorMessage" ).string();
    }
}

Use as or asObject to "cast" a JsonValue to a custom type:

JsonValue jsonValue = fetchPasswordValidation();
JsonPasswordValidation result = jsonValue.as(JsonPasswordValidation.class );
assertTrue(result.isValidPassword());
assertNull(result.getErrorMessage());

Asserting a Schema or Structure

assertTrue(settings.isA(JsonPasswordValidation.class));

Recursively checks that all primitive members or members annotated with @Expected as defined by the JsonSettings interface are present.

When asObject is used instead of as the structural check is done implicitly and fails with an exception should the actual JSON not match the expectations defined by the provided user defined interface.

Clone this wiki locally