use of 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");
}
}
}
use of 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");
}
}
}
use of 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;
}
use of 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());
}
}
use of com.fasterxml.jackson.core.JsonParseException in project camel by apache.
the class StringMultiSelectPicklistDeserializer method deserialize.
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
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(String.class, length);
for (int i = 0; i < length; i++) {
// use factory method to create object
Array.set(resultArray, i, value[i].trim());
}
return resultArray;
} catch (Exception e) {
throw new JsonParseException(jp, "Exception reading multi-select pick list value", jp.getCurrentLocation(), e);
}
}
Aggregations