Hi Arun,
The thing is, that for JAXB based representations,
Badgerfish [1] convention is used for the generated JSON.
I am afraid it won't be possible to get something "nicer".
However, I can be wrong, so: good luck!
~Jakub
[1]
http://badgerfish.ning.com/
On Wed, Sep 05, 2007 at 05:34:23AM -0700, Arun Gupta wrote:
> Cool, this one works! I'm not very happy with the JSON representation
> 
> {"states":{"names":{"$":"California"}}}
> 
> but now will tweak the JAXB to get a more meaningful value.
> 
> Thanks,
> -Arun
> 
> Jakub Podlesak wrote:
> >More specifically, your jaxb class should look like:
> >
> >--cuthere--
> >@XmlRootElement(name = "states")
> >public class States {
> >
> >    @XmlElement(required=true)
> >    protected String names;
> >    
> >    public String getNames() {
> >        return names;
> >    }
> >
> >    public States() {
> >        this.names = names;
> >    }
> >    
> >    public States(String names) {
> >        this.names = names;
> >    }
> >}
> >--cuthere--
> >
> >
> >Please note the @XmlElement(required=true) annotation.
> >
> >~Jakub
> >
> >On Wed, Sep 05, 2007 at 08:18:04AM +0200, Jakub Podlesak wrote:
> >>Hi Arun,
> >>
> >>please add a default public no-arg constructor to your States jaxb class.
> >>It should help.
> >>
> >>~Jakub
> >>
> >>On Tue, Sep 04, 2007 at 11:12:09PM -0700, Arun Gupta wrote:
> >>>I tried invoking the endpoint using ant's get task and got the following 
> >>>error:
> >>>
> >>>-- cut here --
> >>>get:
> >>>      [get] Getting: http://localhost:9998/helloworld/json2
> >>>      [get] To: C:\workarea\samples\jersey\json\out
> >>>      [get] Error getting http://localhost:9998/helloworld/json2 to 
> >>>C:\workarea\
> >>>samples\jersey\json\out
> >>>
> >>>BUILD FAILED
> >>>C:\workarea\samples\jersey\json\build.xml:32: java.io.IOException: 
> >>>Premature EOF
> >>>
> >>>-- cut here --
> >>>
> >>>No error message is thrown on the server-side. Any further suggestions 
> >>>on debugging it ?
> >>>
> >>>-Arun
> >>>
> >>>
> >>>Jakub Podlesak wrote:
> >>>>Hi Arun,
> >>>>
> >>>>I have changed the produced content type to [application/xml]
> >>>>for your [samples.HelloWorldResource#getStates] method
> >>>>and it does not produce valid xml.
> >>>>
> >>>>I get following error message from firefox:
> >>>>--cuthere--
> >>>>XML Parsing Error: no element found
> >>>>Location: http://localhost:9998/helloworld/json2
> >>>>Line Number 1, Column 1:
> >>>>--cuthere--
> >>>>
> >>>>My recommendation is to test your jaxb class first.
> >>>>
> >>>>I will look at it and let you know,
> >>>>
> >>>>~Jakub
> >>>>
> >>>>On Tue, Sep 04, 2007 at 10:30:46PM -0700, Arun Gupta wrote:
> >>>>>I've attached a zip file that contains a really simple to demonstrate 
> >>>>>usage of JAXB. Steps to follow:
> >>>>>
> >>>>>1). Set jersey.home in build.xml appropriately
> >>>>>2). Invoke "ant run"
> >>>>>3). Access "http://localhost:9998/helloworld/json2" in browser
> >>>>>
> >>>>>The returned JSON is empty.
> >>>>>
> >>>>>What am I missing ?
> >>>>>
> >>>>>-Arun
> >>>>>
> >>>>>Arun Gupta wrote:
> >>>>>>After removing the private directory, I'm getting the following error 
> >>>>>>during build time:
> >>>>>>
> >>>>>>-- cut here --
> >>>>>>Created dir: 
> >>>>>>C:\workarea\samples\JsonFromJaxb\build\web\WEB-INF\classes
> >>>>>>Processing resource classes
> >>>>>>Problem encountered during annotation processing;
> >>>>>>see stacktrace below for more information.
> >>>>>>java.lang.ClassFormatError: Illegal class modifiers in class 
> >>>>>>com/sun/ws/rest/tools/webapp/writer/package-info: 0x1600
> >>>>>>      at java.lang.ClassLoader.defineClass1(Native Method)
> >>>>>>      at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
> >>>>>>      at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
> >>>>>>      at 
> >>>>>>com.sun.istack.tools.ParallelWorldClassLoader.findClass(ParallelWorldClassLoader.java:90) 
> >>>>>>
> >>>>>>      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
> >>>>>>      at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
> >>>>>>      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
> >>>>>>      at java.lang.Class.forName0(Native Method)
> >>>>>>      at java.lang.Class.forName(Class.java:242)
> >>>>>>      at java.lang.Package.getPackageInfo(Package.java:350)
> >>>>>>      at java.lang.Package.getAnnotation(Package.java:361)
> >>>>>>      at com.sun.xml.txw2.TXW.getTagName(TXW.java:60)
> >>>>>>      at com.sun.xml.txw2.TXW.create(TXW.java:83)
> >>>>>>      at 
> >>>>>>com.sun.ws.rest.tools.webapp.writer.WebAppWriter.writeTo(WebAppWriter.java:58) 
> >>>>>>
> >>>>>>      at 
> >>>>>>com.sun.ws.rest.tools.webapp.writer.WebAppWriter.write(WebAppWriter.java:66) 
> >>>>>>
> >>>>>>      at 
> >>>>>>com.sun.ws.rest.tools.annotation.apt.UriTemplateProcessor.process(UriTemplateProcessor.java:214) 
> >>>>>>
> >>>>>>      at 
> >>>>>>com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:60) 
> >>>>>>
> >>>>>>      at com.sun.tools.apt.comp.Apt.main(Apt.java:454)
> >>>>>>      at 
> >>>>>>com.sun.tools.apt.main.JavaCompiler.compile(JavaCompiler.java:448)
> >>>>>>      at com.sun.tools.apt.main.Main.compile(Main.java:1075)
> >>>>>>      at com.sun.tools.apt.main.Main.compile(Main.java:938)
> >>>>>>      at com.sun.tools.apt.Main.processing(Main.java:95)
> >>>>>>      at com.sun.tools.apt.Main.process(Main.java:85)
> >>>>>>      at com.sun.tools.apt.Main.process(Main.java:67)
> >>>>>>      at com.sun.ws.rest.tools.RsGenTool.run(RsGenTool.java:36)
> >>>>>>      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >>>>>>      at 
> >>>>>>sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
> >>>>>>
> >>>>>>      at 
> >>>>>>sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
> >>>>>>
> >>>>>>      at java.lang.reflect.Method.invoke(Method.java:585)
> >>>>>>      at com.sun.ws.rest.tools.Invoker.invoke(Invoker.java:104)
> >>>>>>      at com.sun.ws.rest.tools.RsGen.main(RsGen.java:31)
> >>>>>>-- cut here --
> >>>>>>
> >>>>>>and
> >>>>>>
> >>>>>>-- cut here --
> >>>>>>Initializing Sun's JavaServer Faces implementation (1.2_04-b20-p03) 
> >>>>>>for context ''
> >>>>>>"DPL8015: Invalid Deployment Descriptors in Deployment descriptor 
> >>>>>>file WEB-INF/web.xml in archive [web].
> >>>>>>Line -1 Column -1 -- Premature end of file."
> >>>>>>"DPL8005: Deployment Descriptor parsing failure : Premature end of 
> >>>>>>file."
> >>>>>>Exception occured in J2EEC Phaseorg.xml.sax.SAXParseException: 
> >>>>>>Premature end of file.
> >>>>>>com.sun.enterprise.deployment.backend.IASDeploymentException: Error 
> >>>>>>loading deployment descriptors for module [JsonFromJaxb] Line -1 
> >>>>>>Column -1 -- Deployment descriptor file WEB-INF/web.xml in archive 
> >>>>>>[web]. Premature end of file.
> >>>>>>      at 
> >>>>>>com.sun.enterprise.deployment.backend.Deployer.loadDescriptors(Deployer.java:380) 
> >>>>>>
> >>>>>>      at 
> >>>>>>com.sun.enterprise.deployment.backend.ModuleDeployer.loadDescriptors(ModuleDeployer.java:423) 
> >>>>>>
> >>>>>>      at 
> >>>>>>com.sun.enterprise.deployment.backend.WebModuleDeployer.deploy(WebModuleDeployer.java:157) 
> >>>>>>
> >>>>>>
> >>>>>>-- cut here --
> >>>>>>
> >>>>>>during runtime.
> >>>>>>
> >>>>>>Any idea what might be wrong ?
> >>>>>>
> >>>>>>-Arun
> >>>>>>Jakub Podlesak wrote:
> >>>>>>>Hi Arun,
> >>>>>>>
> >>>>>>>The NetBeans project should use jar files from it's [lib] 
> >>>>>>>subdirectory. I have rewritten the dependencies. (If you look at the 
> >>>>>>>snapshot you will realize
> >>>>>>>I run it from outside of Jersey dir structure)
> >>>>>>>
> >>>>>>>But you right, I forgot to remove [nbproject/private] directory, 
> >>>>>>>which keeps info about the files within my filesystem. It can 
> >>>>>>>probably cause issues.
> >>>>>>>
> >>>>>>>Could you please remove the [private] dir and try again?
> >>>>>>>I will fix the issue and update the project on the web.
> >>>>>>>
> >>>>>>>Thanks,
> >>>>>>>
> >>>>>>>~Jakub
> >>>>>>>
> >>>>>>>On Fri, Aug 31, 2007 at 11:56:52AM -0700, Arun Gupta wrote:
> >>>>>>>>Thanks Jakub.
> >>>>>>>>
> >>>>>>>>The NetBeans project seems to be using Jersey directory structure 
> >>>>>>>>for resolving libraries and that gives the error attached in the 
> >>>>>>>>image. I resolved the references but building the project gives the 
> >>>>>>>>following error:
> >>>>>>>>
> >>>>>>>>C:\workarea\samples\JsonFromJaxb\JsonFromJaxb\build.xml:22: taskdef 
> >>>>>>>>class com.sun.ws.rest.tools.ant.WebResourcesProcessorTask cannot be 
> >>>>>>>>found
> >>>>>>>>
> >>>>>>>>-Arun
> >>>>>>>>
> >>>>>>>>Jakub Podlesak wrote:
> >>>>>>>>>Hi Arun,
> >>>>>>>>>
> >>>>>>>>>please look at the sample linked from:
> >>>>>>>>>http://blogs.sun.com/japod/entry/json_representation_of_jaxb_object
> >>>>>>>>>
> >>>>>>>>>~Jakub
> >>>>>>>>>
> >>>>>>>>>On Fri, Aug 31, 2007 at 08:13:36AM -0700, Arun Gupta wrote:
> >>>>>>>>>>>What you need to do is to create a JAXB bean with 
> >>>>>>>>>>>@XmlRootElement on the class. Write a JAX-RS bean with a method 
> >>>>>>>>>>>that returns the JAXB bean and is annotated with 
> >>>>>>>>>>>@ProduceMime("application/json") something like:
> >>>>>>>>>>>
> >>>>>>>>>>>@XmlRootElement
> >>>>>>>>>>>public class States {
> >>>>>>>>>>>...
> >>>>>>>>>>>}
> >>>>>>>>>>>
> >>>>>>>>>>>@UriTemplate("states")
> >>>>>>>>>>>public class StatesResource {
> >>>>>>>>>>>
> >>>>>>>>>>>@HttpMethod
> >>>>>>>>>>>@ProduceMime("application/json")
> >>>>>>>>>>>States getStates() {
> >>>>>>>>>>>...
> >>>>>>>>>>>}
> >>>>>>>>>>>}
> >>>>>>>>>>>
> >>>>>>>>>>>The JAXB provider will automagically convert the returned States 
> >>>>>>>>>>>class instance into JSON for you. 
> >>>>>>>>>>This is reasonable. I tried it exactly this way without looking 
> >>>>>>>>>>at the sample and got an empty response.
> >>>>>>>>>>
> >>>>>>>>>>>I'm sure Jakub's example will show this in more detail.
> >>>>>>>>>>This sample is not configured to run by default:
> >>>>>>>>>>
> >>>>>>>>>>-- cut here --
> >>>>>>>>>>init:
> >>>>>>>>>>deps-jar:
> >>>>>>>>>>Processing resource classes
> >>>>>>>>>>compile:
> >>>>>>>>>>run:
> >>>>>>>>>>Exception in thread "main" java.lang.IllegalArgumentException
> >>>>>>>>>>     at 
> >>>>>>>>>>com.sun.ws.rest.samples.storageservice.Main.main(Main.java:85)
> >>>>>>>>>>Java Result: 1
> >>>>>>>>>>BUILD SUCCESSFUL (total time: 4 seconds)
> >>>>>>>>>>-- cut here --
> >>>>>>>>>>
> >>>>>>>>>>A default command-line argument is missing.
> >>>>>>>>>>
> >>>>>>>>>>-Arun
> >>>>>>>>>>>Marc.
> >>>>>>>>>>>
> >>>>>>>>>>>>>>use. You might not be able to get the exact JSON format you 
> >>>>>>>>>>>>>>show  below but I'd bet you could get close just using some 
> >>>>>>>>>>>>>>of the JAXB  annotations.
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>Marc.
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>On Aug 30, 2007, at 2:33 PM, Arun Gupta wrote:
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>In order to return a JSON array like:
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>[{"name":"California","value":"California"},{"name":"New  
> >>>>>>>>>>>>>>>York","value":"New York"},{"name":"Alabama" 
> >>>>>>>>>>>>>>>,"value":"Alabama"}, {"name":"Texas","value":"Texas"}]
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>I have to write the following code:
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>-- cut here --
> >>>>>>>>>>>>>>>@HttpMethod("GET")
> >>>>>>>>>>>>>>>@ProduceMime("application/json")
> >>>>>>>>>>>>>>>public JSONArray getMessage() throws JSONException {
> >>>>>>>>>>>>>>>   String[] states = { "California", "New York", "Alabama",  
> >>>>>>>>>>>>>>>"Texas"};
> >>>>>>>>>>>>>>>   JSONArray array = new JSONArray();
> >>>>>>>>>>>>>>>   for (String s : states) {
> >>>>>>>>>>>>>>>       JSONObject item = new JSONObject();
> >>>>>>>>>>>>>>>       item.put("name", s).put("value", s);
> >>>>>>>>>>>>>>>       array.put(item);
> >>>>>>>>>>>>>>>   }
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>   return array;
> >>>>>>>>>>>>>>>}
> >>>>>>>>>>>>>>>-- cut here --
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>I think this is too involving and low-level. Can the code be 
> >>>>>>>>>>>>>>>something like the following ?
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>-- cut here --
> >>>>>>>>>>>>>>>@JSONObject
> >>>>>>>>>>>>>>>ItemBean {
> >>>>>>>>>>>>>>>ItemBean(String name, String value) { ... }
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>@JSONObjectKey
> >>>>>>>>>>>>>>>public String getName() { ... }
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>@JSONObjectValue("value")
> >>>>>>>>>>>>>>>public String getNameValue() { ... }
> >>>>>>>>>>>>>>>}
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>@HttpMethod("GET")
> >>>>>>>>>>>>>>>@ProduceMime("application/json")
> >>>>>>>>>>>>>>>public List<ItemBean> getMessage() {
> >>>>>>>>>>>>>>>   String[] states = { "California", "New York", "Alabama",  
> >>>>>>>>>>>>>>>"Texas"};
> >>>>>>>>>>>>>>>List<ItemBean> list = new ArrayList<ItemBean>();
> >>>>>>>>>>>>>>>   for (String s : states) {
> >>>>>>>>>>>>>>>       ItemBean bean = new ItemBean(s, s);
> >>>>>>>>>>>>>>>       list.add(bean);
> >>>>>>>>>>>>>>>   }
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>   return list;
> >>>>>>>>>>>>>>>}
> >>>>>>>>>>>>>>>-- cut here --
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>This will require defining new annotations JSONObject,  
> >>>>>>>>>>>>>>>JSONObjectKey & JSONObjectValue. I think this is more 
> >>>>>>>>>>>>>>>natural to a  Java developer.
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>-Arun
> >>>>>>>>>>>>>>>-- 
> >>>>>>>>>>>>>>>Web Technologies and Standards
> >>>>>>>>>>>>>>>Sun Microsystems, Inc.
> >>>>>>>>>>>>>>>Blog: http://blogs.sun.com/arungupta
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>--------------------------------------------------------------------- 
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>>>>>>>For additional commands, e-mail: 
> >>>>>>>>>>>>>>>users-help_at_jersey.dev.java.net
> >>>>>>>>>>>>>>>
> >>>>>>>>>>>>>>---
> >>>>>>>>>>>>>>Marc Hadley <marc.hadley at sun.com>
> >>>>>>>>>>>>>>CTO Office, Sun Microsystems.
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>--------------------------------------------------------------------- 
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>>>>>>For additional commands, e-mail: 
> >>>>>>>>>>>>>>users-help_at_jersey.dev.java.net
> >>>>>>>>>>>>>>
> >>>>>>>>>>>>>--------------------------------------------------------------------- 
> >>>>>>>>>>>>>
> >>>>>>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>>>>>>-- 
> >>>>>>>>>>>>Web Technologies and Standards
> >>>>>>>>>>>>Sun Microsystems, Inc.
> >>>>>>>>>>>>Blog: http://blogs.sun.com/arungupta
> >>>>>>>>>>>>
> >>>>>>>>>>>>--------------------------------------------------------------------- 
> >>>>>>>>>>>>
> >>>>>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>>>>>>
> >>>>>>>>>>>---
> >>>>>>>>>>>Marc Hadley <marc.hadley at sun.com>
> >>>>>>>>>>>CTO Office, Sun Microsystems.
> >>>>>>>>>>>
> >>>>>>>>>>>
> >>>>>>>>>>>---------------------------------------------------------------------
> >>>>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>>>>>
> >>>>>>>>>>-- 
> >>>>>>>>>>Web Technologies and Standards
> >>>>>>>>>>Sun Microsystems, Inc.
> >>>>>>>>>>Blog: http://blogs.sun.com/arungupta
> >>>>>>>>>>
> >>>>>>>>>>---------------------------------------------------------------------
> >>>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>>>>
> >>>>>>>>>---------------------------------------------------------------------
> >>>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>>>
> >>>>>>>>-- 
> >>>>>>>>Web Technologies and Standards
> >>>>>>>>Sun Microsystems, Inc.
> >>>>>>>>Blog: http://blogs.sun.com/arungupta
> >>>>>>>>---------------------------------------------------------------------
> >>>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>---------------------------------------------------------------------
> >>>>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>>>>
> >>>>>-- 
> >>>>>Web Technologies and Standards
> >>>>>Sun Microsystems, Inc.
> >>>>>Blog: http://blogs.sun.com/arungupta
> >>>>
> >>>>>---------------------------------------------------------------------
> >>>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>---------------------------------------------------------------------
> >>>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>>
> >>>-- 
> >>>Web Technologies and Standards
> >>>Sun Microsystems, Inc.
> >>>Blog: http://blogs.sun.com/arungupta
> >>>
> >>>---------------------------------------------------------------------
> >>>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >>For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
> 
> -- 
> Web Technologies and Standards
> Sun Microsystems, Inc.
> Blog: http://blogs.sun.com/arungupta
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>