使用在 Kotlin/Java 中输入的异构地图
我从数据库中读取一些 JSON 项目并创建一个 Mapmap.child.prop
)时,我需要像这样:
val prop = (parentMap["child"] as Map<String, Any>)["prop"]
我知道有 Apache Commons 提供了 PropertyUtils.getProperty(map, "map.child.prop"))
,但这并不完全是我想要的。该对象非常复杂,我需要在输入时处理整个地图。有这样的选择吗?由于该项目是一张大地图,选角变得很疯狂,但目前这是我唯一的解决方案。
我尝试过的方法:
- 将地图转换为类
- 太长
- 当您添加一些键/值时,地图类型似乎是连续构建的。但我不是持续构建,而是从 JSON 进行转换。
- 使用 Kotlin 委托属性
- 对于平面结构效果很好,但在深入时需要进行转换。
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:
- Convert map to class
- Too long
- 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.
- Using Kotlin delegated properties
- Works well for flat structure, but needs to casting when going deeper.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论