Hi Sanjay,
 
The JAX-RS spec doesn't mandate any particular JSON serialization. From what
I see the JSON provider shipped with Restlet/JAX-RS extension supports
automatic serialization of Map instances and of Java beans.
 
>From the JSONObject(Object bean) Javadocs:
 

* Construct a JSONObject from an Object using bean getters.

* It reflects on all of the public methods of the object.

* For each of the methods with no parameters and a name starting

* with <code>"get"</code> or <code>"is"</code> followed by an uppercase
letter,

* the method is invoked, and a key and the value returned from the getter
method

* are put into the new JSONObject.

*

* The key is formed by removing the <code>"get"</code> or <code>"is"</code>
prefix. If the second remaining

* character is not upper case, then the first

* character is converted to lower case.

*

* For example, if an object has a method named <code>"getName"</code>, and

* if the result of calling <code>object.getName()</code> is <code>"Larry
Fine"</code>,

* then the JSONObject will contain <code>"name": "Larry Fine"</code>.

 
Are you providing getters for your Item class?

Best regards,
Jerome
 

  _____  

De : Sanjay Acharya [mailto:[EMAIL PROTECTED] 
Envoyé : mardi 5 août 2008 06:11
À : [email protected]
Objet : RE: JSON Set with JAXRS


I got this working  with a custom MessageBodyWriter. Not sure if there is a
simpler way...anyways :-)




  _____  

From: [EMAIL PROTECTED]
To: [email protected]
Date: Tue, 5 Aug 2008 02:11:54 +0000
Subject: RE: JSON Set with JAXRS


I looked at the restlet example of Employees and changed my code to return a
ItemList<Item> and got back a better JSON response like:
{"class":"class Item","items":["[EMAIL PROTECTED]","[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> "]}

What should I do to get the same back with {[id:10, name=car, id:21,
name=tank]}, i.e., json representation of Item. I would not like to override
the toString() method of Item to be in JSON format.
 
Would I need to create custom message body writer for the Item class?
 
Thanks in advance.



 

  _____  


From: [EMAIL PROTECTED]
To: [email protected]
Date: Tue, 5 Aug 2008 00:38:29 +0000
Subject: JSON Set with JAXRS

Greetings,
 
I am exploring Restlet's JAXRS support and trying the following in a
Resource class:
 
@GET
@ProduceMime("application/json")
public Set<Items> getItems() {
    ....
    return items;
}
 
When the call is made, the Resource is invoked, however the returned
response is not a JSON formatted set but something like:
{"empty":false,"class":"class java.util.HashSet"}
 
Do I need to write some custom code to handle the mime type? I though JAXB,
JSON etc were pretty much standard and I would 
have got back a JSON string like [[....] [....]]?
 
Please let me know if I am missing something here. Thanks in advance for any
tips...
 



  _____  


Your PC, mobile phone, and online services work together like never before.
See how  <http://clk.atdmt.com/MRT/go/108587394/direct/01/> Windows® fits
your life 


  _____  

Your PC, mobile phone, and online services work together like never before.
See how  <http://clk.atdmt.com/MRT/go/108587394/direct/01/> Windows® fits
your life 

  _____  

Got Game? Win Prizes in the Windows Live Hotmail Mobile Summer Games Trivia
Contest Find out how.
<http://www.gowindowslive.com/summergames?ocid=TXT_TAGHM>  

Reply via email to