Example #1
0
    @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;
    }
  private void parseField(
      List<String> fieldNames,
      List<ColumnMetaData.Rep> fieldTypes,
      Row.RowBuilder rowBuilder,
      JsonParser parser)
      throws IOException {
    final String fieldName = parser.getCurrentName();

    // Move to next token, which is name's value
    JsonToken token = parser.nextToken();
    int i = fieldNames.indexOf(fieldName);
    if (i < 0) {
      return;
    }
    ColumnMetaData.Rep type = fieldTypes.get(i);
    switch (token) {
      case VALUE_NUMBER_INT:
        if (type == null) {
          type = ColumnMetaData.Rep.INTEGER;
        }
        // fall through
      case VALUE_NUMBER_FLOAT:
        if (type == null) {
          type = ColumnMetaData.Rep.FLOAT;
        }
        switch (type) {
          case BYTE:
            rowBuilder.set(i, parser.getByteValue());
            break;
          case SHORT:
            rowBuilder.set(i, parser.getShortValue());
            break;
          case INTEGER:
            rowBuilder.set(i, parser.getIntValue());
            break;
          case LONG:
            rowBuilder.set(i, parser.getLongValue());
            break;
          case FLOAT:
            rowBuilder.set(i, parser.getFloatValue());
            break;
          case DOUBLE:
            rowBuilder.set(i, parser.getDoubleValue());
            break;
        }
        break;
      case VALUE_TRUE:
        rowBuilder.set(i, true);
        break;
      case VALUE_FALSE:
        rowBuilder.set(i, false);
        break;
      case VALUE_NULL:
        break;
      case VALUE_STRING:
      default:
        if (type == ColumnMetaData.Rep.JAVA_SQL_TIMESTAMP) {
          try {
            final Date parse = UTC_TIMESTAMP_FORMAT.parse(parser.getText());
            rowBuilder.set(i, parse.getTime());
          } catch (ParseException e) {
            // ignore bad value
          }
        } else {
          rowBuilder.set(i, parser.getText());
        }
        break;
    }
  }
 /**
  * Method for copying contents of the current event that the given parser instance points to. Note
  * that the method <b>will not</b> copy any other events, such as events contained within Json
  * Array or Object structures.
  *
  * <p>Calling this method will not advance the given parser, although it may cause parser to
  * internally process more data (if it lazy loads contents of value events, for example)
  */
 @Override
 public 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:
       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:
       throw new IllegalStateException();
   }
 }