Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the YAML 1.2 Core and JSON schemas [Take 2] #555

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Commits on Dec 2, 2023

  1. Move around methods for better inheritance

    so that other classes inheriting from it can use them
    
    * Move methods from SafeConstructor to BaseConstructor
    * Move methods from SafeRepresenter to BaseRepresenter
    perlpunk committed Dec 2, 2023
    Configuration menu
    Copy the full SHA
    fc4be2e View commit details
    Browse the repository at this point in the history
  2. Add support for YAML 1.2 schemas

    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)
    perlpunk committed Dec 2, 2023
    Configuration menu
    Copy the full SHA
    b80472f View commit details
    Browse the repository at this point in the history
  3. Add experimental wrappers around YAML 1.2 tags code

    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)
    perlpunk committed Dec 2, 2023
    Configuration menu
    Copy the full SHA
    5e41e14 View commit details
    Browse the repository at this point in the history