use of io.micronaut.serde.Encoder 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.serde.Encoder in project micronaut-serialization by micronaut-projects.
the class PointSerde method serialize.
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Point> type, Point value) throws IOException {
// <5>
Objects.requireNonNull(value, "Point cannot be null");
int[] coords = value.coords();
try (Encoder array = encoder.encodeArray(type)) {
// <6>
array.encodeInt(coords[0]);
array.encodeInt(coords[1]);
}
}
use of io.micronaut.serde.Encoder in project micronaut-serialization by micronaut-projects.
the class ReversePointSerde method serialize.
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Point> type, Point value) throws IOException {
Objects.requireNonNull(value, "Point cannot be null");
int[] coords = value.coords();
Encoder array = encoder.encodeArray(type);
// <3>
array.encodeInt(coords[1]);
array.encodeInt(coords[0]);
array.finishStructure();
}
use of io.micronaut.serde.Encoder in project micronaut-serialization by micronaut-projects.
the class CustomizedMapSerializer method createSpecific.
@Override
public Serializer<Map<K, V>> createSpecific(EncoderContext context, Argument<? extends Map<K, V>> type) throws SerdeException {
final Argument[] generics = type.getTypeParameters();
final boolean hasGenerics = ArrayUtils.isNotEmpty(generics) && generics.length != 2;
if (hasGenerics) {
final Argument<V> valueGeneric = (Argument<V>) generics[1];
final Serializer<V> valSerializer = (Serializer<V>) context.findSerializer(valueGeneric).createSpecific(context, valueGeneric);
return new Serializer<Map<K, V>>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Map<K, V>> type, Map<K, V> value) throws IOException {
final Encoder childEncoder = encoder.encodeObject(type);
for (K k : value.keySet()) {
encodeMapKey(context, childEncoder, k);
final V v = value.get(k);
if (v == null) {
childEncoder.encodeNull();
} else {
valSerializer.serialize(childEncoder, context, valueGeneric, v);
}
}
childEncoder.finishStructure();
}
@Override
public boolean isEmpty(EncoderContext context, Map<K, V> value) {
return CollectionUtils.isEmpty(value);
}
};
} else {
return new Serializer<Map<K, V>>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Map<K, V>> type, Map<K, V> value) throws IOException {
// slow path, lookup each value serializer
final Encoder childEncoder = encoder.encodeObject(type);
for (Map.Entry<K, V> entry : value.entrySet()) {
encodeMapKey(context, childEncoder, entry.getKey());
final V v = entry.getValue();
if (v == null) {
childEncoder.encodeNull();
} else {
@SuppressWarnings("unchecked") final Argument<V> valueGeneric = (Argument<V>) Argument.of(v.getClass());
final Serializer<? super V> valSerializer = context.findSerializer(valueGeneric).createSpecific(context, valueGeneric);
valSerializer.serialize(childEncoder, context, valueGeneric, v);
}
}
childEncoder.finishStructure();
}
@Override
public boolean isEmpty(EncoderContext context, Map<K, V> value) {
return CollectionUtils.isEmpty(value);
}
};
}
}
use of io.micronaut.serde.Encoder in project micronaut-serialization by micronaut-projects.
the class ObjectSerializer method createSpecific.
@Override
public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<? extends Object> type) {
if (type.equalsType(Argument.OBJECT_ARGUMENT)) {
// dynamic type resolving
return new RuntimeTypeSerializer(encoderContext);
} else {
SerBean<Object> serBean;
try {
serBean = getSerBean(type, encoderContext).get();
} catch (IntrospectionException e) {
return createRuntimeSerializer(encoderContext, type, e);
}
final AnnotationMetadata annotationMetadata = type.getAnnotationMetadata();
if (serBean.hasJsonValue()) {
return new Serializer<Object>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
serBean.initialize(context);
SerBean.SerProperty<Object, Object> jsonValue = serBean.jsonValue;
final Object v = jsonValue.get(value);
serBean.jsonValue.serializer.serialize(encoder, context, jsonValue.argument, v);
}
@Override
public boolean isEmpty(EncoderContext context, Object value) {
try {
serBean.initialize(context);
} catch (SerdeException e) {
throw new RuntimeException(e);
}
return serBean.jsonValue.serializer.isEmpty(context, serBean.jsonValue.get(value));
}
@Override
public boolean isAbsent(EncoderContext context, Object value) {
try {
serBean.initialize(context);
} catch (SerdeException e) {
throw new RuntimeException(e);
}
return serBean.jsonValue.serializer.isAbsent(context, serBean.jsonValue.get(value));
}
};
} else if (annotationMetadata.isAnnotationPresent(SerdeConfig.SerIgnored.class) || annotationMetadata.isAnnotationPresent(SerdeConfig.META_ANNOTATION_PROPERTY_ORDER) || annotationMetadata.isAnnotationPresent(SerdeConfig.SerIncluded.class)) {
final String[] ignored = annotationMetadata.stringValues(SerdeConfig.SerIgnored.class);
final String[] included = annotationMetadata.stringValues(SerdeConfig.SerIncluded.class);
List<String> order = Arrays.asList(annotationMetadata.stringValues(SerdeConfig.META_ANNOTATION_PROPERTY_ORDER));
final boolean hasIgnored = ArrayUtils.isNotEmpty(ignored);
final boolean hasIncluded = ArrayUtils.isNotEmpty(included);
Set<String> ignoreSet = hasIgnored ? CollectionUtils.setOf(ignored) : null;
Set<String> includedSet = hasIncluded ? CollectionUtils.setOf(included) : null;
if (!order.isEmpty() || hasIgnored || hasIncluded) {
return new CustomizedObjectSerializer<Object>(serBean) {
@Override
protected List<SerBean.SerProperty<Object, Object>> getWriteProperties(SerBean<Object> serBean) {
final List<SerBean.SerProperty<Object, Object>> writeProperties = new ArrayList<>(super.getWriteProperties(serBean));
if (!order.isEmpty()) {
writeProperties.sort(Comparator.comparingInt(o -> order.indexOf(o.name)));
}
if (hasIgnored) {
writeProperties.removeIf(p -> ignoreSet.contains(p.name));
}
if (hasIncluded) {
writeProperties.removeIf(p -> !includedSet.contains(p.name));
}
return writeProperties;
}
};
}
}
Serializer<Object> outer;
if (serBean.simpleBean) {
outer = new SimpleObjectSerializer<>(serBean);
} else {
outer = new CustomizedObjectSerializer<>(serBean);
}
if (serBean.subtyped) {
return new RuntimeTypeSerializer(encoderContext) {
@Override
protected Serializer<Object> tryToFindSerializer(EncoderContext context, Object value) throws SerdeException {
if (value.getClass().equals(type.getType())) {
return outer;
} else {
return super.tryToFindSerializer(context, value);
}
}
};
} else {
return outer;
}
}
}
Aggregations