com.fasterxml.jackson.databind.RuntimeJsonMappingException Java Examples

The following examples show how to use com.fasterxml.jackson.databind.RuntimeJsonMappingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Config.java    From digdag with Apache License 2.0 6 votes vote down vote up
public Config getNestedOrderedOrGetEmpty(String key)
{
    JsonNode value = getNode(key);
    if (value == null) {
        value = newObjectNode();
    }
    else if (value.isArray()) {
        Config config = new Config(mapper);
        Iterator<JsonNode> ite = ((ArrayNode) value).elements();
        while (ite.hasNext()) {
            JsonNode nested = ite.next();
            if (!(nested instanceof ObjectNode)) {
                throw new RuntimeJsonMappingException("Expected object but got "+nested);
            }
            // here assumes config is an order-preserving map
            config.setAll(new Config(mapper, (ObjectNode) nested));
        }
        return config;
    }
    else if (!value.isObject()) {
        throw new ConfigException("Parameter '"+key+"' must be an object or array of objects");
    }
    return new Config(mapper, (ObjectNode) value);
}
 
Example #2
Source File: JsonObjectReaderImpl.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Override
public Object read() throws IOException {
  if (closed) {
    throw new IOException("The parser is closed");
  }
  try {
    Object value = null;
    switch (mode) {
      case ARRAY_OBJECTS:
        value = readObjectFromArray();
        break;
      case MULTIPLE_OBJECTS:
        value = readObjectFromStream();
        break;
    }
    return value;
  } catch (RuntimeJsonMappingException ex) {
    throw new JsonParseException(ex.toString(), jsonParser.getTokenLocation(), ex);
  }
}
 
Example #3
Source File: CsvInputStreamMapperTest.java    From pocket-etl with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeJsonMappingException.class)
public void nextThrowsRuntimeJsonMappingExceptionIfInputStreamContainsRowWithExtraColumn() {
    InputStream inputStream = createInputStreamFromString("\"WORKERID1\",123,\"WORKERID1\"");
    Iterator<TestDTO> iterator = csvInputStreamMapper.apply(inputStream);

    iterator.next();
}
 
Example #4
Source File: CsvInputStreamMapperTest.java    From pocket-etl with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeJsonMappingException.class)
public void nextThrowsRuntimeJsonMappingExceptionIfInputStreamContainsRowWithMissingColumn() {
    InputStream inputStream = createInputStreamFromString("\"WORKERID1\"");
    Iterator<TestDTO> iterator = csvInputStreamMapper.apply(inputStream);

    iterator.next();
}
 
Example #5
Source File: CsvInputStreamMapperTest.java    From pocket-etl with Apache License 2.0 5 votes vote down vote up
@Test(expected = RuntimeJsonMappingException.class)
public void nextThrowsRuntimeJsonMappingExceptionIfInputStreamContainsWrongDataType() {
    InputStream inputStream = createInputStreamFromString("\"WORKERID1\",\"BadNumber\"");
    Iterator<TestDTO> iterator = csvInputStreamMapper.apply(inputStream);

    iterator.next();
}
 
Example #6
Source File: JacksonDecoder.java    From metacat with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object decode(final Response response, final Type type) throws IOException {
    if (
        response.status() == HttpURLConnection.HTTP_NO_CONTENT
            || response.body() == null
            || (response.body().length() != null && response.body().length() == 0)
        ) {
        return null;
    }

    try (final Reader reader = response.body().asReader()) {
        return this.mapper.readValue(reader, this.mapper.constructType(type));
    } catch (final JsonMappingException jme) {
        // The case where for whatever reason (most likely bad design) where the server returned OK and
        // trying to de-serialize the content had no content (e.g. the return status should have been no-content)
        if (response.status() == HttpURLConnection.HTTP_OK
            && jme.getMessage().startsWith(NO_CONTENT_MESSAGE)) {
            return null;
        }

        throw jme;
    } catch (final RuntimeJsonMappingException e) {
        if (e.getCause() != null && e.getCause() instanceof IOException) {
            throw IOException.class.cast(e.getCause());
        }
        throw e;
    }
}
 
Example #7
Source File: YamlLoader.java    From digdag with Apache License 2.0 5 votes vote down vote up
private ObjectNode normalizeValidateObjectNode(Object object)
    throws IOException
{
    JsonNode node = treeObjectMapper.readTree(treeObjectMapper.writeValueAsString(object));
    if (!node.isObject()) {
        throw new RuntimeJsonMappingException("Expected object to load Config but got "+node);
    }
    return (ObjectNode) node;
}
 
Example #8
Source File: YamlConfigLoader.java    From digdag with Apache License 2.0 5 votes vote down vote up
private ObjectNode normalizeValidateObjectNode(Object object)
    throws IOException
{
    JsonNode node = treeObjectMapper.readTree(treeObjectMapper.writeValueAsString(object));
    if (!node.isObject()) {
        throw new RuntimeJsonMappingException("Expected object to load Config but got "+node);
    }
    return (ObjectNode) node;
}
 
Example #9
Source File: Config.java    From digdag with Apache License 2.0 5 votes vote down vote up
@JsonCreator
public static Config deserializeFromJackson(@JacksonInject ObjectMapper mapper, JsonNode object)
{
    if (!object.isObject()) {
        throw new RuntimeJsonMappingException("Expected object but got "+object);
    }
    return new Config(mapper, (ObjectNode) object);
}