Re: [json-lib-user] Default values when converting from Java beans to JSON objects
Brought to you by:
aalmiray
|
From: Andres A. <aal...@ya...> - 2007-07-05 15:35:43
|
Hi Florian,
Per the JSON spec,only objects may be null, meaning that numbers, booleans, arrays and strings
will use a default value when the Java bean has a null reference assigned. Default values are
Number - integer 0
Boolean - false
String - empty ""
Array (Collection) - empty [ ]
As you say in your message, you expect 'myInt' and 'myString' to have a null value (which is the correct
assumption on the Java world) but when serializing to JSON those properties are submitted to the above
rules. You can "fool" the serialization though, by making the following changes to your bean:
public class MyBean {
private Object myInt;
private Object myString;
public Object getMyInt(){ return myInt; }
public Object getMyString(){ return myString; }
public void setMyInt( Integer myInt ) { this.myInt = myInt; }
public void setMyString( String myString ) { this.myString = myString; }
}
Which will allow you to transform the bean to JSON but won0t work the other way around as the write methods
for each property do not exist 'perse' because the types for read/write are not equal, the PropertyDescriptor
will see the properties as read-only. If you add the proper setters with Object then the properties will become
writable, allowing for the following test to run green:
public void testBean() {
NBean bean = new NBean();
JSONObject json = JSONObject.fromObject( bean );
System.err.println( json );
bean.setMyInt( new Integer( 1 ) );
json = JSONObject.fromObject( bean );
System.err.println( json );
NBean b = (NBean) JSONObject.toBean( JSONObject.fromObject( "{'myInt':null}" ), NBean.class );
System.err.println( b );
b = (NBean) JSONObject.toBean( JSONObject.fromObject( "{'myInt':42}" ), NBean.class );
System.err.println( b );
b = (NBean) JSONObject.toBean( JSONObject.fromObject( "{'myInt':'48'}" ), NBean.class );
System.err.println( b );
}
-- output
{"myInt":null,"myString":null}
{"myInt":1,"myString":null}
[null,null]
[42,null]
[48,null]
The problem with allowing the workaround is that if you send {"myInt":null,"myString":null} across the wire
to a browser and if it uses the json parser [or the defualt eval()] 'myInt' and 'myString' will be treated as
null object references instead of empty scalar values for number and string.
Cheers,
Andres
-------------------------------------------
http://jroller.com/page/aalmiray
http://www.linkedin.com/in/aalmiray
--
What goes up, must come down. Ask any system administrator.
There are 10 types of people in the world: Those who understand binary, and those who don't.
To understand recursion, we must first understand recursion.
----- Mensaje original ----
De: Florian Shkurti <flo...@gm...>
Para: jso...@li...
Enviado: jueves, 5 de julio, 2007 10:07:11
Asunto: [json-lib-user] Default values when converting from Java beans to JSON objects
Hi Andres,
I have the following Java bean class:
public class MyBean {
private Integer myInt;
private String myString;
public MyBean(){
this.myInt = null;
this.myString = null;
}
//getters & setters
}
When I try to convert this bean into a JSON object using
JSONObject.fromBean( new MyBean() )
I get { "myInt": 0, "myString": "" } instead of what I would
expect, namely { "myInt": null, "myString": null }.
Is there a way to get the second JSONObject, which is
what I intuitively expect?
Thanks in advance,
Florian
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
json-lib-user mailing list
jso...@li...
https://lists.sourceforge.net/lists/listinfo/json-lib-user
Llama gratis a cualquier PC del mundo.
Con una excelente calidad de sonido.
http://mx.messenger.yahoo.com/ |