> -----Original Message-----
> From: Sergey Beryozkin [mailto:[email protected]]
>
> Given this sample Item and Feature classes :
>
> @XmlRootElement
> public static class Item {
> private String description;
> @XmlElement
> public List<Feature> features = new ArrayList<Feature>();
> private int id;
> private String title;
>
> public void setDescription(String description) {
> this.description = description;
> }
>
> public String getDescription() {
> return description;
> }
>
> public void setId(int id) {
> this.id = id;
> }
>
> public int getId() {
> return id;
> }
>
> public void setTitle(String title) {
> this.title = title;
> }
>
> public String getTitle() {
> return title;
> }
> }
>
> @XmlRootElement
> public static class Feature {
> private int id;
>
> public Feature() {
>
> }
>
> public Feature(int id) {
> this.id = id;
> }
>
> public void setId(int id) {
> this.id = id;
> }
>
> public int getId() {
> return id;
> }
>
> }
>
> The following test :
>
> @Test
> public void testIt2() throws Exception {
> JSONProvider p = new JSONProvider();
> p.setSerializeAsArray(true);
> List<String> keys = new ArrayList<String>();
> //keys.add("features");
> p.setArrayKeys(keys);
>
> Item item = new Item();
> item.setDescription("def");
> item.features.add(new Feature(123));
> item.features.add(new Feature(124));
> item.setId(1);
> item.setTitle("abc");
>
> ByteArrayOutputStream os = new ByteArrayOutputStream();
>
> p.writeTo(item, (Class)Item.class, Item.class,
> Item.class.getAnnotations(),
> MediaType.APPLICATION_JSON_TYPE, new
> MetadataMap<String, Object>(), os);
>
> String str = os.toString();
> System.out.println(str);
> }
>
> produces :
>
>
{"item":{"features":[{"id":123},{"id":124}],"description":"def","id":1,
> "
> title":"abc"}}
>
> (one can probably affect the ordering with JAXB annotations too so
that
> "description" is listed first)
>
> Can you please send me the source for Item and Feature classes ?
I assume your example here is using the default Jettison provider, then?
In my current example, "features" is just a list of Strings, but an
example of a list of non-primitives is also worth exploring.
I believe what you're saying in this example is that I need to set
"serializeAsArray" to true, but leave "arrayKeys" as an empty list
(which makes me wonder what that property actually does).
For reference, here is my current Item class:
@XmlRootElement(name = "Item")
public class Item {
private String id;
private String title;
private String description;
private List<String> features = new ArrayList<String>();
// This combination produces
"<features><feature>val</feature></features>".
@XmlElementWrapper(name = "features")
@XmlElement(name = "feature")
public List<String> getFeatures() { return features; }
}
> In meantime, please see how Jackson provider can be registered :
>
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resou
> r
> ces/jaxrs_jackson_provider/WEB-INF/beans.xml
>
> Will it produce JSON you expect ?
We'll see. If I still have issues with using Jettison, I'll try to move
forward with using Jackson.
Thanks for your help.
> -----Original Message-----
> From: KARR, DAVID (ATTCINW) [mailto:[email protected]]
> Sent: 01 October 2009 18:13
> To: [email protected]
> Subject: RE: How to make clean json, instead of xml-ish json?
>
> > -----Original Message-----
> > From: Sergey Beryozkin [mailto:[email protected]]
> > Sent: Thursday, October 01, 2009 9:42 AM
> > To: [email protected]
> > Subject: Re: How to make clean json, instead of xml-ish json?
> >
> > Hi David
> >
> > sorry for a late reply, I was planning to reply later this evening.
> > Setting a 'features' key should've made a difference but I will try
> > your example just a bit later
> > I'll get back to you asap
> >
> > Apparently a Jackson provider shoud do easily enough.
>
> Thanks. I'll start diving into the Jackson documentation.
>
> > ----- Original Message -----
> > From: "KARR, DAVID (ATTCINW)" <[email protected]>
> > To: <[email protected]>
> > Sent: Thursday, October 01, 2009 5:35 PM
> > Subject: RE: How to make clean json, instead of xml-ish json?
> >
> >
> > > -----Original Message-----
> > > From: KARR, DAVID (ATTCINW)
> > > Sent: Thursday, October 01, 2009 9:28 AM
> > > To: [email protected]
> > > Subject: RE: How to make clean json, instead of xml-ish json?
> > >
> > > > -----Original Message-----
> > > > From: KARR, DAVID (ATTCINW)
> > > >
> > > > I have a REST prototype using CXF that produces reasonable
> looking
> > > XML,
> > > > and almost reasonably looking JSON. The JSON it produces is
> > > "xml-ish".
> > > >
> > > > For instance, the sample XML I get is the following:
> > > >
> > > > <Item>
> > > > <description>def</description>
> > > > <features>
> > > > <feature>123</feature>
> > > > <feature>456</feature>
> > > > </features>
> > > > <id>1</id>
> > > > <title>abc</title>
> > > > </Item>
> > > >
> > > > The JSON is this:
> > > >
> > > >
> > >
> >
>
{"Item":{"description":"def","features":{"feature":[123,456]},"id":1,"t
> > > > i
> > > > tle":"abc"}}
> > > >
> > > > A nicer JSON result would have been this:
> > > >
> > > >
> > >
> >
>
{"Item":{"description":"def","features":[123,456],"id":1,"title":"abc"}
> > > > }
> > > >
> > > > Is it possible to modify just the JSON results, but still have
> the
> > > "out
> > > > of the box" XML results?
> > >
> > > Related to this, I noticed the "Dealing with JSON array
> serialization
> > > issues" section in the user guide. I have a feeling this is
> telling
> > me
> > > that setting "serializeAsArray" and "arrayKeys" in my
JSONProvider.
> > > I'm
> > > building a test for this now. Even if this works, I'd have to say
> > that
> > > this documentation could use a clearer explanation of this,
perhaps
> > > with
> > > more fully-elaborated examples. The documentation for this is
> quite
> > > brief. Full examples showing "before" and "after" would be
useful.
> > > There is a link to another example that supposedly provides more
> > info,
> > > but it only shows the beans.xml file, without any elaboration or
> > > example.
> >
> > My change to "serializeAsArray" and "arrayKeys" in a JSONProvider
> > didn't
> > do anything. The JSON output didn't change.
> >
> > The following are the relevant pieces of my beans.xml file:
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <beans xmlns="http://www.springframework.org/schema/beans"
> > xmlns:util="http://www.springframework.org/schema/util"
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > xmlns:jaxws="http://cxf.apache.org/jaxws"
> > xmlns:jaxrs="http://cxf.apache.org/jaxrs"
> > xmlns:cxf="http://cxf.apache.org/core"
> > xsi:schemaLocation="
> > http://www.springframework.org/schema/beans
> > http://www.springframework.org/schema/beans/spring-beans.xsd
> > http://www.springframework.org/schema/util
> > http://www.springframework.org/schema/util/spring-util-2.0.xsd
> > http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
> > http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
> > http://cxf.apache.org/core http://cxf.apache.org/schemascore.xsd">
> >
> > <import resource="classpath:META-INF/cxf/cxf.xml" />
> > <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"
> > />
> > <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
> > <import
> > resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
> >
> > <jaxrs:server name="restcatalogserver" address="/rest">
> > <jaxrs:features>
> > <cxf:logging/>
> > </jaxrs:features>
> > <jaxrs:providers>
> > <ref bean="jsonProvider"/>
> > </jaxrs:providers>
> >
> > <jaxrs:serviceBeans>
> > <ref bean="catalog"/>
> > </jaxrs:serviceBeans>
> > </jaxrs:server>
> >
> > <bean id="jsonProvider"
> > class="org.apache.cxf.jaxrs.provider.JSONProvider">
> > <property name="serializeAsArray" value="true"/>
> > <property name="arrayKeys">
> > <list>
> > <value>features</value>
> > </list>
> > </property>
> > </bean>
> > <bean id="catalog" class="com.att.ecom.catalog.Catalog">
> > </bean>
> > </beans>