Search in sources :

Example 1 with ParseException

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

the class JsonParser_Test method parse_failsOnTooDeeplyNestedObject.

@Test
public void parse_failsOnTooDeeplyNestedObject() {
    JsonObject object = new JsonObject();
    for (int i = 0; i < 1001; i++) {
        object = new JsonObject().add("foo", object);
    }
    final String input = object.toString();
    ParseException exception = assertException(ParseException.class, new RunnableEx() {

        public void run() throws IOException {
            parser.parse(input);
        }
    });
    assertEquals("Nesting too deep at 1:7002", exception.getMessage());
}
Also used : JsonObject(com.hazelcast.internal.json.JsonObject) RunnableEx(com.hazelcast.internal.json.TestUtil.RunnableEx) ParseException(com.hazelcast.internal.json.ParseException) IOException(java.io.IOException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with ParseException

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

the class JsonParser_Test method parse_failsOnTooDeeplyNestedMixedObject.

@Test
public void parse_failsOnTooDeeplyNestedMixedObject() {
    JsonValue value = new JsonObject();
    for (int i = 0; i < 1001; i++) {
        value = i % 2 == 0 ? new JsonArray().add(value) : new JsonObject().add("foo", value);
    }
    final String input = value.toString();
    ParseException exception = assertException(ParseException.class, new RunnableEx() {

        public void run() throws IOException {
            parser.parse(input);
        }
    });
    assertEquals("Nesting too deep at 1:4002", exception.getMessage());
}
Also used : JsonArray(com.hazelcast.internal.json.JsonArray) JsonValue(com.hazelcast.internal.json.JsonValue) JsonObject(com.hazelcast.internal.json.JsonObject) RunnableEx(com.hazelcast.internal.json.TestUtil.RunnableEx) ParseException(com.hazelcast.internal.json.ParseException) IOException(java.io.IOException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with ParseException

use of com.hazelcast.internal.json.ParseException 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)

Example 4 with ParseException

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

the class ManagementCenterServiceIntegrationTest method testTimedMemberState_returnsNotNull.

@Test
public void testTimedMemberState_returnsNotNull() {
    String responseString = mcs.getTimedMemberStateJson().orElse(null);
    assertFalse(isNullOrEmpty(responseString));
    JsonObject object;
    try {
        object = Json.parse(responseString).asObject();
    } catch (ParseException e) {
        throw new AssertionError("Failed to parse JSON: " + responseString);
    }
    TimedMemberState memberState = new TimedMemberState();
    memberState.fromJson(object.get("timedMemberState").asObject());
    assertEquals(CLUSTER_NAME, memberState.getClusterName());
}
Also used : JsonObject(com.hazelcast.internal.json.JsonObject) ParseException(com.hazelcast.internal.json.ParseException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with ParseException

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

the class JsonParser_Test method parse_reader_rejectsEmpty.

@Test
public void parse_reader_rejectsEmpty() {
    ParseException exception = assertException(ParseException.class, new RunnableEx() {

        public void run() throws IOException {
            parser.parse(new StringReader(""));
        }
    });
    assertEquals(0, exception.getLocation().offset);
    assertThat(exception.getMessage(), startsWith("Unexpected end of input at"));
}
Also used : StringReader(java.io.StringReader) RunnableEx(com.hazelcast.internal.json.TestUtil.RunnableEx) ParseException(com.hazelcast.internal.json.ParseException) IOException(java.io.IOException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ParseException (com.hazelcast.internal.json.ParseException)8 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Test (org.junit.Test)6 RunnableEx (com.hazelcast.internal.json.TestUtil.RunnableEx)5 IOException (java.io.IOException)5 JsonObject (com.hazelcast.internal.json.JsonObject)3 JsonArray (com.hazelcast.internal.json.JsonArray)2 StringReader (java.io.StringReader)2 HazelcastException (com.hazelcast.core.HazelcastException)1 JsonReducedValueParser (com.hazelcast.internal.json.JsonReducedValueParser)1 JsonValue (com.hazelcast.internal.json.JsonValue)1 Location (com.hazelcast.internal.json.Location)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1