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

dev.harrel.jsonschema.JsonNodeUtil Maven / Gradle / Ivy

The newest version!
package dev.harrel.jsonschema;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

final class JsonNodeUtil {
    private JsonNodeUtil() {}

    static Optional> getAsObject(JsonNode node) {
        return node.isObject() ? Optional.of(node.asObject()) : Optional.empty();
    }

    static Optional getBooleanField(Map objectMap, String fieldName) {
        return Optional.ofNullable(objectMap.get(fieldName))
                .filter(JsonNode::isBoolean)
                .map(JsonNode::asBoolean);
    }

    static Optional getStringField(Map objectMap, String fieldName) {
        return Optional.ofNullable(objectMap.get(fieldName))
                .filter(JsonNode::isString)
                .map(JsonNode::asString);
    }

    static Optional> getObjectField(Map objectMap, String fieldName) {
        return Optional.ofNullable(objectMap.get(fieldName))
                .filter(JsonNode::isObject)
                .map(JsonNode::asObject);
    }

    static Optional> getVocabulariesObject(Map objectNode) {
        return getObjectField(objectNode, Keyword.VOCABULARY)
                .map(obj -> obj.entrySet().stream()
                        .filter(entry -> entry.getValue().isBoolean())
                        .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().asBoolean())))
                .map(Collections::unmodifiableMap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy