Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to allow customization of kotlin serializer in AbstractKotlinSerializationHttpMessageConverter #29761

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private KSerializer<Object> serializer(Type type) {
KSerializer<Object> serializer = serializerCache.get(type);
if (serializer == null) {
try {
serializer = SerializersKt.serializerOrNull(type);
serializer = serializerInternal(type);
}
catch (IllegalArgumentException ignored) {
}
Expand All @@ -166,6 +166,12 @@ private KSerializer<Object> serializer(Type type) {
return serializer;
}

/**
* An abstract method that returns a KSerializer over the given type.
*/
@Nullable
protected abstract KSerializer<Object> serializerInternal(Type type) throws IllegalArgumentException;

private boolean hasPolymorphism(SerialDescriptor descriptor, Set<String> alreadyProcessed) {
alreadyProcessed.add(descriptor.getSerialName());
if (descriptor.getKind().equals(PolymorphicKind.OPEN.INSTANCE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package org.springframework.http.converter;

import java.io.IOException;
import java.lang.reflect.Type;

import kotlinx.serialization.BinaryFormat;
import kotlinx.serialization.KSerializer;
import kotlinx.serialization.SerializationException;

import kotlinx.serialization.SerializersKt;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -76,4 +78,9 @@ protected void writeInternal(Object object, KSerializer<Object> serializer, T fo
throw new HttpMessageNotWritableException("Could not write " + format + ": " + ex.getMessage(), ex);
}
}

@Override
protected KSerializer<Object> serializerInternal(Type type) throws IllegalArgumentException {
return SerializersKt.serializerOrNull(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package org.springframework.http.converter.json;

import kotlinx.serialization.KSerializer;
import kotlinx.serialization.SerializersKt;
import kotlinx.serialization.json.Json;

import org.springframework.http.MediaType;
import org.springframework.http.converter.KotlinSerializationStringHttpMessageConverter;

import java.lang.reflect.Type;

/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
* that can read and write JSON using
Expand Down Expand Up @@ -53,4 +57,9 @@ public KotlinSerializationJsonHttpMessageConverter() {
public KotlinSerializationJsonHttpMessageConverter(Json json) {
super(json, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}

@Override
protected KSerializer<Object> serializerInternal(Type type) throws IllegalArgumentException {
return SerializersKt.serializerOrNull(type);
}
}