Re: [json-lib-user] Default values when converting from Java beans to JSON objects
Brought to you by:
aalmiray
From: Florian S. <flo...@gm...> - 2007-07-05 17:44:48
|
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 > > > > > > |