Search in sources :

Example 1 with JsonReducedValueParser

use of com.hazelcast.internal.json.JsonReducedValueParser in project hazelcast by hazelcast.

the class JsonSchemaHelper method findValueWithPattern.

/**
 * Extract the JsonValue that is stored in attributePath in input.
 * This method validates expectedPattern using attributePath while
 * extraction. If expectedPattern points to invalid or wrong attributes
 * at any point, this method returns {@code null}
 *
 * NOTE: this method cannot handle patterns with "any" in it.
 *
 * @param input             a byte array containing the target object
 * @param schemaNode        valid schema description to the target
 *                          object. The behavior is undefined if
 *                          description does not match the actual
 *                          object
 * @param expectedPattern   this cannot contain "any"
 * @param attributePath     this cannot contain "any"
 * @return                  JsonValue extracted or null
 */
@SuppressWarnings("checkstyle:npathcomplexity")
public static JsonValue findValueWithPattern(NavigableJsonInputAdapter input, JsonSchemaNode schemaNode, JsonPattern expectedPattern, JsonPathCursor attributePath) throws IOException {
    for (int i = 0; i < expectedPattern.depth(); i++) {
        if (attributePath.getNext() == null) {
            return null;
        }
        if (schemaNode.isTerminal()) {
            return null;
        }
        int expectedOrderIndex = expectedPattern.get(i);
        JsonSchemaStructNode structDescription = (JsonSchemaStructNode) schemaNode;
        if (structDescription.getChildCount() <= expectedOrderIndex) {
            return null;
        }
        JsonSchemaNameValue nameValue = structDescription.getChild(expectedOrderIndex);
        if (structMatches(input, nameValue, expectedOrderIndex, attributePath)) {
            schemaNode = nameValue.getValue();
        } else {
            return null;
        }
    }
    if (attributePath.getNext() == null) {
        if (schemaNode.isTerminal()) {
            // Otherwise, let the exceptions propagate
            try {
                JsonReducedValueParser valueParser = new JsonReducedValueParser();
                int valuePos = ((JsonSchemaTerminalNode) schemaNode).getValueStartLocation();
                return input.parseValue(valueParser, valuePos);
            } catch (ParseException parseException) {
                throw new HazelcastException(parseException);
            }
        } else {
            return NonTerminalJsonValue.INSTANCE;
        }
    }
    return null;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) JsonReducedValueParser(com.hazelcast.internal.json.JsonReducedValueParser) ParseException(com.hazelcast.internal.json.ParseException)

Aggregations

HazelcastException (com.hazelcast.core.HazelcastException)1 JsonReducedValueParser (com.hazelcast.internal.json.JsonReducedValueParser)1 ParseException (com.hazelcast.internal.json.ParseException)1