yeison / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

Add option to skip missing properties when deserializing YAML into Java object #154

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Current behavior:
1. Create YAML doc with a property that doesn't exist in Test.class
2. Call new Yaml().loadAs(doc, Test.class)
3. Throws YAMLException.

We would like an option that allows properties such as this to be skipped. The 
natural place for this seems to be on the PropertyUtils object. Here is our 
suggested API:

        Representer representer = new Representer();
        // skip missing properties; default is false, i.e. current behavior
        representer.getPropertyUtils().setSkipMissingProperties(true);
        yaml = new Yaml(new Constructor(), representer);
        String doc = "missing_property: 5";
        TestBean bean = yaml.loadAs(doc, TestBean.class);

What version of SnakeYAML are you using? On what Java version?
Currently running SnakeYAML 1.10 on Java 6

Original issue reported on code.google.com by sh...@knewton.com on 23 Jul 2012 at 9:29

GoogleCodeExporter commented 9 years ago
Note that this is not a defect, but I don't see a way to modify the type.

Original comment by sh...@knewton.com on 23 Jul 2012 at 9:31

GoogleCodeExporter commented 9 years ago
I just added this code into my fork at 
https://code.google.com/r/shawn-snakeyaml/. I don't see a way to create a pull 
request; let me know if there's anything that I can do to help, or if the fix 
should be different. Thanks.

Original comment by sh...@knewton.com on 23 Jul 2012 at 10:24

GoogleCodeExporter commented 9 years ago
I do not mind to include the change into the coming release. (since the change 
is backwards-compatible). I will have a close look into the code later this 
week. 

Original comment by py4fun@gmail.com on 24 Jul 2012 at 4:30

GoogleCodeExporter commented 9 years ago
Great, thanks. I also added a log statement when a property is skipped since 
people may still want to know that. However, I didn't see any other logging in 
to SnakeYAML code. Please let me know if there's a better way of doing logging, 
and I can update it.

Original comment by sh...@knewton.com on 24 Jul 2012 at 4:35

GoogleCodeExporter commented 9 years ago
I see in the Mercurial history that you try to apply GIT usage to Mercurial. In 
Mercurial you do not have to use branches at all. At least we never use them in 
SnakeYAML. If you do not mind I will merge all your commits into one without 
the branch.

Original comment by py4fun@gmail.com on 26 Jul 2012 at 3:28

GoogleCodeExporter commented 9 years ago
That's completely fine with me.

Original comment by sh...@knewton.com on 26 Jul 2012 at 3:30

GoogleCodeExporter commented 9 years ago
Your patch is taken.
http://code.google.com/p/snakeyaml/wiki/changes

It will be delivered in version 1.11

By the way, does it also fix issue 152 ?

Original comment by py4fun@gmail.com on 27 Jul 2012 at 10:12

GoogleCodeExporter commented 9 years ago
Great, we'll upgrade to 1.11 when that becomes available.

Wrt issue 152, the OP wanted a way to map yaml_property_a to java_property_b, 
and this only provides a way to ignore yaml_property_a if java_property_b 
doesn't exist. One doesn't naturally derive from the other, since there would 
need to be some mapping metadata that doesn't exist.

Original comment by sh...@knewton.com on 27 Jul 2012 at 2:06

GoogleCodeExporter commented 9 years ago
Thank you for the time and efforts.

Original comment by aso...@gmail.com on 27 Jul 2012 at 3:06