结构化数据的文本友好文件格式

时间:2015-12-01 15:06:54

标签: html json xml yaml

我正在寻找一种文件格式,可以让我对字典和数组等结构化数据进行编码,还可以让我轻松编辑文本块,包括换行符。

到目前为止的候选人:

  • xml :( +)适用于文本编辑和结构化数据,( - )忽略换行符,关闭标签很麻烦
  • html:(+)包含换行符标签,( - )没有结构化数据
  • json:(+)适用​​于结构化数据,( - )不适合编辑多行文字
  • yaml:(+)适用​​于结构化数据,( - )不适合编辑多行文本,如果文本包含冒号等特殊字符 [编辑:请参阅接受的答案,文字作品]

到目前为止我最喜欢的:带有自定义标签的xml用于换行符。更好的想法?

1 个答案:

答案 0 :(得分:1)

YAML是一个完美的契合,如果文本中包含冒号等特殊字符,那么“对于编辑多行文本很糟糕”的“骗局”完全没有根据。 YAML是迄今为止用于多行文本的最具特色的格式:

---
# Block scalars are folded and stripped by default
preamble:
  We the People of the United States, in Order to form a more
  perfect Union, establish Justice, insure domestic Tranquility,
  provide for the common defence, promote the general Welfare,
  and secure the Blessings of Liberty to ourselves and our
  Posterity, do ordain and establish this Constitution for the
  United States of America.

# Chomping indicators (+ and -) allow explicit control over how
# leading/trailing whitespace will be preserved or stripped
chomp: >+


  Hello: Is it me you're looking for?



# Literal style preserves formatting
homepage: |
  <html>
    <head>
      <title>My kewl web site</title>
    </head>
    <body>
      <h1>Hello world!</h1>
    </body>
  </html>

# The indentation indicator lets you explicitly control indentation if it
# can't be inferred
indentation: |4

            I'll be indented eight spaces
          I'll be indented six

# And colons (or other special characters) are not a problem
emoji: |
  : Grinning face {U+1F600}
  : Grimacing face {U+1F62C}
  : Disappointed face {U+1F61E}

...当然,您可以在映射(字典)或序列(数组)中使用这些格式中的任何一种。您甚至可以使用复杂的字符串(或任何YAML结构,as it happens)来映射键。

如果您有一个用例的例子,您认为YAML不适合,请随时发表评论。 YAML并不适合所有事情,但它对很多事情都很好。

为了比较,这里的内容与JSON相同:

{ "preamble": "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.",
  "chomp": "\n\nHello: Is it me you're looking for?\n\n\n\n",
  "homepage": "<html>\n  <head>\n    <title>My kewl web site</title>\n  </head>\n  <body>\n    <h1>Hello world!</h1>\n  </body>\n</html>\n",
  "indentation": "\n        I'll be indented eight spaces\n      I'll be indented six\n",
  "emoji": ": Grinning face {U+1F600}\n: Grimacing face {U+1F62C}\n: Disappointed face {U+1F61E}\n"
}