大多数 YAML 解析器都是为读取 YAML(由其他程序编写或由人类编辑)而构建的,并且用于编写 YAML 以供其他程序读取。众所周知,缺乏的是解析器编写人类仍然可读的 YAML 的能力:
- 映射键的顺序未定义
- 评论被扔掉
- 标量文字块样式(如果有)被删除
- 标量周围的间距被丢弃
- 标量折叠信息(如果有)被丢弃
加载已加载的手工制作的 YAML 文件的转储将导致与初始加载相同的内部数据结构,但中间转储通常看起来不像原始(手工制作的)YAML。
如果你有一个 Python 程序:
import ruamel.yaml as yaml
yaml_str = """\
receipt: Oz-Ware Purchase Invoice
date: 2007-08-06
customer:
given: Dorothy
items:
- part_no: A4786
descrip: Water Bucket (Filled)
- part_no: E1628
descrip: High Heeled "Ruby" Slippers
size: 8
bill-to: &id001
street: |
123 Tornado Alley
Suite 16
city: East Centerville
state: KS
ship-to: *id001
specialDelivery: >
Follow the Yellow Brick
Road to the Emerald City.
"""
data1 = yaml.load(yaml_str, Loader=yaml.Loader)
dump_str = yaml.dump(data1, Dumper=yaml.Dumper)
data2 = yaml.load(dump_str, Loader=yaml.Loader)
那么以下断言成立:
assert data1 == data2
assert dump_str != yaml_str
中间dump_str
看起来像:
bill-to: &id001 {city: East Centerville, state: KS, street: '123 Tornado Alley
Suite 16
'}
customer: {given: Dorothy}
date: 2007-08-06
items:
- {descrip: Water Bucket (Filled), part_no: A4786}
- {descrip: High Heeled "Ruby" Slippers, part_no: E1628, size: 8}
receipt: Oz-Ware Purchase Invoice
ship-to: *id001
specialDelivery: 'Follow the Yellow Brick Road to the Emerald City.
'
以上是ruamel.yaml、PyYAML和许多其他语言的 YAML 解析器和在线 YAML 转换服务的默认行为。对于某些解析器,这是提供的唯一行为。
我启动 ruamel.yaml 作为 PyYAML 的增强的原因是从手工制作的 YAML 到内部数据,再到 YAML,产生更好的人类可读性(我称之为往返),并保留更多信息(特别是评论)。
data = yaml.load(yaml_str, Loader=yaml.RoundTripLoader)
print yaml.dump(data, Dumper=yaml.RoundTripDumper)
给你:
receipt: Oz-Ware Purchase Invoice
date: 2007-08-06
customer:
given: Dorothy
items:
- part_no: A4786
descrip: Water Bucket (Filled)
- part_no: E1628
descrip: High Heeled "Ruby" Slippers
size: 8
bill-to: &id001
street: |
123 Tornado Alley
Suite 16
city: East Centerville
state: KS
ship-to: *id001
specialDelivery: 'Follow the Yellow Brick Road to the Emerald City.
'
我的重点是注释、键、顺序和文字块样式。标量和折叠标量周围的间距(还)不是特别的。
从那里开始(您也可以在 PyYAML 中执行此操作,但您不会拥有 ruamel.yaml 键顺序保持的内置增强功能)您可以提供特殊的发射器,或者在较低级别挂钩到系统,覆盖某些方法in emitter.py
(并确保您可以为不需要处理的情况调用原件:
def rewrite_write_plain(self, text, split=True):
if self.state == self.expect_block_mapping_simple_value:
text = '###' + text + '###'
while self.column < 20:
text = ' ' + text
self.column += 1
self._org_write_plain(text, split)
def rewrite_write_literal(self, text):
if self.state == self.expect_block_mapping_simple_value:
last_nl = False
if text and text[-1] == '\n':
last_nl = True
text = text[:-1]
text = '###' + text + '###'
if False:
extra_indent = ''
while self.column < 15:
text = ' ' + text
extra_indent += ' '
self.column += 1
text = text.replace('\n', '\n' + extra_indent)
if last_nl:
text += '\n'
self._org_write_literal(text)
def rewrite_write_single_quoted(self, text, split=True):
if self.state == self.expect_block_mapping_simple_value:
last_nl = False
if text and text[-1] == u'\n':
last_nl = True
text = text[:-1]
text = u'###' + text + u'###'
if last_nl:
text += u'\n'
self.write_folded(text)
def rewrite_write_indicator(self, indicator, need_whitespace,
whitespace=False, indention=False):
if indicator and indicator[0] in u"*&":
indicator = u'###' + indicator + u'###'
while self.column < 20:
indicator = ' ' + indicator
self.column += 1
self._org_write_indicator(indicator, need_whitespace, whitespace,
indention)
dumper._org_write_plain = dumper.write_plain
dumper.write_plain = rewrite_write_plain
dumper._org_write_literal = dumper.write_literal
dumper.write_literal = rewrite_write_literal
dumper._org_write_single_quoted = dumper.write_single_quoted
dumper.write_single_quoted = rewrite_write_single_quoted
dumper._org_write_indicator = dumper.write_indicator
dumper.write_indicator = rewrite_write_indicator
print yaml.dump(data, Dumper=dumper, indent=4)
给你:
receipt: ###Oz-Ware Purchase Invoice###
date: ###2007-08-06###
customer:
given: ###Dorothy###
items:
- part_no: ###A4786###
descrip: ###Water Bucket (Filled)###
- part_no: ###E1628###
descrip: ###High Heeled "Ruby" Slippers###
size: ###8###
bill-to: ###&id001###
street: |
###123 Tornado Alley
Suite 16###
city: ###East Centerville###
state: ###KS###
ship-to: ###*id001###
specialDelivery: >
###Follow the Yellow Brick Road to the Emerald City.###
希望这对于 C# 中的进一步处理是可以接受的