marcusaram / snakeyaml

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

Enchancement Request: break value representation into separate method #48

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Starting at line 103 of Representer.java, it would be nice if a sub class could 
decide how to 
represent that specific value. getProperties allows a subclass to determine 
which properties get 
written as a whole, but it would be nice to not write out nulls or default 
values in certain situations. 

Replacing all of representJavaBean, the other option, makes the subclass more 
fragile. 

Original issue reported on code.google.com by obast...@gmail.com on 11 Feb 2010 at 9:53

GoogleCodeExporter commented 9 years ago
Can't figure out how to make this an Enhancement request, sorry. 

Original comment by obast...@gmail.com on 11 Feb 2010 at 9:54

GoogleCodeExporter commented 9 years ago
It seems an important feature: to exclude JavaBean properties based on some 
runtime 
criteria.
Do you have an idea how the API should look like ?

Original comment by py4fun@gmail.com on 12 Feb 2010 at 9:10

GoogleCodeExporter commented 9 years ago
I'm of two thoughts here. 

First is simple:

representJavaBeanProperty(Object javaBean, Property property, Object 
javaBeanValue)

Move the contents of the for loop into the method, then loop over the method 
call. It's then trivial to just look 
at the value and filter whether you call super. representJavaBeanProperty or 
not. 

Second is slightly different:

representJavaBeanProperties(Object javaBean, List<Property> properties)

Virtue of the second is that in my case, I want to filter out both nulls (easy 
with the first) and default values 
(more complex with the first because I have to build an empty object and 
compare the values). 

But I suspect #1 is the way to go, if you tweak the original to look like #1, I 
can just add a lazily built cache in 
a map of "blank" objects to compare against for default values. 

Original comment by obast...@gmail.com on 12 Feb 2010 at 3:27

GoogleCodeExporter commented 9 years ago
>representJavaBeanProperty(Object javaBean, Property property, Object 
javaBeanValue)

But how the COMPLETE solution should look like?
I mean how to create the Yaml instance and what should be the custom code ?

If you intend to overwrite the Representer then you can do it now and we do not 
need 
the API.

Original comment by py4fun@gmail.com on 12 Feb 2010 at 5:34

GoogleCodeExporter commented 9 years ago
The issue is that the method you have to override is large and complex, so if 
you override all of 
representJavaBean its fragile. 

If you moved the contents of the properties loop to 
representJavaBeanProperty(Object javaBean, Property 
property, Object javaBeanValue), then a "null filter" would be:

public void representJavaBeanProperty(Object javaBean, Property property, 
Object javaBeanValue)
{
    if (javaBeanValue !=null) super. representJavaBeanProperty(javaBean,property,javaBeanValue)

}

Original comment by obast...@gmail.com on 12 Feb 2010 at 6:35

GoogleCodeExporter commented 9 years ago
Finally I understand what you mean. I have just made the change.
Please have a look:
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/issues/issue48/SkipJavaBeanPropertyTest.java

Thank you for the idea.

Original comment by aso...@gmail.com on 12 Feb 2010 at 9:10

GoogleCodeExporter commented 9 years ago
Looks great. 

Original comment by obast...@gmail.com on 12 Feb 2010 at 9:19

GoogleCodeExporter commented 9 years ago
It will be included in version 1.6

Original comment by aso...@gmail.com on 12 Feb 2010 at 11:06