Closed GoogleCodeExporter closed 9 years ago
SnakeYAML needs to know how to serialize/deserialize stateful Java instances.
SnakeYAML defines some rules for known classes (Integer, String etc). All other
classes are treated as JavaBeans if nothing else is specified.
This is exactly what happens in this case. The instances are not JavaBeans and
there is no explicit description what to do with the instances.
It is not an error to have no properties. But it an error to serialize an
instance with no data to serialize without any explanation to the parser. In
the same way if you need to serialize 'java.io.File' you may wish to include or
exclude its content. But it should be explicit definition and not a wild guess
of the parser.
Original comment by py4fun@gmail.com
on 17 May 2011 at 11:48
While I agree that the parser should not be making a wild guess, I believe that
it is already making a guess to some degree. In the default mode, when the
serializer/parser assumes an object is a JavaBean, it will ignore private
fields as though their values don't matter without giving any sort of error.
So long as there is at least one public field or JavaBean property, the parser
has no issues with ignoring the rest of the object state that is not publicly
accessible. Of course, the developer is on his own if he does such a thing...
but it is still a guess made by the parser.
In my situation, however, I know that the object that I'm trying to serialize
should have no problems being treated in the default manner. A thorough
inspection of the object's fields, like what is done for the switch case of
BeanAccess.FIELD (PropertyUtils:50-58, only also checking Modifier.isPublic)
can determine that some objects have no fields whatsoever, so that the parser
could know this too, without guessing. In the end, the only information of
value to me for some interface implementations is the virtual method table that
dispatches the call, and that's encoded in the class name.
Is a JavaBean not a JavaBean simply because it has no instance data (besides
the VMT)?
Anyhow, as we've both mentioned, there are ways around this issue. It's just
that I think the check that is being done and throwing the exception is not
accurate enough. I would recommend throwing the exception in the case where
you can guarantee some state is being missed: some fields are not public and
there are no set/get methods. Unfortunately, that test may cause some users'
code to fail when currently it does not.
Original comment by elkn...@gmail.com
on 17 May 2011 at 2:04
1)
> I believe that it is already making a guess to some degree.
If you find the current "guess" as inappropriate please create an issue and we
can discuss it separately
2)
>like what is done for the switch case of BeanAccess.FIELD
Using BeanAccess.FIELD is an _explicit_ action and users should understand what
is happening.
We may introduce another explicit action to indicate that no properties is a
normal state. But it does not mean that the current situation is an error
3)
> Is a JavaBean not a JavaBean simply because it has no instance data (besides
the VMT)?
I do not think this is the proper place to discuss that is a JavaBean and what
is not. At the moment SnakeYAML trusts 'java.beans.Introspector.getBeanInfo()'
(unfortunately it does not work the same across different JDKs)
4)
> I would recommend throwing the exception in the case where you can guarantee
> some state is being missed: some fields are not public and there are no
> set/get methods. Unfortunately, that test may cause some users' code to fail
> when currently it does not.
Private fields without public access methods do not violate the JavaBean spec.
I do not understand why it should throw an exception
Original comment by py4fun@gmail.com
on 23 May 2011 at 1:55
I imagine we're misunderstanding one another, and you're right, this is not a
good place to discuss this.
Original comment by elkn...@gmail.com
on 23 May 2011 at 2:01
Original comment by py4fun@gmail.com
on 23 May 2011 at 3:09
possible patch attached...
Original comment by elkn...@gmail.com
on 8 Jun 2011 at 6:36
Attachments:
Issue is re-open due to discussion in
https://groups.google.com/forum/#!topic/snakeyaml-core/-sRma0bgCnM/discussion
I have put your patch to a clone: http://code.google.com/r/py4fun-emptybean/
The change is accepted without modifications
(http://code.google.com/r/py4fun-emptybean/source/detail?r=af3bbeafef0df3deb554a
0858fea634806e724f8)
A weird test case is added just to see how it works.
I have removed one failing test because it was merely testing that an empty
JavaBean must not be dumped.
But one failing test is left (in RepresenterTest). I think the test proves
there is a regression. A JavaBean is dumped as if it is empty, but it is not.
Failure to get a property must not cause an empty bean to be serialized.
Instead, an exception is expected. We shall investigate the cause of the
failure.
Original comment by py4fun@gmail.com
on 9 Jun 2011 at 8:27
Sorry, the failing test was not caused by your patch. (I am afraid it reveals a
bug in SnakeYAML and it must be treated separately).
The fix has been promoted to the master source repository.
Feel free to test the latest SNAPSHOT:
https://oss.sonatype.org/content/groups/public/org/yaml/snakeyaml/1.9-SNAPSHOT/
Original comment by py4fun@gmail.com
on 9 Jun 2011 at 9:50
The fix will be delivered in version 1.9
Original comment by py4fun@gmail.com
on 10 Jun 2011 at 1:15
Original issue reported on code.google.com by
elkn...@gmail.com
on 16 May 2011 at 7:35Attachments: