Search in sources :

Example 1 with InvalidFormatException

use of io.micronaut.serde.exceptions.InvalidFormatException in project micronaut-serialization by micronaut-projects.

the class SimpleObjectDeserializer method readProperties.

private void readProperties(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> beanType, Object obj) throws IOException {
    Decoder objectDecoder = decoder.decodeObject(beanType);
    if (deserBean.readProperties == null) {
        skipUnknownProperties(objectDecoder, beanType);
    } else {
        PropertiesBag<? super Object>.Consumer readProperties = deserBean.readProperties.newConsumer();
        while (true) {
            final String prop = objectDecoder.decodeKey();
            if (prop == null) {
                break;
            }
            @SuppressWarnings("unchecked") final DeserBean.DerProperty<Object, Object> consumedProperty = (DeserBean.DerProperty<Object, Object>) readProperties.consume(prop);
            if (consumedProperty != null) {
                boolean isNull = objectDecoder.decodeNull();
                if (isNull) {
                    if (consumedProperty.nullable) {
                        consumedProperty.set(obj, null);
                    } else {
                        consumedProperty.setDefault(decoderContext, obj);
                    }
                } else {
                    Object val;
                    try {
                        val = consumedProperty.deserializer.deserialize(objectDecoder, decoderContext, consumedProperty.argument);
                    } catch (InvalidFormatException e) {
                        throw new InvalidPropertyFormatException(e, consumedProperty.argument);
                    }
                    consumedProperty.set(obj, val);
                    if (readProperties.isAllConsumed()) {
                        skipUnknownProperties(objectDecoder, beanType);
                        break;
                    }
                }
            } else {
                skipUnknown(objectDecoder, beanType, prop);
            }
        }
        if (!readProperties.isAllConsumed()) {
            for (DeserBean.DerProperty<? super Object, ?> dp : readProperties.getNotConsumed()) {
                dp.setDefault(decoderContext, obj);
            }
        }
    }
    objectDecoder.finishStructure();
}
Also used : Decoder(io.micronaut.serde.Decoder) InvalidFormatException(io.micronaut.serde.exceptions.InvalidFormatException) InvalidPropertyFormatException(io.micronaut.serde.exceptions.InvalidPropertyFormatException)

Example 2 with InvalidFormatException

use of io.micronaut.serde.exceptions.InvalidFormatException in project micronaut-serialization by micronaut-projects.

the class SpecificObjectDeserializer method deserializeValue.

private Object deserializeValue(DecoderContext decoderContext, Decoder objectDecoder, DeserBean.DerProperty<? super Object, ?> derProperty, Argument<Object> propertyType, Object constructedBean) throws IOException {
    final Object val;
    final boolean hasRef = constructedBean != null && derProperty.managedRef != null;
    try {
        if (hasRef) {
            decoderContext.pushManagedRef(new PropertyReference<>(derProperty.managedRef, derProperty.instrospection, derProperty.argument, constructedBean));
        }
        val = derProperty.deserializer.deserialize(objectDecoder, decoderContext, propertyType);
    } catch (InvalidFormatException e) {
        throw new InvalidPropertyFormatException(e, propertyType);
    } finally {
        if (hasRef) {
            decoderContext.popManagedRef();
        }
    }
    return val;
}
Also used : InvalidPropertyFormatException(io.micronaut.serde.exceptions.InvalidPropertyFormatException) InvalidFormatException(io.micronaut.serde.exceptions.InvalidFormatException)

Example 3 with InvalidFormatException

use of io.micronaut.serde.exceptions.InvalidFormatException in project micronaut-serialization by micronaut-projects.

the class SerdeJsonBeanPropertyBinderExceptionHandler method toConversionError.

@Override
public Optional<ConversionErrorException> toConversionError(Object object, Exception e) {
    if (e instanceof InvalidFormatException) {
        InvalidFormatException ife = (InvalidFormatException) e;
        Argument<?> argument;
        if (ife instanceof InvalidPropertyFormatException) {
            argument = ((InvalidPropertyFormatException) ife).getArgument();
        } else {
            Class<?> type = object != null ? object.getClass() : Object.class;
            argument = Argument.of(type);
        }
        return Optional.of(new ConversionErrorException(argument, ife.toConversionError()));
    }
    return Optional.empty();
}
Also used : InvalidPropertyFormatException(io.micronaut.serde.exceptions.InvalidPropertyFormatException) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) InvalidFormatException(io.micronaut.serde.exceptions.InvalidFormatException)

Aggregations

InvalidFormatException (io.micronaut.serde.exceptions.InvalidFormatException)3 InvalidPropertyFormatException (io.micronaut.serde.exceptions.InvalidPropertyFormatException)3 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)1 Decoder (io.micronaut.serde.Decoder)1