All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.relogiclabs.json.schema.tree.JsonTree Maven / Gradle / Ivy

Go to download

The New JSON Schema prioritizes simplicity, conciseness, and readability, making it user-friendly and accessible without the need for extensive prior knowledge. It offers efficient read-write facilities, precise JSON document definition through various data types and functions, and extensibility to meet modern web service diverse requirements.

There is a newer version: 1.12.1
Show newest version
package com.relogiclabs.json.schema.tree;

import com.relogiclabs.json.schema.internal.antlr.JsonLexer;
import com.relogiclabs.json.schema.internal.antlr.JsonParser;
import com.relogiclabs.json.schema.internal.tree.JsonTreeVisitor;
import com.relogiclabs.json.schema.internal.util.LexerErrorListener;
import com.relogiclabs.json.schema.internal.util.ParserErrorListener;
import com.relogiclabs.json.schema.type.JRoot;
import lombok.Getter;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

import static com.relogiclabs.json.schema.tree.TreeType.JSON_TREE;

@Getter
public final class JsonTree implements DataTree {
    private final RuntimeContext runtime;
    private final JRoot root;

    public JsonTree(RuntimeContext runtime, String input) {
        this.runtime = runtime;
        var jsonLexer = new JsonLexer(CharStreams.fromString(input));
        jsonLexer.removeErrorListeners();
        jsonLexer.addErrorListener(LexerErrorListener.JSON);
        var jsonParser = new JsonParser(new CommonTokenStream(jsonLexer));
        jsonParser.removeErrorListeners();
        jsonParser.addErrorListener(ParserErrorListener.JSON);
        root = (JRoot) new JsonTreeVisitor(runtime).visit(jsonParser.json());
    }

    @Override
    public boolean match(DataTree dataTree) {
        var result = root.match(dataTree.getRoot());
        result &= runtime.invokeValidators();
        return result;
    }

    @Override
    public TreeType getType() {
        return JSON_TREE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy