杰克逊YAML:支持标签 [英] Jackson YAML: support for tags

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

问题描述

我正在研究将YAML用于某种较为复杂的元数据语言.一件很有用的事情是,如果YAML解析器支持使用 YAML标签.这将使使用这种元数据语言编写文档的人可以指示何时/是否使用定义良好的架构来定义某个对象.例如:

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

在上述YAML中使用!dset"标记表明,作者认为"set_one"定义的对象应该定义一个鼓组,并且应根据与"dset"相对应的模式进行解析. ".如果对象违反了该模式的约束之一(例如,它未定义踩-),则用户希望在解析时看到该错误,而不希望它导致某些更不透明的运行时错误. /p>

我正在像上面那样解析上面的示例:

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

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

当我在调试器中查看"set_one"的JsonNode时,看不到!dset"标记的任何痕迹.我可以想到Jackson可以做的许多很酷的事情(例如,使用批注将Java类与给定的标签关联,并自动将元素反序列化为该类的实例),但是至少,我需要一些方法来发现该节点被标记为"dset".有谁知道我是否/如何做?

解决方案

JsonNode不能包含对YAML标记的任何引用,因为JSON中没有这样的结构.

请注意,Jackson是JSON,XML,YAML等的高级抽象.您想要做的是特定于YAML的-JSON和XML都没有直接等同于YAML的标签系统(XML也具有 tags ,但是它们是完全不同的).因此,杰克逊是使用此高级YAML功能的错误API.

Jackson中的

YAML支持由 SnakeYAML 提供,它完全能够处理标签.因此,您可能想使用SnakeYAML的API而不是Jackson. 此处是一个示例,其中包括,注册并使用自定义标签.哦,它还可以使用批注将Java类与给定标签相关联,并自动将元素反序列化为该类的实例.

I'm investigating the use of YAML for a somewhat complicated metadata language. One thing that would be of great use is if the YAML parser supported the use of YAML tags. This would allow people who write documents in this metadata language to indicate when/if they are defining some object with a well-defined schema. For example:

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

The use of the "!dset" tag in the above YAML is an indication, by the author, that the object defined by "set_one" is supposed to define a drumset and should be parsed according to the schema corresponding to "dset". If the object violates one of the constraints of that schema (for example, it does not define a hi-hat) the user would like to see the error at parsing time and not have it result in some, more opaque, runtime error.

I'm parsing the above sample like so:

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

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

When I look at the JsonNode for "set_one" in the debugger, I don't see any trace of the "!dset" tag. I can think of a lot of cool things Jackson could do (e.g. use annotations to associate a Java class with a given tag and automatically deserialize the elements into an instance of that class) but, at a minimum, I need some way of discovering that that node was tagged with "dset". Does anyone know if/how I can do this?

解决方案

A JsonNode cannot contain any reference to a YAML tag because there is no such structure in JSON.

Be aware that Jackson is a high-level abstraction over JSON, XML, YAML and the like. What you want to do is YAML-specific - neither JSON nor XML have a direct equivalent of YAML's tag system (XML also has tags, but they are something entirely different). Therefore, Jackson is the wrong API to use this advanced YAML feature.

YAML support in Jackson is provided by SnakeYAML, which is perfectly able to deal with tags. So you probably want to use SnakeYAML's API instead of Jackson. Here is an example which, amongst other things, registers and uses a custom tag. Oh, and it is also able to use annotations to associate a Java class with a given tag and automatically deserialize the elements into an instance of that class.

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

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