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);
}
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;
}
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);
}
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();
}
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);
}
Aggregations