views:

80

answers:

2

Hi everyone! How can I implement a Serialization Proxy that can be used with a XMLEncoder/Decoder? The classic one (the Bloch one, I mean) doesn't work...

A: 

The short answer is that you need to write a front-end that manages serialization. However, that will be painful, and may not give you what you want. The original Java serialization mechanism allowed objects to control their own serialization and deserialization, the XML implementation doesn't. As far as I can tell, XMLEncoder/XMLDecoder were meant for value objects alone.

If you describe what you're actually trying to accomplish, it's likely that someone can give you an alternative approach.

kdgregory
I would like to serialize into an XML a unmodifiable class, where the fields are final and there is no setter method.
Claudio
That is a desired solution, not a problem to be solved. What is the class used for? Why are you serializing it? Is XML the best way to accomplish the task?Regardless, my first sentence still holds: you need to write a front end to manage your serialization.
kdgregory
+1  A: 

I believe you need to set PersistenceDelegates for each Encoder instance. You can make some changes to properties using the usual bean mechanism of providing a BeanInfo.

If you look at the source you will see that there is a load of hardwired fudges for AWT and Swing beans. It really isn't nice - I suggest looking at something like JAXB.

Tom Hawtin - tackline
+1 for suggesting JAXB; it won't handle serialization of arbitrary objects (ie, without preprocessing), but is probably a better real-world solution
kdgregory