@Override
 public String deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // 22-Sep-2012, tatu: For 2.1, use this new method, may force coercion:
   String text = jp.getValueAsString();
   if (text != null) {
     return text;
   }
   // [JACKSON-330]: need to gracefully handle byte[] data, as base64
   JsonToken curr = jp.getCurrentToken();
   if (curr == JsonToken.VALUE_EMBEDDED_OBJECT) {
     Object ob = jp.getEmbeddedObject();
     if (ob == null) {
       return null;
     }
     if (ob instanceof byte[]) {
       return Base64Variants.getDefaultVariant().encode((byte[]) ob, false);
     }
     // otherwise, try conversion using toString()...
     return ob.toString();
   }
   throw ctxt.mappingException(_valueClass, curr);
 }
Beispiel #2
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;
    }