I am using SnakeYaml to parse yaml file, is there anyway to ignore properities from the yaml file ?
Asked
Active
Viewed 1.0k times
2 Answers
38
I found it :)
Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(MyClass.class),representer);

The Alchemist
- 3,397
- 21
- 22

Bilal Halayqa
- 932
- 1
- 6
- 25
-
2This solution doesn't work if you have a custom `PropertyUtils` that call `super.getProperty(type, name)`. In this case you need to wrap the call in a `try-catch` block and return an empty `Property` object if it fails (: – Jezor Nov 17 '17 at 12:32
1
This is based on the solution from Bilal; however, the empty Representer Constructor has been marked as deprecated.
Adding the following will avoid using the deprecated constructor.
For SnakeYml 1.x
Representer representer = new Representer(new DumperOptions());
representer.getPropertyUtils().setSkipMissingProperties(true);
Constructor constructor = new Constructor(MyClass.class);
Yaml yaml = new Yaml(constructor, representer);
For SnakeYml >= 2.x
Representer representer = new Representer(new DumperOptions());
representer.getPropertyUtils().setSkipMissingProperties(true);
LoaderOptions loaderOptions = new LoaderOptions();
Constructor constructor = new Constructor(MyClass.class, loaderOptions);
Yaml yaml = new Yaml(constructor, representer);

Reg
- 10,717
- 6
- 37
- 54