Lev Teytelman
03/23/2023, 4:21 AM@Serializable
data class Tag(
val name: String,
@Serializable(with = TagStringSerializer::class)
val parent: Tag? = null,
val subTags: MutableSet<@Serializable(with = TagStringSerializer::class) Tag> = ConcurrentSet(),
val files: MutableSet<@Serializable(with = SavedFileStringSerializer::class) SavedFile> = ConcurrentSet()
)
@Serializable
data class SavedFile(
@Serializable(with = FileSerializer::class)
val file: File,
val tags: MutableSet<@Serializable(with = TagStringSerializer::class) Tag> = ConcurrentSet()
)
Of course, I don't want recursive Tags/SavedFiles in my serialization, as that would lead to infinite loops; I only serialize the names (all my custom serializers do is extract names).
When I use ApplicationCall#respond(savedFile)
, this works properly and sends {"file":"example","tags":["test"]}
.
However, when I do ApplicationCall#respond(savedFile.tags)
, I receive an array of default-serialized tags instead of just the names, as if it's ignoring the annotation in the set: [{"name":"test","subTags":[],"files":["example"]}]
.
Even stranger, when I do just Json.encodeToString(savedFile.tags)
, it works just fine and gives me a properly-encoded array ["test"]
.
Of course, I could just create a function like respondJson
and wrap a call to respondText(Json.encodeToString(
, but this seems hacky and non-ideal.
I'm just very confused as to why the two functions are behaving differently and how I can fix the serialization done within ApplicationCall#respond
.Aleksei Tirman [JB]
03/23/2023, 10:50 AMLev Teytelman
03/24/2023, 7:21 PM./gradlew run
to run it)
Requesting /r1
returns the properly serialized object, but requesting /r1children
returns the children with default serialization, despite them being in a set with a specific serialization set. Moreover, requesting /r1childrenjson
returns the proper string array and not an array of objects.Lev Teytelman
03/24/2023, 7:22 PMLev Teytelman
03/24/2023, 7:28 PMLev Teytelman
03/24/2023, 8:10 PMJson.encodeToString
work properly? I'm worried that I will build my application around one behavior and suddenly begin to encounter another.Lev Teytelman
03/27/2023, 8:01 PMAleksei Tirman [JB]
03/28/2023, 8:34 AMtypeInfo
method from the reflection package to pass a type info to the ContentNegotiation
plugin, I guess it loses the information about the serializer.Aleksei Tirman [JB]
03/28/2023, 8:36 AM