For Programmers: Free Programming Magazines  


Home > Archive > Java Beans > March 2006 > Java Bean properties not serializing default values









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Java Bean properties not serializing default values
wizofaus@hotmail.com

2006-03-27, 7:00 pm

If a class has a default value for a particular property, even if you
explicitly in the code set the value to that same value, e.g.

public class MyBean
{
private int prop = 42;
void setProp(int p) { prop = p; }
int getProp() { return prop; }
};

....

MyBean bean = new MyBean();

bean.setProp(42);


then it appears the property value will not get serialized (at least,
using the XMLEncoder).
Only if the value is different to the default value assigned in the
class definition will it be saved.
I've checked and rechecked this, and I'm 100% sure this is what is
occurring.

But this is quite unacceptable, as the default value in the code may
well change in the future - but I don't want old files to suddenly be
interpreted differently. Is there a way to ensure that *all* property
values are written, no matter if they are the same as the default
values?

Thanks,

Dylan

wizofaus@hotmail.com

2006-03-27, 7:00 pm

Never mind, I found out how to get around this (setting the "transient"
property to false). Still, kind of annoying.

wizofaus@hotmail.com

2006-03-28, 4:00 am

Ok, do mind - that doesn't work. Supposedly you can fix this problem
using XMLEncoder.setPersistenceDelegate() but I couldn't get it to
work.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com