Different views of the YAML array

I am writing a file type converter using Python and PyYAML for a project where I have repeatedly translated and from YAML files. These files are then used by a separate service that I do not control, so I need to translate YAML back in the same way as I do. My source file has the following sections:

key:
- value1
- value2
- value3

What is evaluated {key: [value1,value2,value3]}using yaml.load(). When I transfer this back to YAML, my new file reads as follows:

key: [value1,value2,value3]

My question is whether these two forms are equivalent in relation to the different parsers of the YAML file language. Obviously, using PyYaml is equivalent, but is this true for Ruby or other languages โ€‹โ€‹that the application uses? Otherwise, the application will not be able to display the data correctly.

+4
2

, YAML, , . : http://www.yaml.org/spec/1.2/spec.html

3.2.3.1 ( ):

3.2.3.1. Node

Node , . Node . . ; , .

, Node - YAML, ( ). (. 7.4.1), ( 8.2.1). .

+3

, node - . .

PyYAML , default_flow_style :

yaml.dump(yaml.load("""\
key:
- value1
- value2
- value3
"""), sys.stdout, default_flow_style=False)

:

key:
- value1
- value2
- value3

" " ruamel.yaml ( : ),

import sys
import ruamel.yaml as yaml

yaml_str = """\
key:
- value1
- value2  # this is the second value
- value3
"""

data = yaml.load(yaml_str, Loader=yaml.RoundTripLoader)

yaml.dump(data, sys.stdout, Dumper=yaml.RoundTripDumper, default_flow_style=False)

:

key:
- value1
- value2  # this is the second value
- value3

/, , . (, YAML).

, YAML, , .

+2

Source: https://habr.com/ru/post/1598152/


All Articles