YML语法错误

时间:2017-04-30 21:23:52

标签: ruby yaml

我希望在yam文件中有一个多行的markdown java。我尝试了很多东西,但我想我不太明白Yaml的引用规则。

{
  title: Museum,
  body: |
      "```java
      code code code
      java2",
  answers: [
    "`museum`",
    "`museum.getFloor(3)`",
    "`museum.getFloor(3).getExhibit(5)`",
    "`museum.getFloor(3).getExhibit(5).getCurator()`",
    "`museum.getFloor(3).getExhibit(5).getCurator().name`",
    "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"
  ]
}

制作:

/Users/pitosalas/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych.rb:377:in `parse': (generator/test.yml): found character that cannot start any token while scanning for the next token at line 3 column 9 (Psych::SyntaxError)

2 个答案:

答案 0 :(得分:1)

YAML有两种风格:JSON如flow style和更好的人类可读block style

粗略地说,你可以拥有嵌套结构,每个样式都嵌套在自身内,并且可以在嵌套样式中嵌套流式样,但是b lock style nested within flow style is not allowed

您的级别{}是流式,但您尝试使用|在该流式样式中引入literal block style标量。从该标量向上替换块样式的流样式:

title: Museum
body: |
      "```java
      code code code
      java2"
answers: [
    "`museum`",
    "`museum.getFloor(3)`",
    "`museum.getFloor(3).getExhibit(5)`",
    "`museum.getFloor(3).getExhibit(5).getCurator()`",
    "`museum.getFloor(3).getExhibit(5).getCurator().name`",
    "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"
]

你的YAML很好。请注意,加载时不会剥离密钥body的“左右”的双引号,这可能不是您想要的。

你应该IMO不要在(流式)序列中的最后一个值,之后省略尾随answers。当您扩展列表并忘记在上面的行中放入尾随逗号时,这肯定会导致错误。

我个人会一直采用块式:

title: Museum
body: |
      "```java
      code code code
      java2"
answers: 
- "`museum`"
- "`museum.getFloor(3)`"
- "`museum.getFloor(3).getExhibit(5)`"
- "`museum.getFloor(3).getExhibit(5).getCurator()`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"

答案 1 :(得分:1)

当处理复杂或复杂的YAML文件生成时,或者当它无法正常工作时,我会让Ruby向我展示道路:

require 'yaml'

body = <<EOT
"```java
code code code
java2
"
EOT

answers = %w(
  `museum`
  `museum.getFloor(3)`
  `museum.getFloor(3).getExhibit(5)`
  `museum.getFloor(3).getExhibit(5).getCurator()`
  `museum.getFloor(3).getExhibit(5).getCurator().name`
  `museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`
)

obj = {
  "title" => "Museum",
  "body" => body,
  "answers" => answers
}

puts obj.to_yaml

在这种情况下,输出:

---
title: Museum
body: |
  "```java
  code code code
  java2
  "
answers:
- "`museum`"
- "`museum.getFloor(3)`"
- "`museum.getFloor(3).getExhibit(5)`"
- "`museum.getFloor(3).getExhibit(5).getCurator()`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"

如果您然后将该YAML传递回解析器,则应该返回原始数据结构:

YAML.load(obj.to_yaml)
# => {"title"=>"Museum",
#     "body"=>"\"```java\n" +
#    "code code code\n" +
#    "java2\n" +
#    "\"\n",
#     "answers"=>
#      ["`museum`",
#       "`museum.getFloor(3)`",
#       "`museum.getFloor(3).getExhibit(5)`",
#       "`museum.getFloor(3).getExhibit(5).getCurator()`",
#       "`museum.getFloor(3).getExhibit(5).getCurator().name`",
#       "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"]}