I have a complicated problem (as always). I have a...
# serialization
a
I have a complicated problem (as always). I have a large json that could have something like
{ $ref: 2}
instead of some of its nodes. My task is to find those nodes and replace them by objects those refs are referencing to. The problem is that I do not know in advance on which level those refs will appear. It is quite easy to create either custom decoder o custom serializer that checks specific node and replaced it if it is ref, but I can't see a way to propagate this decoder/serializer logic recursively to sub-nodes. Does anybody have a similar expirience? I know that is is possible to do in @pdvrieze's XML processor by applying custom decoder logic to configuration.
t
Looks like custom decoder with memory. Also decoder must be provided by custom serialization module to avoid “old memory” effects
e
possibly an easier hack would be to decode to JsonElement first, munge that, then decode JsonElement to your output type
a
The problem is that I need to base it on Json decoder without rewriting the whole decoder from scratch. I can't just inherit Json decoder.
I tried to decode to JsonElement first as well via KSerializer, but it works on one specific type, not on all typese
Here is the code I tried first. The problem is that it delegates to json and json does not know anything about custom transformation rule inside it.
Probably I can do a custom Json decoder which proxies "real" decoder...
👌 1
a
Solving references (or anchors) in documents looks like a big feature. Is there an issue for that already (found [this](https://github.com/Kotlin/kotlinx.serialization/issues/1363), but not sure it adress the whole concept) ? In OpenAPI, there's similar things, and the Yaml format also provides it. I assume it could be useful in other cases too.
a
I found a rather unorthodox solution: https://github.com/mipt-npm/visionforge/blob/1b2d61008f4d8602d0ca4735fdfed89591702[…]-root-loader/src/commonMain/kotlin/ru/mipt/npm/root/rootJson.kt. The idea is that I put the cache inside the serializers module and wrap all serializers with cached serializers.
@Alexis Manin thanks for the link. I remember seeing this issue, but I forgot about it.