Configuring snakeyaml to ignore setters?

瀏覽次數:1,913 次
跳到第一則未讀訊息

sarikan

未讀,
2011年12月7日 上午8:12:382011/12/7
收件者:SnakeYAML
Greetings,
I've been using RestEasy framework to send and receive JAXB generated
classes for a while, and alongside of JSON and XML, it also provides
support for YAML through SnakeYAML.

The problem is, JAXB generated types do not have setters for
collections, which makes SnakeYAML throw an exception
( org.yaml.snakeyaml.error.YAMLException: No JavaBean properties found
in ...)
There may be more problems with them , but this is my show stopper at
the moment.

I have two choices, trying to modify the JAXB generator to add setters
(which I would not prefer) or to convince SnakeYAML to ignore setters,
which will not be required in my use case anyway.

Is there a way of doing this?

Best regards
Seref

Andrey

未讀,
2011年12月7日 上午8:22:472011/12/7
收件者:snakeya...@googlegroups.com
Can you please provide more information ? What classes are failing ? How do you expect them to be serialized ?

What I see so far is the following. You try to serialize an object as a JavaBean but the class of the object does not define properties as it is specified in the JavaBean specification.
SnakeYAML has its own way to serialize the Java collections according to the YAML specification. If the failing class happens to be a Java collection please let us know. 

A failing test is the best way to identify a problem. Feel free to provide a JUnit test.

-
Andrey

Seref Arikan

未讀,
2011年12月7日 上午9:16:512011/12/7
收件者:snakeya...@googlegroups.com
Hi Andrey,
Thanks for the response. Here is an example:

@XmlAccessorType(
XmlAccessType.FIELD)
@XmlType(name = "Archetypes_List", propOrder = {
    "name"
})
public class ArchetypesList {

    protected List<String> name;

    public List<String> getName() {
        if (name == null) {
            name = new ArrayList<String>();
        }
        return this.name;
    }
  
    //this setter does not exist in the xjc output
    public void setName(List<String> pval){
       
    }

}


Normally, the JAXB compiler, xjc.exe generates the class above from an xml schema. The original of the class does not have a setter, since JAXB specs create immutable collections. If I add the setter by hand, all values I've put into name field are serialized to YAML correctly. Remove that setter, and you end up with the exception I've mentioned before.

However, I have lots of classes like these, created from the xml schema via JAXB, and they are not proper beans, since for collections they do not have setters, only getters.
Can I stop snakeYAML from checking for setters (at least for collections) so that I can use these (almost java bean) types?

Kind regards


--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To view this discussion on the web visit https://groups.google.com/d/msg/snakeyaml-core/-/veAZ1DR4AZMJ.
To post to this group, send email to snakeya...@googlegroups.com.
To unsubscribe from this group, send email to snakeyaml-cor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/snakeyaml-core?hl=en.

Andrey

未讀,
2011年12月7日 上午9:29:052011/12/7
收件者:snakeya...@googlegroups.com
It looks like what you want is not to "ignore setters" as you asked, but to set the value without available setter.


By default standard JavaBean properties and public fields are included. BeanAccess.FIELD makes is possible to use private fields directly.


-
Andrey

maslovalex

未讀,
2011年12月7日 上午9:32:132011/12/7
收件者:snakeya...@googlegroups.com
Isn't DumpOptions.setAllowReadOnlyProperties(boolean allowReadOnlyProperties) is made for that?

-alex

maslovalex

未讀,
2011年12月7日 上午9:35:172011/12/7
收件者:snakeya...@googlegroups.com
but that's for dumping. If you want to load yaml back you need to use BeanAccess.FIELD or your own implementation.

-alex

Seref Arikan

未讀,
2011年12月7日 上午11:51:372011/12/7
收件者:snakeya...@googlegroups.com
Dear Alex,
Bingo! This did the trick for me. RestEasy was using dump to create yaml output, so I've simply added the option, and the error is gone.
The problem is that JAXB is not exactly producing types which are compatible with Java beans specs. It is great that SnakeYAML has the flexibility that allows me to save the day for now ;)

Kind regards
Seref

On Wed, Dec 7, 2011 at 2:32 PM, maslovalex <alexande...@gmail.com> wrote:
Isn't DumpOptions.setAllowReadOnlyProperties(boolean allowReadOnlyProperties) is made for that?

-alex

--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To view this discussion on the web visit https://groups.google.com/d/msg/snakeyaml-core/-/6MlXZdI3MQcJ.
回覆所有人
回覆作者
轉寄
0 則新訊息