com.fasterxml.jackson.core.JsonTokenId Java Examples

The following examples show how to use com.fasterxml.jackson.core.JsonTokenId. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Bean2Deserializer.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Bean2 deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
	Bean2 bean2 = new Bean2();
	p.nextToken();
	do {
		String propName = p.getCurrentName();
		if (propName == null) {
			break;
		}
		if (p.getCurrentTokenId() != JsonTokenId.ID_FIELD_NAME && propName.equals("p1")) {
			bean2.setP1(p.getText());
		} else if (p.getCurrentTokenId() != JsonTokenId.ID_FIELD_NAME && propName.equals("p2")) {
			bean2.setP2(new BigDecimal(p.getText()));
		}
	} while (p.nextToken() != null);
	return bean2;
}
 
Example #2
Source File: DurationDeserializer.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Override
public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch (parser.getCurrentTokenId()) {
        case JsonTokenId.ID_NUMBER_FLOAT:
            BigDecimal value = parser.getDecimalValue();
            long seconds = value.longValue();
            int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds);
            return Duration.ofSeconds(seconds, nanoseconds);

        case JsonTokenId.ID_NUMBER_INT:
            return Duration.ofSeconds(parser.getLongValue());

        case JsonTokenId.ID_STRING:
            String string = parser.getText().trim();
            if (string.length() == 0) {
                return null;
            }
            return Duration.parse(string);
    }
    throw context.mappingException("Expected type float, integer, or string.");
}
 
Example #3
Source File: CustomLocalDateDeserializer.java    From celerio-angular-quickstart with Apache License 2.0 5 votes vote down vote up
public LocalDate deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException {

        if (parser.hasTokenId(JsonTokenId.ID_STRING)) {
            String date = parser.getText().trim();

            if (date.isEmpty()) {
                return null;
            }

            return LocalDateTime.ofInstant(Instant.parse(date), ZoneOffset.UTC).toLocalDate();
        }

        return null;
    }
 
Example #4
Source File: DurationDeserializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    switch (parser.currentTokenId())
    {
        case JsonTokenId.ID_NUMBER_FLOAT:
            BigDecimal value = parser.getDecimalValue();
            return DecimalUtils.extractSecondsAndNanos(value, Duration::ofSeconds);

        case JsonTokenId.ID_NUMBER_INT:
            if(context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) {
                return Duration.ofSeconds(parser.getLongValue());
            }
            return Duration.ofMillis(parser.getLongValue());

        case JsonTokenId.ID_STRING:
            String string = parser.getText().trim();
            if (string.length() == 0) {
                if (!isLenient()) {
                    return _failForNotLenient(parser, context, JsonToken.VALUE_STRING);
                }
                return null;
            }
            try {
                return Duration.parse(string);
            } catch (DateTimeException e) {
                return _handleDateTimeException(context, e, string);
            }
        case JsonTokenId.ID_EMBEDDED_OBJECT:
            // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded
            //    values quite easily
            return (Duration) parser.getEmbeddedObject();
            
        case JsonTokenId.ID_START_ARRAY:
        	return _deserializeFromArray(parser, context);
    }
    return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING,
            JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
}
 
Example #5
Source File: LocalDateTimeDeserializer.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    if (parser.hasTokenId(JsonTokenId.ID_STRING)) {
        String string = parser.getText().trim();
        if (string.length() == 0) {
            return null;
        }
        return LocalDateTime.parse(string, _formatter);
    }

    throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}
 
Example #6
Source File: IonParser.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public int getCurrentTokenId() {
    return currentToken == null
           ? JsonTokenId.ID_NO_TOKEN
           : currentToken.id();
}
 
Example #7
Source File: IonParser.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public int getCurrentTokenId() {
    return currentToken == null
            ? JsonTokenId.ID_NO_TOKEN
            : currentToken.id();
}
 
Example #8
Source File: EntityBeanDeserializer.java    From requery with Apache License 2.0 4 votes vote down vote up
@Override
public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (_nonStandardCreation || _needViewProcesing) {
        return super.deserializeFromObject(p, ctxt);
    }

    Object bean = null;

    if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) {
        String propertyName = p.getCurrentName();
        do {
            p.nextToken();
            SettableBeanProperty property = _beanProperties.find(propertyName);

            if (property == null) {
                handleUnknownVanilla(p, ctxt, bean, propertyName);
                continue;
            }

            // lazily create the bean, the id property must be the first property
            if (bean == null) {
                if (propertyName.equals(_objectIdReader.propertyName.getSimpleName())) {

                    // deserialize id
                    Object id = property.deserialize(p, ctxt);

                    ReadableObjectId objectId = ctxt.findObjectId(id,
                            _objectIdReader.generator, _objectIdReader.resolver);

                    bean = objectId == null ? null : objectId.resolve();
                    if (bean == null) {
                        bean = _valueInstantiator.createUsingDefault(ctxt);
                        property.set(bean, id);
                    }
                } else {
                    bean = _valueInstantiator.createUsingDefault(ctxt);
                }
                p.setCurrentValue(bean);
            }
            property.deserializeAndSet(p, ctxt, bean);

        } while ((propertyName = p.nextFieldName()) != null);
    }

    return bean;
}
 
Example #9
Source File: PushDownJsonHandler.java    From depan with Apache License 2.0 4 votes vote down vote up
public void parseDocument(ElementHandler docHandler)
    throws IOException {

  push(docHandler);
  JsonToken tkn = parser.nextToken();
  while (null != tkn) {
    switch(tkn.id()) {
    case JsonTokenId.ID_START_OBJECT:
      ElementHandler newHandler = top().newObject();
      push(newHandler);
      break;
    case JsonTokenId.ID_END_OBJECT:
      ElementHandler endHandler = top();
      endHandler.endObject();
      pop();
      break;
    case JsonTokenId.ID_FIELD_NAME:
      ElementHandler fieldHandler = top();
      fieldHandler.fieldName(parser.getText());
      break;
    case JsonTokenId.ID_START_ARRAY:
      top().newArray();
      break;
    case JsonTokenId.ID_END_ARRAY:
      top().endArray();
      break;
    case JsonTokenId.ID_STRING:
      top().withString(parser.getText());
      break;
    case JsonTokenId.ID_NUMBER_INT:
      top().withBigInteger(parser.getBigIntegerValue());
      break;
    case JsonTokenId.ID_NUMBER_FLOAT:
      top().withBigDecimal(parser.getDecimalValue());
      break;
    case JsonTokenId.ID_TRUE:
      top().withBoolean(true);
      break;
    case JsonTokenId.ID_FALSE:
      top().withBoolean(false);
      break;
    case JsonTokenId.ID_NULL:
      top().withNull();
      break;
    default:
      PushJsonLogger.LOG.warn(fmtToken(tkn, parser.getText()));
      break;
    }

    // Keep going
    tkn = parser.nextToken();
  }
}
 
Example #10
Source File: CustomLocalDateTimeDeserializer.java    From celerio-angular-quickstart with Apache License 2.0 3 votes vote down vote up
public LocalDateTime deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException {

        if (parser.hasTokenId(JsonTokenId.ID_STRING)) {
            String date = parser.getText().trim();

            if (date.isEmpty()) {
                return null;
            }

            return LocalDateTime.ofInstant(Instant.parse(date), ZoneOffset.UTC);

        }

        return null;
    }