Is it possible to deserialize unknown keys?
See original GitHub issueI have currently an issue where I need to be able to deserialize JSON that has a defined structure but also allows for arbitary extensions. So I need to be able to work with unknown keys and unknown types. Now I would like to somehow store those unknown keys with their values and later, when needed serialize them into their original form again.
My first thought was to create some map property in which all the unknown keys and their values are put. When serializing that map I probably need to use a JsonTransformingSerializer since I want to keep the original structure and without that the map would be serialized as object.
The problem of how to store unknown keys and their values remain. I searched a lot but I could not find an answer to that. Does there exist a feature that deals with unknown keys or do I need to write a custom deserializer for that and what would an example of that look like dealing with unknown keys? Or do I need to approach this problem in a different way?
At the moment this is the approach I am experimenting with:
My test class:
@Serializable data class testSerializationVC (val knownKey: String, val elements: JsonObject)
My test json:
jsonToParse = """{"knownKey":"knownKey", "knownKey":"knownKey", "elements": {"arbitraryValue1":"json", "arbitraryValue2": 1, "arbitraryValue3": "arbitraryValue3"}}"""
Created Object:
testSerializationVC(knownKey=knownKey, elements={"arbitraryValue1":"json","arbitraryValue2":1,"arbitraryValue3":"arbitraryValue3"})
Output of serialization:
{"knownKey":"knownKey", "elements":{"arbitraryValue1":"json","arbitraryValue2":1,"arbitraryValue3":"arbitraryValue3"}}
My goal is to parse JSON like:
jsonToParse = """{"knownKey":"knownKey", "arbitraryValue1":"json", "arbitraryValue2": 1, "arbitraryValue3": "arbitraryValue3"}"""
And deserilze the object back into:
{"knownKey":"knownKey", "arbitraryValue1":"json", "arbitraryValue2": 1, "arbitraryValue3": "arbitraryValue3"}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Thanks to @pdvrieze suggestions I was able to accomplish the same challenge asked from @MMairinger. Please note that I’m using version 0.20.0. You’ll have to use
JsonDecoder
/JsonEncoder
instead ofJsonInput
/JsonOutput
in order to make it work on version 1.0.0.Example:
By far the simplest approach to do this would be to have a custom deserializer that does the following:
It first delegates to a serializer of a map of string,JsonElement values. For the known keys you then invoke the appropriate deserializers from the jsonElement and store them in some temporary. The unknown keys go into some map of unknown values. You create the instance of the result class and return it.
Note that it requires you delegate the descriptor to be that of a map (with the correct serialkind)
To serialize you do the same thing in reverse, you first serialize all the properties into a map (you serialize to an in-memory structure, not the encoder passed as function parameter). Then you add all the “unknown” values to the same map. Finally you serialize the map (against the encoder).
In short, you do nested serialization into/from an intermediate representation.