private void verifyFromMap(String input) throws Exception { JsonParser p = new JsonFactory().createParser(input); assertEquals(JsonToken.START_OBJECT, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD4, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_STRING, p.nextToken()); assertEquals(TEXT2, getAndVerifyText(p)); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD3, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(-1, p.getIntValue()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD2, getAndVerifyText(p)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); assertEquals(DOUBLE_VALUE, p.getDoubleValue(), 0); assertEquals(JsonToken.END_OBJECT, p.nextToken()); assertNull(p.nextToken()); p.close(); }
public final void copyCurrentEvent(JsonParser jp) throws IOException, JsonProcessingException { switch (jp.getCurrentToken()) { case START_OBJECT: writeStartObject(); break; case END_OBJECT: writeEndObject(); break; case START_ARRAY: writeStartArray(); break; case END_ARRAY: writeEndArray(); break; case FIELD_NAME: writeFieldName(jp.getCurrentName()); break; case VALUE_STRING: if (jp.hasTextCharacters()) { writeString(jp.getTextCharacters(), jp.getTextOffset(), jp.getTextLength()); } else { writeString(jp.getText()); } break; case VALUE_NUMBER_INT: switch (jp.getNumberType()) { case INT: writeNumber(jp.getIntValue()); break; case BIG_INTEGER: writeNumber(jp.getBigIntegerValue()); break; default: writeNumber(jp.getLongValue()); } break; case VALUE_NUMBER_FLOAT: switch (jp.getNumberType()) { case BIG_DECIMAL: writeNumber(jp.getDecimalValue()); break; case FLOAT: writeNumber(jp.getFloatValue()); break; default: writeNumber(jp.getDoubleValue()); } break; case VALUE_TRUE: writeBoolean(true); break; case VALUE_FALSE: writeBoolean(false); break; case VALUE_NULL: writeNull(); break; case VALUE_EMBEDDED_OBJECT: writeObject(jp.getEmbeddedObject()); break; default: _cantHappen(); } }
@Override public Record deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String id = null; String type = null; Date creationDate = null; JsonToken currentToken = null; Map<String, Field> fields = new HashMap<>(); boolean processingFields = false; while ((currentToken = jp.nextValue()) != null) { switch (currentToken) { case START_OBJECT: processingFields = true; break; case END_OBJECT: processingFields = true; break; case VALUE_NUMBER_INT: try { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.INT, jp.getIntValue())); } catch (JsonParseException ex) { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.LONG, jp.getLongValue())); } break; case VALUE_NUMBER_FLOAT: try { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.FLOAT, jp.getFloatValue())); } catch (JsonParseException ex) { fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.DOUBLE, jp.getDoubleValue())); } break; case VALUE_FALSE: case VALUE_TRUE: fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.BOOLEAN, jp.getBooleanValue())); break; case START_ARRAY: logger.info(jp.getCurrentName()); break; case END_ARRAY: break; case VALUE_STRING: if (jp.getCurrentName() != null) { switch (jp.getCurrentName()) { case "id": id = jp.getValueAsString(); break; case "type": type = jp.getValueAsString(); break; case "creationDate": try { creationDate = sdf.parse(jp.getValueAsString()); // "Thu Sep 08 12:11:08 CEST 2016\" } catch (ParseException e) { e.printStackTrace(); } break; default: fields.put( jp.getCurrentName(), new Field(jp.getCurrentName(), FieldType.STRING, jp.getValueAsString())); break; } } break; default: break; } } Record record = new StandardRecord(type); record.setId(id); record.setType(type); record.setTime(creationDate); record.setFields(fields); return record; }