cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sergey.beryoz...@iona.com>
Subject RE: JSON in CXF
Date Thu, 30 Jul 2009 11:56:28 GMT

Hi Gary

I added a simple test confirming Jettison can output the data meeting the
natural notation criteria. I thought I'd need to add a simple STAX writer
wrapper (which is something one can easily do now with JSONProvider anyway
whenever one wants to customize somehow the output being produced) but it
proved to be even simpler to achieve.

So given say Post & Comment classes :

   @XmlRootElement()
   @XmlType(name = "", propOrder = {"title", "comments" })
    public static class Post {
        private String title;
        private List<Comment> comments = new ArrayList<Comment>();
        public void setTitle(String title) {
            this.title = title;
        }
        public String getTitle() {
            return title;
        }
        public void setComments(List<Comment> comments) {
            this.comments = comments;
        }
        public List<Comment> getComments() {
            return comments;
        }
    }
    
    public static class Comment {
        private String title;

        public void setTitle(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }
    }

one can do :

JSONProvider p = new JSONProvider();
/**********************/
p.setSerializeAsArray(true);
p.setArrayKeys(Collections.singletonList("comments"));
/**********************/
Post post = new Post();
post.setTitle("post");
Comment c1 = new Comment();
c1.setTitle("comment1");
Comment c2 = new Comment();
c2.setTitle("comment2");
post.getComments().add(c1);
post.getComments().add(c2);
        
ByteArrayOutputStream os = new ByteArrayOutputStream();
        
p.writeTo(post, (Class)Post.class, Post.class, Post.class.getAnnotations(), 
            MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String,
Object>(), os);

which produces

{"post":{"title":"post","comments":[{"title":"comment1"},{"title":"comment2"}]}}

So, when configuring JSONProvider from Spring, one should set two properties
:
* serializeAsArray = true
* arrayKeys = {"comments"} 

arrayKeys allows to selectively nominate all the fields which need to be
serialized using a [] syntaxis

This kind of configuration is a bit low-level but it can do to get the right
output out 

cheers, Sergey
 



Tong, Gary (IDEAS) wrote:
> 
> I think it's a limitation of the underlying JSON library.  Something like:
> 
> @XmlRootElement
> public class Foo {
>   @XmlElementWrapper(name="values")
>   @XmlElement(name="value")
>   private List<String> values
> }
> 
> Gives XML like:
> 
> <foo>
>   <values>
>     <value>foo</value>
>     <value>bar</value>
>   </values>
> </foo>
> 
> And json like:
> {foo: {values: {value: ["foo", "bar"]}}}
> 
> Whereas really, if this is an API that you want to publicize, you really
> want:
> 
> {values: ["foo", "bar"]}
> 
> Seems like the JSON is generated via JAXB and an XMLStreamWriter, which
> unfortunately is too limited to provide real control over the JSON.
> 
> Thanks,
> Gary
> 
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozk@progress.com] 
> Sent: 10 February 2009 10:48
> To: dev@cxf.apache.org
> Subject: Re: JSON in CXF
> 
> Hi Gary
> 
>> JSON via JAXB definitely leaves something to be desired.
> 
> Do you reckon it's the limitations of the underlying JSON library that we
> use (Jettison) or do you refer to the insufficient number of hooks for our
> JSON JAXRS reader/writer whiich would help in producing a better quality
> JSON ?
> 
> Can you post some examples please - I hope it will help us to improve what
> we have
> 
> Thanks, Sergey
> 
> Hi guys,
> 
> I really like how CXF provides both JSON and XML out of the box.  However,
> after working with the JSON serializer a bit, it's obvious that the JAXB
> annotations translate poorly to JSON, and that while you have great
> control over XML via JAXB, JSON via JAXB definitely leaves something to be
> desired.
> 
> Do you guys know of any jaxb-quality, annotation-driven JSON serializers?
> 
> Cheers,
> Gary
> 
> --------------------------------------------------------------------------
> This is not an offer (or solicitation of an offer) to buy/sell the
> securities/instruments mentioned or an official confirmation.
> Morgan Stanley may deal as principal in or own or act as market maker for
> securities/instruments mentioned or may advise the issuers. This is not
> research and is not from MS Research but it may refer to a research
> analyst/research report. Unless indicated, these views are the author's
> and may differ from those of Morgan Stanley research or others in the
> Firm. We do not represent this is accurate or complete and we may not
> update this. Past performance is not indicative of future returns. For
> additional information, research reports and important disclosures,
> contact me or see https://secure.ms.com/servlet/cls. You should not use
> e-mail to request, authorize or effect the purchase or sale of any
> security or instrument, to send transfer instructions, or to effect any
> other transactions. We cannot guarantee that any such requests received
> via e-mail will be processed in a timely manner. This communication is
> solely for the addressee(s) and may contain confidential information. We
> do not waive confidentiality by mistransmission. Contact me if you do not
> wish to receive these communications. In the UK, this communication is
> directed in the UK to those persons who are professional and eligible
> counterparties (as defined in the UK Financial Services Authority's
> rules).
> 
> 
> --------------------------------------------------------------------------
> This is not an offer (or solicitation of an offer) to buy/sell the
> securities/instruments mentioned or an official confirmation. Morgan
> Stanley may deal as principal in or own or act as market maker for
> securities/instruments mentioned or may advise the issuers. This is not
> research and is not from MS Research but it may refer to a research
> analyst/research report. Unless indicated, these views are the author’s
> and may differ from those of Morgan Stanley research or others in the
> Firm. We do not represent this is accurate or complete and we may not
> update this. Past performance is not indicative of future returns. For
> additional information, research reports and important disclosures,
> contact me or see https://secure.ms.com/servlet/cls. You should not use
> e-mail to request, authorize or effect the purchase or sale of any
> security or instrument, to send transfer instructions, or to effect any
> other transactions. We cannot guarantee that any such requests received
> via e-mail will be processed in a timely manner. This communication is
> solely for the addressee(s) and may contain confidential information. We
> do not waive confidentiality by mistransmission. Contact me if you do not
> wish to receive these communications. In the UK, this communication is
> directed in the UK to those persons who are professional and eligible
> counterparties (as defined in the UK Financial Services Authority’s
> rules).
> 
> 

-- 
View this message in context: http://www.nabble.com/JSON-in-CXF-tp21931013p24737011.html
Sent from the cxf-dev mailing list archive at Nabble.com.


Mime
View raw message