杰克逊YAML:支持锚和参考 [英] Jackson YAML: support for anchors and references

查看:207
本文介绍了杰克逊YAML:支持锚和参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究将YAML用于某种复杂的元数据语言。如果我们可以使用 YAML的锚点和参考文献,那将有助于使文档更小,更简单。我写了一些测试代码,似乎表明Jackson的YAML实现不支持此功能(和/或没有表现出SnakeYAML对此功能的支持)。

I'm investigating the use of YAML for a somewhat complicated metadata language. It would help to make the documents smaller and less complex if we could use YAML's anchors and references. I've written some test code that seems to show that Jackson's YAML implementation doesn't support this feature (and/or doesn't surface SnakeYAML's support for this feature).

这是我的测试YAML文件:

Here is my test YAML file:

set_one:
  bass: tama rockstar 22x16
  snare: &ludwig ludwig supralight 6.5x15
  tom1: tama rockstar 12x11
  tom2: tama rockstar 16x16

set_two:
  snare: *ludwig

我正在解析这个文件:

    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    FileInputStream fis = null;

    try
    {
        fis = new FileInputStream(file);
        JsonNode nodeTree = mapper.readTree(fis);
        examineObject(nodeTree, 0);
    }
    ...

这是我的examineObject的结果( )方法(你可以猜测它的作用):

Here is the out from my "examineObject()" method (you can probably guess what it does):

key = "set_one", type = OBJECT
  key = "bass", type = STRING, value = "tama rockstar 22x16"
  key = "snare", type = STRING, value = "ludwig supralight 6.5x15"
  key = "tom1", type = STRING, value = "tama rockstar 12x11"
  key = "tom2", type = STRING, value = "tama rockstar 16x16"
key = "set_two", type = OBJECT
  key = "snare", type = STRING, value = "ludwig"

显然有些东西足以省略锚点来自set_one.snare的值但是,在调试器中,我无法在JsonNode中的任何位置找到该元素的值。真正的问题是set_two.snare的价值只是路德维希。引用符号('*')已被剥离,但值是引用的值而不是它引用的元素。

Clearly something knew enough to omit the anchor value from "set_one.snare" but, in the debugger, I can't find that value anywhere in the JsonNode for this element. The real problem is that the value of "set_two.snare" is just "ludwig". The reference symbol ('*') has been stripped, but the value is that of the reference and not the element it is referring to.

我正在使用Jackson版本2.8.3和SnakeYaml版本1.17。我只能使用杰克逊,因为这只是一个更大的项目的一部分,该项目已经使用杰克逊作为JSON。

I'm using Jackson version 2.8.3 and SnakeYaml version 1.17. I am constrained to using Jackson as this is only part of a much bigger project which already uses Jackson for JSON.

如果杰克逊可以自动解析引用,我真正想要的是并复制引用的值。在我的例子中,这意味着set_two.snare的值将是ludwig supralight 6.5x15。

What I would really like is if Jackson could automatically resolve references and make a copy of the referenced value. In my example this would mean that the value of "set_two.snare" would be "ludwig supralight 6.5x15".

如果我不能得到我的第一选择那么我我希望Jackson保留锚点和引用,以便我可以手动后处理节点树并自己解析引用。例如,当我看到set_two.snare的值是* ludwig时,我可以在树中搜索一个锚点为& ludwig的节点,然后复制该节点。

If I can't get my first choice then I would like Jackson to preserve both the anchors and the references so that I could manually post-process the node tree and resolve the references myself. For example, when I saw that the value of "set_two.snare" was "*ludwig", I could search the tree for a node with an anchor of "&ludwig" and make a copy of that node.

如果有答案,我觉得它可能会以某种方式涉及com.fasterxml.jackson.dataformat.yaml.YAMLParser.Feature类。遗憾的是,我找不到任何有关这些功能的文档(如果存在),这些功能将启用我正在寻找的行为。

If there is an answer, I have the feeling that it will probably involve the "com.fasterxml.jackson.dataformat.yaml.YAMLParser.Feature" class somehow. Unfortunately I can't find any documentation on the features (if they exist) that will enable the behavior I am looking for.

推荐答案

我只是试图让杰克逊的锚点/别名工作......而且失败了。
你可以看到这里没有实现基于别名的id支持。 _currentAnchor实例变量由getObjectId()设置和公开,但我没有找到一种实用的方法来挂钩Jackson以使用该方法。这不是我与Jacksons Object-Id解析架构的第一次战斗。建议不要花太多时间。

I just tried to make anchors/aliases work in Jackson... and failed. You can see here that alias based id support is not implemented. The _currentAnchor instance variable is set and exposed by getObjectId(), but I didn't find a practical way to hook into Jackson to use that method. This is not my first fight with Jacksons Object-Id resolution architecture. Be advised not the spend too much time with it.

我的解决方案是直接使用snakeyaml库。

My solution was using the snakeyaml library directly.

这篇关于杰克逊YAML:支持锚和参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆