private Object success(Class<?> clazz, HttpInputMessage inputMessage) throws JsonParseException, IOException { // Note, parsing code used from ektorp project JsonParser jp = objectMapper.getJsonFactory().createJsonParser(inputMessage.getBody()); if (jp.nextToken() != JsonToken.START_OBJECT) { throw new RuntimeException("Expected data to start with an Object"); } Map<String, Integer> fields = readHeaderFields(jp); List result; if (fields.containsKey(TOTAL_ROWS_FIELD_NAME)) { int totalRows = fields.get(TOTAL_ROWS_FIELD_NAME); if (totalRows == 0) { return Collections.emptyList(); } result = new ArrayList(totalRows); } else { result = new ArrayList(); } ParseState state = new ParseState(); Object first = parseFirstRow(jp, state, clazz); if (first == null) { return Collections.emptyList(); } else { result.add(first); } while (jp.getCurrentToken() != null) { skipToField(jp, state.docFieldName, state); if (atEndOfRows(jp)) { return result; } result.add(jp.readValueAs(clazz)); endRow(jp, state); } return result; }
private Object parseFirstRow(JsonParser jp, ParseState state, Class clazz) throws JsonParseException, IOException, JsonProcessingException, JsonMappingException { skipToField(jp, VALUE_FIELD_NAME, state); JsonNode value = null; if (atObjectStart(jp)) { value = jp.readValueAsTree(); jp.nextToken(); if (isEndOfRow(jp)) { state.docFieldName = VALUE_FIELD_NAME; Object doc = objectMapper.readValue(value, clazz); endRow(jp, state); return doc; } } skipToField(jp, INCLUDED_DOC_FIELD_NAME, state); if (atObjectStart(jp)) { state.docFieldName = INCLUDED_DOC_FIELD_NAME; Object doc = jp.readValueAs(clazz); endRow(jp, state); return doc; } return null; }