这是有效的 YAML 吗?
因此,对于我在 C# 中的文本解析 问题,我直接针对 YAML 。 我在推荐这个库时遇到了困难,所以这是一个快速的方法。
heading:
name: A name
taco: Yes
age: 32
heading:
name: Another name
taco: No
age: 27
等等。 这有效吗?
So for my text parsing in C# question, I got directed at YAML. I'm hitting a wall with this library I was recommended, so this is a quickie.
heading:
name: A name
taco: Yes
age: 32
heading:
name: Another name
taco: No
age: 27
And so on. Is that valid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
似乎有一个名为 Kwalify 的 YAML 验证器,它应该会给你答案。 你应该直接使用字符串标记化,伙计。 编写解析器很有趣:)
There appears to be a YAML validator called Kwalify which should give you the answer. You shoulda just gone with the String tokenizing, man. Writing parsers is fun :)
另一个用于 .NET 的 YAML 库正在开发中。 现在它支持读取 YAML 流。 它已经在 Windows 和 Mono 上进行了测试。 目前正在实施写入支持。
There is another YAML library for .NET which is under development. Right now it supports reading YAML streams. It has been tested on Windows and Mono. Write support is currently being implemented.
CodeProject 有一个位于:
http://www.codeproject.com/KB/recipes/yamlparser .aspx
我没有尝试太多,但值得一看。
CodeProject has one at:
http://www.codeproject.com/KB/recipes/yamlparser.aspx
I haven't tried it too much, but it's worth a look.
您可以在在线 yaml 解析器中看到输出:
http://yaml-online-parser.appspot.com/?yaml=heading%3A%0D%0A +姓名%3A+A+姓名%0D%0A+炸玉米饼%3A+是%0D%0A+年龄%3A+32%0D%0A%0D%0标题%3A%0D%0A+姓名%3A+另一个+姓名%0D%0A+taco%3A+No%0D%0A+age%3A+27%0D%0A&type=json
可以看到,只创建了一个标题节点。
You can see the output in the online yaml parser :
http://yaml-online-parser.appspot.com/?yaml=heading%3A%0D%0A+name%3A+A+name%0D%0A+taco%3A+Yes%0D%0A+age%3A+32%0D%0A%0D%0Aheading%3A%0D%0A+name%3A+Another+name%0D%0A+taco%3A+No%0D%0A+age%3A+27%0D%0A&type=json
As you can see, there is only one heading node created.
只是对此做出明确的评论:您有重复的映射键问题。 YAML 处理器会将其解析为 !!map,这会禁止重复的键。 不过,并非所有处理器都强制执行此约束,因此如果将不正确的 YAML 流传递给处理器,您可能会得到不正确的结果。
Just to make an explicit comment about it: You have a duplicate mapping key issue. A YAML processor will resolve this as a !!map, which prohibits duplicate keys. Not all processors enforce this constraint, though, so you might get an incorrect result if you pass an incorrect YAML stream to a processor.
尝试这个(在线 YAML 解析器)。
您无需下载任何内容或执行任何操作。 只要去那里,复制并复制即可。 粘贴。 就是这样。
Try this(Online YAML parser).
You don't have to download anything or do something. Just go there, and copy & paste. That's it.
好吧,看来 YAML 已经被淘汰了。 我想要一些人类可写和可读的东西。 另外,这个 C# 实现...我不知道它是否有效,该文档由一些单行代码示例组成。 它对自己的 YAML 文件感到厌烦,是一个老学生项目。 我发现的唯一一个使用 MS-PL 的 C# YAML 解析器,我使用起来不太舒服。
我可能最终会推出自己的格式。 该死的最佳实践,我想做的就是将键与值关联起来。
Well, it appears YAML is gone out the window then. I want something both human writable and readable. Plus, this C# implementation...I have no idea if it's working or not, the documentation consists of a few one line code examples. It barfs on their own YAML files, and is an old student project. The only other C# YAML parser I've found uses the MS-PL which I'm not really comfortable using.
I might just end up rolling my own format. Best practices be damned, all I want to do is associate a key with a value.
部分地。 YAML 支持多个连续“文档”的概念。 如果这就是您在这里尝试执行的操作,那么是的,这是正确的 - 您有两个文档(或文档片段)。 为了使其更明确,您应该用三个破折号将它们分开,如下所示:
另一方面,如果您希望使它们成为同一文档的一部分(以便反序列化它们将产生一个包含两个元素的列表),您应该编写就像下面这样。 对缩进级别要格外小心:
一般来说,YAML 是简洁且人类可读/可编辑的,但不是真正人类可写的,因此您应该始终使用库来生成它。 另外,请注意不同版本的 YAML 之间存在一些重大更改,如果您使用符合不同版本标准的不同语言的库,这些更改可能会影响您。
Partially. YAML supports the notion of multiple consecutive "documents". If this is what you are trying to do here, then yes, it is correct - you have two documents (or document fragments). To make it more explicit, you should separate them with three dashes, like this:
On the other hand if you wish to make them part of the same document (so that deserializing them would result in a list with two elements), you should write it like the following. Take extra care with the indentation level:
In general YAML is concise and human readable / editable, but not really human writable, so you should always use libraries to generate it. Also, take care that there exists some breaking changes between different versions of YAML, which can bite you if you are using libraries in different languages which conform to different versions of the standard.