Пример #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;
    }