Search in sources :

Example 1 with Decoder

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

the class JacksonJsonMapper method readValue0.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> T readValue0(JsonParser parser, Argument<?> type) throws IOException {
    parser.setCodec(objectCodecImpl);
    Deserializer deserializer = registry.findDeserializer(type).createSpecific(decoderContext, (Argument) type);
    if (!parser.hasCurrentToken()) {
        parser.nextToken();
    }
    // for jackson compat we need to support deserializing null, but most deserializers don't support it.
    if (parser.currentToken() == JsonToken.VALUE_NULL && !deserializer.allowNull()) {
        return null;
    }
    final Decoder decoder = JacksonDecoder.create(parser, view);
    return (T) deserializer.deserialize(decoder, decoderContext, type);
}
Also used : Deserializer(io.micronaut.serde.Deserializer) UpdatingDeserializer(io.micronaut.serde.UpdatingDeserializer) Decoder(io.micronaut.serde.Decoder)

Example 2 with Decoder

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

the class BsonReaderDecoder method decodeBuffer.

@Override
public Decoder decodeBuffer() throws IOException {
    byte[] documentBytes = decodeCustom(p -> ((BsonReaderDecoder) p).copyValueToDocument());
    BsonReaderDecoder topDecoder = new BsonReaderDecoder(new BsonBinaryReader(ByteBuffer.wrap(documentBytes)));
    Decoder objectDecoder = topDecoder.decodeObject();
    // skip key
    objectDecoder.decodeKey();
    return objectDecoder;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) Decoder(io.micronaut.serde.Decoder) AbstractStreamDecoder(io.micronaut.serde.support.AbstractStreamDecoder)

Example 3 with Decoder

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

the class ReversePointSerde method deserialize.

@Override
public Point deserialize(Decoder decoder, DecoderContext context, Argument<? super Point> type) throws IOException {
    Decoder array = decoder.decodeArray();
    // <2>
    int y = array.decodeInt();
    int x = array.decodeInt();
    array.finishStructure();
    return Point.valueOf(x, y);
}
Also used : Decoder(io.micronaut.serde.Decoder)

Example 4 with Decoder

use of io.micronaut.serde.Decoder 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 5 with Decoder

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

the class CustomizedObjectArrayDeserializer method deserialize.

@Override
public Object[] deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object[]> type) throws IOException {
    if (decoder.decodeNull()) {
        return null;
    }
    final Decoder arrayDecoder = decoder.decodeArray();
    // safe to assume only object[] handled
    Object[] buffer = (Object[]) Array.newInstance(componentType.getType(), 50);
    int index = 0;
    while (arrayDecoder.hasNextArrayValue()) {
        final int l = buffer.length;
        if (l == index) {
            buffer = Arrays.copyOf(buffer, l * 2);
        }
        buffer[index++] = componentDeserializer.deserialize(arrayDecoder, decoderContext, componentType);
    }
    arrayDecoder.finishStructure();
    return Arrays.copyOf(buffer, index);
}
Also used : Decoder(io.micronaut.serde.Decoder)

Aggregations

Decoder (io.micronaut.serde.Decoder)13 Argument (io.micronaut.core.type.Argument)6 Deserializer (io.micronaut.serde.Deserializer)5 SerdeException (io.micronaut.serde.exceptions.SerdeException)5 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)3 PropertyNamingStrategy (io.micronaut.serde.config.naming.PropertyNamingStrategy)3 IOException (java.io.IOException)3 Collection (java.util.Collection)3 BeanContext (io.micronaut.context.BeanContext)2 BeanRegistration (io.micronaut.context.BeanRegistration)2 Secondary (io.micronaut.context.annotation.Secondary)2 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2 Order (io.micronaut.core.annotation.Order)2 OrderUtil (io.micronaut.core.order.OrderUtil)2 ReflectionUtils (io.micronaut.core.reflect.ReflectionUtils)2 ArrayUtils (io.micronaut.core.util.ArrayUtils)2 CollectionUtils (io.micronaut.core.util.CollectionUtils)2 StringUtils (io.micronaut.core.util.StringUtils)2