【问题标题】:Is this a valid method of using YAML?这是使用 YAML 的有效方法吗?
【发布时间】:2012-01-06 00:09:03
【问题描述】:

我一般是snakeyaml 和yaml 的新手。我需要它来为MUD 存储有关“房间”的信息。

房间的条目将如下所示:

room:
  id: 12
  entry: "Long string"
  description: "Longer more precise string"
  objects:
    ids: 1,23

object:
  id: 1
  name: "chest"
  description: "looks pretty damn old"
  on-text: "the chest has been opened!"
  off-text: "the chest has been closed!"

基本上,每个房间都有一个id 和一些文本,当玩家进入/搜索它时会显示给他们。它还有一个“对象”数组,它们本身在同一个 yaml 文件中声明。

是否可以在我的 yaml 文件中进行此配置?另外,我需要将每个房间和每个对象提取到数组中,所以它看起来像这样:

[12, "long string", "Longer more precise string", [1, "chest", "looks pretty damn old", "the chest has been opened!", "the chest has been closed!"], [ ... item 23 ... ]]

此配置使我可以轻松地解析文件并通过创建一个循环并通过数组位置引用每个值来创建 GenericRoom 和 GenericObject 类。这是 SnakeYAML 可以为我做的事情吗?我一直在玩一些例子,但我对实际 YAML 缺乏了解,这让我很难获得好的结果。

【问题讨论】:

    标签: arrays parsing yaml snakeyaml mud


    【解决方案1】:

    你必须自己将对象连接到房间:

    room:
      id: 12
      entry: "Long string"
      objects: [1, 23]
    
    objects:
      - { id: 1, text: bla bla }
      - { id: 2, text: bla bla 2 }
      - { id: 23, text: bla bla 23}
    

    或 SnakeYAML 可以从锚点和别名中受益: (必须在使用别名之前定义锚点)

    objects:
      - &id001 {id: 1, text: bla bla }
      - &id002 {id: 2, text: bla bla 2 }
      - &id023 {id: 23, text: bla bla 23 }
    
    room:
      id: 12
      entry: "Long string"
      objects: [ *id001, *id023]
    

    (您可以在这里查看您的文件:http://www.yaml.org/spec/1.2/spec.html#id2765878

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多