Search code examples
snakeyaml

How to set root construct for SnakeYaml?


I'd like to a construct which is going to kick in by default and therefore I'll be able to extract some metadata and remove it from the parsed YAML.

For instance I could have a construct to do that but in SnakeYAML to register a construct one has to specify a String for it meaning that it has to have a name. However, I don't want that but instead I want my tag to be a root one, a default one which always will kick in so to say.

Is there a possibility to do something like that?

Cheers,


Solution

  • I actually found the answer myself, there is a property at org.yaml.snakeyaml.constructor.BaseConstructor#rootTag which one can set a tag like:

    super.rootTag = new Tag("!root");
    

    The rest is simple because one has to only register that construct to org.yaml.snakeyaml.constructor.BaseConstructor#yamlConstructors and you are done. Your construct will be triggered in all read attempts.

    Hope that helps to someone,

    Cheers,