Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet155/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   151   152   153   154   155   156   157   158   ...   245
Bog'liq
Java 17 Recipes

 How It Works
XMLEncoder and XMLDecoder, like the Serialization framework, use reflection to 
determine which fields are to be written, but instead of writing the fields as binary, they 
are written as XML. Objects that are encoded do not need to be serializable, but they 
need to follow the JavaBeans specification.
JavaBeans is the name of any object that conforms to the following contract.
• The object contains a public empty (no-arg) constructor.
• The object contains public getters and setters for each protected/
private property that takes the name of get{Property}() and 
set{Property}().
XMLEncoder and XMLDecoder encode/decode only the properties of the bean that 
have public accessors (get{property}, set{property}), so any properties that are 
private and do not have accessors are not encoded/decoded.
Tip It is a good idea to register an exception listener when encoding/decoding.
XMLEncoder creates a new instance of the class that is being serialized (remember 
that they need to be JavaBeans, so they must have an empty no-arg constructor), and 
then figures out which properties are accessible (via get{property}, set{property}). 
And if a property of the newly instantiated class contains the same value as the property 
of the original class (i.e., has the same default value), XMLEncoder doesn’t write that 
property. In other words, if the default value of a property hasn’t changed, XMLEncoder 
ChApTer 8 InpuT And OuTpuT


306
does not write it. This provides the flexibility of changing what a “default” value is 
between versions. For example, if the default value of a property is 2 when an object is 
encoded and later decoded after the default property changes from 2 to 4, the decoded 
object contains the new default property of 4 (which might not be correct).
XMLEncoder also keeps track of references. If an object appears more than once when 
being persisted in the object graph (for example, an object is inside a Map from the main 
class but is also as the DefaultValue property), then XMLEncoder only encodes it once 
and link up a reference by putting a link in the XML. The XMLEncoder/XMLDecoder class 
is much more forgiving than the Serialization framework. When decoding, if a property 
type is changed or deleted/added/moved/renamed, the decoding decodes “as much as 
it can” while skipping the properties that it couldn’t decode.
The recommendation is to not persist your main classes (even though XMLEncoder is 
more forgiving) but to create special objects that are simple, hold the basic information, 
and do not perform many tasks by themselves.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   151   152   153   154   155   156   157   158   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling