Thread: 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/ |
From: Andres A. <aal...@ya...> - 2007-07-05 17:54:33
|
Florian, Using a map will work because maps do not carry type information for a property, only names and values =) 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 12:44:47 Asunto: Re: [json-lib-user] Default values when converting from Java beans to JSON objects Hi Andres, Here's a reply to myself :-) that might provide some sort of solution to the previous problem (although it is as elegant as an elephant) We can create a Map<Object, Object> that will contain all the names of the fields from the Java bean that we want to convert into a JSONObject associated to their values. Instead of converting the Java bean into a JSONObject we can convert the map. In fact both JSONObject.fromBean( map ) and JSONObject.fromMap( map ) seem to work as expected. I am not sure whether there are any subtle complications with this approach, but it seems to solve my problem (at least at the moment...) Thanks agin, Florian On 7/5/07, Florian Shkurti <flo...@gm...> wrote: 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/ |