0

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.
Filip Zedek
  • 147
  • 2
  • 7
  • Generally speaking, no. You really need to convert the entire deep map to a full concrete type. – Louis Wasserman Mar 02 '22 at 23:22
  • How big is this JSON? If it is not very big and the data structure doesn't change often, then you can just re-create the data structure and use one of serialization frameworks that provide object mapping (e.g.: kotlinx-serialization, jackson, gson). If the data structure is big and/or it changes, I believe there are some tools that generate typed data structures from JSON. Unfortunately, I can't suggest any specific one. – broot Mar 03 '22 at 00:28
  • There are some suggestions here: https://stackoverflow.com/questions/1957406/generate-java-class-from-json – broot Mar 03 '22 at 00:30
  • The JSON itself is not too big. It could have about 30 props, including nested, and it has some multi-level nested maps/lists inside. The problem is I need to process thousands of them for the request, so each millisecond in processing a single item counts. Generating a class from JSON is not what I need, since I know the type in advance and as I said, instantiating objects is slow. And when working with Map I need to cast, which is ugly and also takes some time, because it's not implicit. – Filip Zedek Mar 03 '22 at 13:47
  • So honestly, I'm not sure what do you need ;-) Deserializing to classes shouldn't be slower than deserializing to map. I think it would be even faster. If for some reason you have to keep it as a map then you can create classes that wrap this map. You can make it easier by using property delegation to map: https://kotlinlang.org/docs/delegated-properties.html#storing-properties-in-a-map – broot Mar 03 '22 at 20:52
  • Unfortunately, it's not faster. It took 327 millis converting 5k items to objects vs 232 to map (Jackson object mapper). The closest I could get in optimization is using property delegation, but here I need to cast when working with arrays. – Filip Zedek Mar 12 '22 at 17:57

0 Answers0