use of io.micronaut.serde.exceptions.SerdeException 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.exceptions.SerdeException 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;
}
}
}
use of io.micronaut.serde.exceptions.SerdeException in project micronaut-serialization by micronaut-projects.
the class OptionalSerializer method createSpecific.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Serializer<Optional<T>> createSpecific(EncoderContext encoderContext, Argument<? extends Optional<T>> type) throws SerdeException {
final Argument[] generics = type.getTypeParameters();
if (ArrayUtils.isEmpty(generics)) {
throw new SerdeException("Serializing raw optionals is for type: " + type);
}
final Argument<?>[] generics1 = type.getTypeParameters();
if (ArrayUtils.isEmpty(generics1)) {
throw new SerdeException("Serializing raw optionals is not supported for type: " + type);
}
// noinspection unchecked
final Argument<T> generic = (Argument<T>) generics1[0];
final Serializer<? super T> componentSerializer = encoderContext.findSerializer(generic).createSpecific(encoderContext, generic);
return new Serializer<Optional<T>>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Optional<T>> type, Optional<T> value) throws IOException {
final Argument<?>[] generics1 = type.getTypeParameters();
if (ArrayUtils.isEmpty(generics1)) {
throw new SerdeException("Serializing raw optionals is not supported for type: " + type);
}
// noinspection unchecked
final Argument<T> generic = (Argument<T>) generics1[0];
final T o = value.orElse(null);
if (o != null) {
componentSerializer.serialize(encoder, context, generic, o);
} else {
encoder.encodeNull();
}
}
@Override
public boolean isEmpty(EncoderContext context, Optional<T> value) {
Optional o = value;
if (value != null && o.isPresent()) {
return componentSerializer.isEmpty(context, (T) o.get());
}
return true;
}
@Override
public boolean isAbsent(EncoderContext context, Optional<T> value) {
return value == null || !value.isPresent();
}
};
}
use of io.micronaut.serde.exceptions.SerdeException in project micronaut-serialization by micronaut-projects.
the class OptionalValuesSerializer method createSpecific.
@Override
public Serializer<OptionalValues<V>> createSpecific(EncoderContext context, Argument<? extends OptionalValues<V>> type) throws SerdeException {
Argument<?>[] generics = type.getTypeParameters();
if (ArrayUtils.isEmpty(generics)) {
throw new SerdeException("Cannot serialize raw OptionalValues");
}
Argument<V> generic = (Argument<V>) generics[0];
Serializer<V> valueSerializer = (Serializer<V>) context.findSerializer(generic).createSpecific(context, generic);
return new Serializer<OptionalValues<V>>() {
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends OptionalValues<V>> type, OptionalValues<V> value) throws IOException {
Objects.requireNonNull(value, "Value cannot be null");
Encoder objectEncoder = encoder.encodeObject(type);
for (CharSequence key : value) {
Optional<V> opt = value.get(key);
if (opt.isPresent()) {
objectEncoder.encodeKey(key.toString());
valueSerializer.serialize(encoder, context, generic, opt.get());
}
}
objectEncoder.finishStructure();
}
};
}
use of io.micronaut.serde.exceptions.SerdeException in project micronaut-serialization by micronaut-projects.
the class SimpleObjectSerializer method serialize.
@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends T> type, T value) throws IOException {
try {
serBean.initialize(context);
Encoder childEncoder = encoder.encodeObject(type);
for (SerBean.SerProperty<Object, Object> property : serBean.writeProperties) {
childEncoder.encodeKey(property.name);
Object v = property.get(value);
if (v == null) {
childEncoder.encodeNull();
} else {
property.serializer.serialize(childEncoder, context, property.argument, v);
}
}
childEncoder.finishStructure();
} catch (StackOverflowError e) {
throw new SerdeException("Infinite recursion serializing type: " + type.getType().getSimpleName() + " at path " + encoder.currentPath(), e);
} catch (IntrospectionException e) {
throw new SerdeException("Error serializing value at path: " + encoder.currentPath() + ". No serializer found for " + "type: " + type, e);
}
}
Aggregations