Add YAML 1.2 support#1
Merged
Merged
Conversation
so that other classes inheriting from it can use them * Move methods from SafeConstructor to BaseConstructor * Move methods from SafeRepresenter to BaseRepresenter
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)
This way people can play with it, and we don't promise this wrapper will stay
around forever, and newly created classes CommonDumper/CommonRepresenter aren't
exposed.
MyCoreLoader = yaml.experimental_12_Core_loader()
data = yaml.load(input, Loader=MyCoreLoader)
MyCoreDumper = yaml.experimental_12_Core_dumper()
out = yaml.dump(data, Dumper=MyCoreDumper)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ref: yaml#555