Esempio n. 1
0
 @Override
 public DateMidnight deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // We'll accept either long (timestamp) or array:
   if (jp.isExpectedStartArrayToken()) {
     jp.nextToken(); // VALUE_NUMBER_INT
     int year = jp.getIntValue();
     jp.nextToken(); // VALUE_NUMBER_INT
     int month = jp.getIntValue();
     jp.nextToken(); // VALUE_NUMBER_INT
     int day = jp.getIntValue();
     if (jp.nextToken() != JsonToken.END_ARRAY) {
       throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after DateMidnight ints");
     }
     return new DateMidnight(year, month, day);
   }
   switch (jp.getCurrentToken()) {
     case VALUE_NUMBER_INT:
       return new DateMidnight(jp.getLongValue());
     case VALUE_STRING:
       DateTime local = parseLocal(jp);
       if (local == null) {
         return null;
       }
       return local.toDateMidnight();
   }
   throw ctxt.wrongTokenException(
       jp, JsonToken.START_ARRAY, "expected JSON Array, Number or String");
 }
  /** This is the trickiest thing to handle, since property we are looking for may be anywhere... */
  @Override
  public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext ctxt)
      throws IOException, JsonProcessingException {
    // but first, sanity check to ensure we have START_OBJECT or FIELD_NAME
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.START_OBJECT) {
      t = jp.nextToken();
    } else if (t != JsonToken.FIELD_NAME) {
      throw ctxt.wrongTokenException(
          jp,
          JsonToken.START_OBJECT,
          "need JSON Object to contain As.PROPERTY type information (for class "
              + baseTypeName()
              + ")");
    }
    // Ok, let's try to find the property. But first, need token buffer...
    TokenBuffer tb = null;

    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
      String name = jp.getCurrentName();
      jp.nextToken(); // to point to the value
      if (_propertyName.equals(name)) { // gotcha!
        JsonDeserializer<Object> deser = _findDeserializer(ctxt, jp.getText());
        // deserializer should take care of closing END_OBJECT as well
        if (tb != null) {
          jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
        }
        /* Must point to the next value; tb had no current, jp
         * pointed to VALUE_STRING:
         */
        jp.nextToken(); // to skip past String value
        // deserializer should take care of closing END_OBJECT as well
        return deser.deserialize(jp, ctxt);
      }
      if (tb == null) {
        tb = new TokenBuffer(null);
      }
      tb.writeFieldName(name);
      tb.copyCurrentStructure(jp);
    }
    // Error if we get here...
    throw ctxt.wrongTokenException(
        jp,
        JsonToken.FIELD_NAME,
        "missing property '"
            + _propertyName
            + "' that is to contain type id  (for class "
            + baseTypeName()
            + ")");
  }
Esempio n. 3
0
    @Override
    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
      // We'll accept either long (timestamp) or array:
      if (jp.isExpectedStartArrayToken()) {
        jp.nextToken(); // VALUE_NUMBER_INT
        int year = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int month = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int day = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int hour = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int minute = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int second = jp.getIntValue();
        // let's leave milliseconds optional?
        int millisecond = 0;
        if (jp.nextToken() != JsonToken.END_ARRAY) { // VALUE_NUMBER_INT
          millisecond = jp.getIntValue();
          jp.nextToken(); // END_ARRAY?
        }
        if (jp.getCurrentToken() != JsonToken.END_ARRAY) {
          throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after LocalDateTime ints");
        }
        return new LocalDateTime(year, month, day, hour, minute, second, millisecond);
      }

      switch (jp.getCurrentToken()) {
        case VALUE_NUMBER_INT:
          return new LocalDateTime(jp.getLongValue());
        case VALUE_STRING:
          DateTime local = parseLocal(jp);
          if (local == null) {
            return null;
          }
          return local.toLocalDateTime();
      }
      throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Array or Number");
    }
Esempio n. 4
0
 @Override
 public ReadablePeriod deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // TODO: perhaps support array of numbers...
   // if (jp.isExpectedStartArrayToken()) { ]
   switch (jp.getCurrentToken()) {
     case VALUE_NUMBER_INT: // assume it's millisecond count
       return new Period(jp.getLongValue());
     case VALUE_STRING:
       return new Period(jp.getText());
   }
   throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Number or String");
 }