use of io.micronaut.data.model.runtime.convert.AttributeConverter in project micronaut-data by micronaut-projects.
the class DataEncoderContext method findCustomSerializer.
@Override
public <T, D extends Serializer<? extends T>> D findCustomSerializer(Class<? extends D> serializerClass) throws SerdeException {
if (serializerClass == IdSerializer.class) {
IdSerializer idSerializer = new IdSerializer() {
@Override
public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<?> type) throws SerdeException {
boolean isGeneratedObjectIdAsString = type.isAssignableFrom(String.class) && type.isAnnotationPresent(GeneratedValue.class);
if (isGeneratedObjectIdAsString) {
Serializer<? super ObjectId> objectIdSerializer = findSerializer(OBJECT_ID);
return (encoder, encoderContext2, stringType, value) -> {
String stringId = (String) value;
objectIdSerializer.serialize(encoder, encoderContext2, OBJECT_ID, new ObjectId(stringId));
};
}
return (Serializer<Object>) findSerializer(type);
}
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) {
throw new IllegalStateException("Create specific call is required!");
}
};
return (D) idSerializer;
}
if (serializerClass == CustomConverterSerializer.class) {
CustomConverterSerializer customConverterSerializer = new CustomConverterSerializer() {
@Override
public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<?> type) throws SerdeException {
Class<?> converterClass = type.getAnnotationMetadata().classValue(MappedProperty.class, "converter").orElseThrow(IllegalStateException::new);
Class<Object> converterPersistedType = type.getAnnotationMetadata().classValue(MappedProperty.class, "converterPersistedType").orElseThrow(IllegalStateException::new);
Argument<Object> convertedType = Argument.of(converterPersistedType);
Serializer<? super Object> serializer = findSerializer(convertedType);
AttributeConverter<Object, Object> converter = attributeConverterRegistry.getConverter(converterClass);
return new Serializer<Object>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
if (value == null) {
encoder.encodeNull();
return;
}
Object converted = converter.convertToPersistedValue(value, ConversionContext.of(type));
if (converted == null) {
encoder.encodeNull();
return;
}
serializer.serialize(encoder, context, convertedType, converted);
}
};
}
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) {
throw new IllegalStateException("Create specific call is required!");
}
};
return (D) customConverterSerializer;
}
return parent.findCustomSerializer(serializerClass);
}
use of io.micronaut.data.model.runtime.convert.AttributeConverter in project micronaut-data by micronaut-projects.
the class DataDecoderContext method findCustomDeserializer.
@Override
public <T, D extends Deserializer<? extends T>> D findCustomDeserializer(Class<? extends D> deserializerClass) throws SerdeException {
if (deserializerClass == OneRelationDeserializer.class) {
OneRelationDeserializer oneRelationDeserializer = new OneRelationDeserializer() {
@Override
public Deserializer<Object> createSpecific(DecoderContext decoderContext, Argument<? super Object> type) throws SerdeException {
Deserializer<?> relationDeser = findDeserializer(type);
return new Deserializer<Object>() {
@Override
public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
if (decoder.decodeNull()) {
return null;
}
CodecBsonDecoder<BsonDocument> codecBsonDecoder = new CodecBsonDecoder<>(new BsonDocumentCodec(codecRegistry));
BsonDocument document = codecBsonDecoder.deserialize(decoder, decoderContext, type);
if (document == null || document.size() <= 1) {
return null;
}
return relationDeser.deserialize(new BsonReaderDecoder(document.asBsonReader()), decoderContext, type);
}
};
}
@Override
public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
throw new IllegalStateException("Create specific call is required!");
}
};
return (D) oneRelationDeserializer;
}
if (deserializerClass == IdDeserializer.class) {
IdDeserializer idDeserializer = new IdDeserializer() {
@Override
public Deserializer<Object> createSpecific(DecoderContext decoderContext, Argument<? super Object> type) throws SerdeException {
if (type.getType().isAssignableFrom(String.class) && type.isAnnotationPresent(GeneratedValue.class)) {
Deserializer<? extends ObjectId> deserializer = findDeserializer(OBJECT_ID);
return (decoder, decoderContext2, objectIdType) -> {
ObjectId objectId = deserializer.deserialize(decoder, decoderContext2, OBJECT_ID);
return objectId == null ? null : objectId.toHexString();
};
}
return (Deserializer<Object>) findDeserializer(type);
}
@Override
public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
throw new IllegalStateException("Create specific call is required!");
}
};
return (D) idDeserializer;
}
if (deserializerClass == CustomConverterDeserializer.class) {
CustomConverterDeserializer customConverterDeserializer = new CustomConverterDeserializer() {
@Override
public Deserializer<Object> createSpecific(DecoderContext decoderContext, Argument<? super Object> type) throws SerdeException {
Class<?> converterClass = type.getAnnotationMetadata().classValue(MappedProperty.class, "converter").orElseThrow(IllegalStateException::new);
Class<Object> converterPersistedType = type.getAnnotationMetadata().classValue(MappedProperty.class, "converterPersistedType").orElseThrow(IllegalStateException::new);
Argument<Object> convertedType = Argument.of(converterPersistedType);
AttributeConverter<Object, Object> converter = attributeConverterRegistry.getConverter(converterClass);
Deserializer<?> deserializer = findDeserializer(convertedType);
return new Deserializer<Object>() {
@Override
public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
if (decoder.decodeNull()) {
return null;
}
Object deserialized = deserializer.deserialize(decoder, decoderContext, convertedType);
return converter.convertToEntityValue(deserialized, ConversionContext.of(convertedType));
}
};
}
@Override
public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
throw new IllegalStateException("Create specific call is required!");
}
};
return (D) customConverterDeserializer;
}
return parent.findCustomDeserializer(deserializerClass);
}
Aggregations