简体   繁体   English

将 object 图形展平为 map

[英]Flattening an object graph to a map

I'm trying to flatten an object graph completely to a map.我正在尝试将 object 图表完全展平为 map。

Complex objects should also be flattened to the top level using "namespaces".还应使用“名称空间”将复杂对象展平到顶层。 So if the object A contains an int i, a string pid and another object B that contains a string id, the resulting Map would look like {i=1, pid="test", B.id="test1"}.因此,如果 object A 包含一个 int i、一个字符串 pid 和另一个包含字符串 id 的 object B,则生成的 Map 看起来像 {i=1, pid="test", B.id="test1"}。

I also want to be able to reconstruct the original object from a given map.我还希望能够从给定的 map 重建原始的 object。

I've searched around for libraries that do this.我四处寻找执行此操作的库。 But I'm not quite getting what I'm looking for.但我不太明白我要找的东西。 I see stuff that maintains the hierarchy but nothing that completely flattens the structure.我看到了维护层次结构的东西,但没有看到完全扁平化结构的东西。

I do see something in Spring Integration that looks like what I want to do: http://static.springsource.org/spring-integration/api/org/springframework/integration/transformer/ObjectToMapTransformer.html#ObjectToMapTransformer%28%29我确实在 Spring 集成中看到了一些看起来像我想要做的事情: http://static.springsource.org/spring-integration/api/org/springframework/integration/transformer/ObjectToMapTransformer.html#ObjectToMapTransformer%28%29

But I can't get it to work.但我无法让它工作。

Any help would be appreciated.任何帮助,将不胜感激。

Thanks.谢谢。

The Apache BeanUtils library has a describe() method that does something similar to what I was looking for. Apache BeanUtils库有一个describe()方法,可以执行与我想要的类似的操作。

Another possible solution would be via the Jackson JSON library, since JSON objects are essentially key-value pairs. 另一种可能的解决方案是通过Jackson JSON库,因为JSON对象本质上是键值对。

Related discussions: How to convert a Java object (bean) to key-value pairs (and vice versa)? 相关讨论: 如何将Java对象(bean)转换为键值对(反之亦然)?

你考虑过使用Protobufs吗?

The json-flattener library solves exactly your problem json-flattener库正好解决了你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM