'Working with heterogenous map as it was typed in Kotlin/Java

I read some items from DB as JSON and create a Map<String, Any> from each of them. I want to process the items as Map because instantiating objects using Jackson Object Mapper's .convertValue took a long time (decimals of milliseconds more than with Map, per item). However, the type of the map is not uniform and when I need to for example get some nested prop (e.g. map.child.prop), I need to go like this:

val prop = (parentMap["child"] as Map<String, Any>)["prop"]

I am aware there is Apache Commons providing PropertyUtils.getProperty(map, "map.child.prop")), but it is not exactly what I want. The object is quite complex and I need to work with the whole map as it was typed. Is there such an option? The casting goes crazy as the item is a big map, but currently the only solution I have.

What I tried:

  1. Convert map to class
  • Too long
  1. Using typed map library https://github.com/broo2s/typedmap
  • The map type seems to be built continuously as you add some key/value. But I am not building continuously, I am converting from JSON.
  1. Using Kotlin delegated properties
  • Works well for flat structure, but needs to casting when going deeper.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source