Search in sources :

Example 66 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project dcos-commons by mesosphere.

the class DefaultServiceSpecTest method invalidTaskName.

@Test
public void invalidTaskName() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("invalid-task-name.yml").getFile());
    try {
        DefaultServiceSpec.newGenerator(file, SCHEDULER_CONFIG).build();
        Assert.fail("Expected exception");
    } catch (JsonMappingException e) {
        Assert.assertTrue(e.getCause().toString(), e.getCause() instanceof JsonParseException);
        JsonParseException cause = (JsonParseException) e.getCause();
        Assert.assertTrue(cause.getMessage(), cause.getMessage().contains("Duplicate field 'meta-data-task'"));
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) File(java.io.File) Test(org.junit.Test)

Example 67 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project elasticsearch by elastic.

the class BaseXContentTestCase method testChecksForDuplicates.

public void testChecksForDuplicates() throws Exception {
    assumeTrue("Test only makes sense if XContent parser has strict duplicate checks enabled", XContent.isStrictDuplicateDetectionEnabled());
    XContentBuilder builder = builder().startObject().field("key", 1).field("key", 2).endObject();
    JsonParseException pex = expectThrows(JsonParseException.class, () -> createParser(builder).map());
    assertThat(pex.getMessage(), startsWith("Duplicate field 'key'"));
}
Also used : JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 68 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project elasticsearch by elastic.

the class FunctionScoreQueryBuilderTests method testMalformedThrowsException.

public void testMalformedThrowsException() throws IOException {
    String json = "{\n" + "    \"function_score\":{\n" + "        \"query\":{\n" + "            \"term\":{\n" + "                \"name.last\":\"banon\"\n" + "            }\n" + "        },\n" + "        \"functions\": [\n" + "            {\n" + "                {\n" + "            }\n" + "        ]\n" + "    }\n" + "}";
    JsonParseException e = expectThrows(JsonParseException.class, () -> parseQuery(json));
    assertThat(e.getMessage(), containsString("Unexpected character ('{"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 69 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project jackson-core by FasterXML.

the class ParserErrorHandlingTest method _testMangledNumbers.

private void _testMangledNumbers(int mode) throws Exception {
    String doc = "123true";
    JsonParser p = createParser(mode, doc);
    try {
        JsonToken t = p.nextToken();
        fail("Should have gotten an exception; instead got token: " + t);
    } catch (JsonParseException e) {
        verifyException(e, "expected space");
    }
    p.close();
    // Also test with floats
    doc = "1.5false";
    p = createParser(mode, doc);
    try {
        JsonToken t = p.nextToken();
        fail("Should have gotten an exception; instead got token: " + t);
    } catch (JsonParseException e) {
        verifyException(e, "expected space");
    }
    p.close();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 70 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project jackson-core by FasterXML.

the class ParserErrorHandlingTest method doTestInvalidKeyword1.

private void doTestInvalidKeyword1(int mode, String value) throws IOException {
    String doc = "{ \"key1\" : " + value + " }";
    JsonParser p = createParser(mode, doc);
    assertToken(JsonToken.START_OBJECT, p.nextToken());
    // get the exception early or late...
    try {
        assertToken(JsonToken.FIELD_NAME, p.nextToken());
        p.nextToken();
        fail("Expected an exception for malformed value keyword");
    } catch (JsonParseException jex) {
        verifyException(jex, "Unrecognized token");
        verifyException(jex, value);
    } finally {
        p.close();
    }
    // Try as root-level value as well:
    // may need space after for DataInput
    doc = value + " ";
    p = createParser(mode, doc);
    try {
        p.nextToken();
        fail("Expected an exception for malformed value keyword");
    } catch (JsonParseException jex) {
        verifyException(jex, "Unrecognized token");
        verifyException(jex, value);
    } finally {
        p.close();
    }
}
Also used : JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)143 IOException (java.io.IOException)73 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)18 Map (java.util.Map)18 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 File (java.io.File)11 HashMap (java.util.HashMap)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 JsonFactory (com.fasterxml.jackson.core.JsonFactory)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5