More and more YAML libraries are implementing YAML 1.2, either new ones
simply starting with 1.2 or older ones adding support for it.
While also the syntax was changed in YAML 1.2, this pull request is about the
schema changes.
As an example, in 1.1, Y, yes, NO, on etc. are resolved as booleans in 1.1.
This sounds convenient, but also means that all these 22 different strings must
be quoted if they are not meant as booleans. A very common obstacle is the
country code for Norway, NO ("Norway Problem").
In YAML 1.2 this was improved by reducing the list of boolean representations.
Also other types have been improved. The 1.1 regular expression for float allows
. and ._ as floats, although there isn't a single digit in these strings.
While the 1.2 Core Schema, the recommended default for 1.2, still allows a few
variations (true, True and TRUE, etc.), the 1.2 JSON Schema is there to match
JSON behaviour regarding types, so it allows only true and false.
Note that this implementation of the YAML JSON Schema might not be exactly like
the spec defines it (all plain scalars not resolving to numbers, null or
booleans would be an error).
Short usage example:
class MyCoreLoader(yaml.BaseLoader): pass
class MyCoreDumper(yaml.CommonDumper): pass
MyCoreLoader.init_tags('core')
MyCoreDumper.init_tags('core')
data = yaml.load(input, Loader=MyCoreLoader)
output = yaml.dump(data, Dumper=MyCoreDumper)
Detailed example code to play with:
import yaml
class MyCoreLoader(yaml.BaseLoader): pass
MyCoreLoader.init_tags('core')
class MyJSONLoader(yaml.BaseLoader): pass
MyJSONLoader.init_tags('json')
class MyCoreDumper(yaml.CommonDumper): pass
MyCoreDumper.init_tags('core')
class MyJSONDumper(yaml.CommonDumper): pass
MyJSONDumper.init_tags('json')
input = """
- TRUE
- yes
- ~
- true
#- .inf
#- 23
#- #empty
#- !!str #empty
#- 010
#- 0o10
#- 0b100
#- 0x20
#- -0x20
#- 1_000
#- 3:14
#- 0011
#- +0
#- 0001.23
#- !!str +0.3e3
#- +0.3e3
#- &x foo
#- *x
#- 1e27
#- 1x+27
"""
print('--------------------------------------------- BaseLoader')
data = yaml.load(input, Loader=yaml.BaseLoader)
print(data)
print('--------------------------------------------- SafeLoader')
data = yaml.load(input, Loader=yaml.SafeLoader)
print(data)
print('--------------------------------------------- CoreLoader')
data = yaml.load(input, Loader=MyCoreLoader)
print(data)
print('--------------------------------------------- JSONLoader')
data = yaml.load(input, Loader=MyJSONLoader)
print(data)
print('--------------------------------------------- SafeDumper')
out = yaml.dump(data, Dumper=yaml.SafeDumper)
print(out)
print('--------------------------------------------- MyCoreDumper')
out = yaml.dump(data, Dumper=MyCoreDumper)
print(out)
print('--------------------------------------------- MyJSONDumper')
out = yaml.dump(data, Dumper=MyJSONDumper)
print(out)