Search in sources :

Example 71 with JsonParseException

use of org.apache.flink.shaded.jackson2.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)

Example 72 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project jackson-core by FasterXML.

the class NumberCoercionTest method testToIntFailing.

@SuppressWarnings("resource")
public void testToIntFailing() throws Exception {
    for (int mode : ALL_STREAMING_MODES) {
        JsonParser p;
        // long -> error
        long big = 1L + Integer.MAX_VALUE;
        p = createParser(mode, String.valueOf(big));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(big, p.getLongValue());
        try {
            p.getIntValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of int");
        }
        long small = -1L + Integer.MIN_VALUE;
        p = createParser(mode, String.valueOf(small));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(Long.valueOf(small), p.getNumberValue());
        assertEquals(small, p.getLongValue());
        try {
            p.getIntValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of int");
        }
        // double -> error
        p = createParser(mode, String.valueOf(big) + ".0");
        assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
        assertEquals((double) big, p.getDoubleValue());
        try {
            p.getIntValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of int");
        }
        p = createParser(mode, String.valueOf(small) + ".0");
        assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
        assertEquals((double) small, p.getDoubleValue());
        try {
            p.getIntValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of int");
        }
        // BigInteger -> error
        p = createParser(mode, String.valueOf(big));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(BigInteger.valueOf(big), p.getBigIntegerValue());
        try {
            p.getIntValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of int");
        }
        p = createParser(mode, String.valueOf(small));
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(BigInteger.valueOf(small), p.getBigIntegerValue());
        try {
            p.getIntValue();
            fail("Should not pass");
        } catch (JsonParseException e) {
            verifyException(e, "out of range of int");
        }
    }
}
Also used : JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 73 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project hadoop by apache.

the class JobSubmitter method readTokensFromFiles.

@SuppressWarnings("unchecked")
private void readTokensFromFiles(Configuration conf, Credentials credentials) throws IOException {
    // add tokens and secrets coming from a token storage file
    String binaryTokenFilename = conf.get(MRJobConfig.MAPREDUCE_JOB_CREDENTIALS_BINARY);
    if (binaryTokenFilename != null) {
        Credentials binary = Credentials.readTokenStorageFile(FileSystem.getLocal(conf).makeQualified(new Path(binaryTokenFilename)), conf);
        credentials.addAll(binary);
    }
    // add secret keys coming from a json file
    String tokensFileName = conf.get("mapreduce.job.credentials.json");
    if (tokensFileName != null) {
        LOG.info("loading user's secret keys from " + tokensFileName);
        String localFileName = new Path(tokensFileName).toUri().getPath();
        try {
            // read JSON
            Map<String, String> nm = READER.readValue(new File(localFileName));
            for (Map.Entry<String, String> ent : nm.entrySet()) {
                credentials.addSecretKey(new Text(ent.getKey()), ent.getValue().getBytes(Charsets.UTF_8));
            }
        } catch (JsonMappingException | JsonParseException e) {
            LOG.warn("couldn't parse Token Cache JSON file with user secret keys");
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Text(org.apache.hadoop.io.Text) JsonParseException(com.fasterxml.jackson.core.JsonParseException) File(java.io.File) Map(java.util.Map) Credentials(org.apache.hadoop.security.Credentials)

Example 74 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project hadoop by apache.

the class DomainLogInfo method doParse.

protected long doParse(TimelineDataManager tdm, JsonParser parser, ObjectMapper objMapper, UserGroupInformation ugi, boolean appCompleted) throws IOException {
    long count = 0;
    long bytesParsed;
    long bytesParsedLastBatch = 0;
    boolean putError = false;
    try {
        MappingIterator<TimelineDomain> iter = objMapper.readValues(parser, TimelineDomain.class);
        while (iter.hasNext()) {
            TimelineDomain domain = iter.next();
            domain.setOwner(ugi.getShortUserName());
            LOG.trace("Read domain {}", domain.getId());
            ++count;
            bytesParsed = parser.getCurrentLocation().getCharOffset() + 1;
            LOG.trace("Parser now at offset {}", bytesParsed);
            try {
                tdm.putDomain(domain, ugi);
                setOffset(getOffset() + bytesParsed - bytesParsedLastBatch);
                bytesParsedLastBatch = bytesParsed;
            } catch (YarnException e) {
                putError = true;
                throw new IOException("Error posting domain", e);
            } catch (IOException e) {
                putError = true;
                throw new IOException("Error posting domain", e);
            }
        }
    } catch (IOException e) {
        // incomplete file which are treated as EOF until app completes
        if (appCompleted || putError) {
            throw e;
        }
    } catch (RuntimeException e) {
        if (appCompleted || !(e.getCause() instanceof JsonParseException)) {
            throw e;
        }
    }
    return count;
}
Also used : TimelineDomain(org.apache.hadoop.yarn.api.records.timeline.TimelineDomain) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException)

Example 75 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project camel by apache.

the class MultiSelectPicklistDeserializer method deserialize.

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    // validate enum class
    if (enumClass == null) {
        throw new JsonMappingException(jp, "Unable to parse unknown pick-list type");
    }
    final String listValue = jp.getText();
    try {
        // parse the string of the form value1;value2;...
        final String[] value = listValue.split(";");
        final int length = value.length;
        final Object resultArray = Array.newInstance(enumClass, length);
        for (int i = 0; i < length; i++) {
            // use factory method to create object
            Array.set(resultArray, i, factoryMethod.invoke(null, value[i].trim()));
        }
        return resultArray;
    } catch (Exception e) {
        throw new JsonParseException(jp, "Exception reading multi-select pick list value", jp.getCurrentLocation());
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)145 IOException (java.io.IOException)75 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 Map (java.util.Map)19 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 HashMap (java.util.HashMap)12 File (java.io.File)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