Hi David
On 05/11/13 20:39, KARR, DAVID wrote:
-----Original Message-----
From: KARR, DAVID
Sent: Tuesday, November 05, 2013 12:30 PM
To: Sergey Beryozkin
Cc: [email protected]
Subject: RE: Configure json to give array of foo objects instead of array of
objects with key of "foo" and value of foo object?

-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]]
Sent: Monday, November 04, 2013 2:20 PM
To: KARR, DAVID
Cc: [email protected]
Subject: Re: Configure json to give array of foo objects instead of array
of
objects with key of "foo" and value of foo object?

Hi David

Sure. Suppose you need to have only

"[...]", i.e, we have a pure array going out, with no "{" and "}".

This can be done like this:
JSONProvider<Book> provider = new JSONProvider<Book>();
provider.setSerializeAsArray(true);
provider.setDropRootElement(true);
provider.setDropElementsInXmlStream(false);

Typically dropping the elements is done at CXF (XMLStreamWriter) level,
but this can confuse Jettison when dealing with arrays is also needed.
So the last code line ensures it is done at Jettison level.

You can also do

JSONProvider<ReportDefinition> provider
              = new JSONProvider<ReportDefinition>();
provider.setSerializeAsArray(true);
provider.setArrayKeys(Collections.singletonList("reportDefinition"));
provider.setOutDropElements(Collections.singletonList("reportDefinition"));
provider.setDropElementsInXmlStream(false);

That can affect the arbitrary elements.

Ask your team to give us a bean not resembling the one used in the
production, get them create a bean named Foo, with the list field like
"List<Bar> bars", etc :-), so that we can experiment

Ok, let's say my "ConfigInfo" class has the following:

     @XmlElementWrapper(name = "devices")
     @XmlElement(name = "device")
     private List<Device>    devices;

I've created an instance that only has one element in the devices list.  What
I want to see is the following:

{"configInfo":{"devices":[{"name":"abc","type":"def"}]}}

What I'm currently getting is the following:

{"configInfo":{"devices":[{"device":{"name":"abc","type":"def"}}]}}

I'm first trying to get this working in an integration test, using the
JAXRSServerFactoryBean class, so I'll definitely be manually creating my
JSONProvider objects.

I've tried various things based on what you said, but it's not made a
difference as of yet.

Thanks for the example, I'll experiment tomorrow.

The other thing that I wonder about this is that it seems like this is the sort 
of thing that would be good to put in an annotation on the instance variable, 
just like the various @Xml annotations.  Doesn't that seem reasonable?

I think this would be a Jackson style, Jackson can be quite precise as it works with the annotations too. Jettison is a much simpler library, for now at least. All it has at its disposal during the write time is XMLStreamWriter events & some additional configuration, which is what we can try and experiment with

Cheers, Sergey


On 04/11/13 20:44, KARR, DAVID wrote:
-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]]
Sent: Friday, November 01, 2013 3:39 AM
To: [email protected]; KARR, DAVID
Subject: Re: Configure json to give array of foo objects instead of
array
of
objects with key of "foo" and value of foo object?

Hi David,
On 31/10/13 23:30, KARR, DAVID wrote:
I'm working with some people who had some trouble configuring both xml
and
json output in Jersey, and I wanted to see how their particular problem
could
be resolved in CXF.  I haven't had to deal with the particular problem
they
had, and a quick search of the docs didn't give me an answer.

In short, they were hoping to get a json structure like the following:

{..., [{"prop1": prop1, "prop2": prop2},...], ...}

Where the objects in the array are "Foo" objects.

What they got was this:

{..., [{"foo": {"prop1": prop1, "prop2": prop2}},...], ...}

So instead of the "Foo" object, they got a wrapper object whose key is
"foo", and the value is the "Foo" object.  Is it practical to configure
Jettison in CXF to do this?

I need to see a sample JAXB bean and experiment with it, there are quite
a few ways to affect the Jettison output now, but I'd like to see how
the actual bean.

It is possible now to do only say
"[{"prop1": prop1, "prop2": prop2},...]"

but I'm not sure if having such an array alongside other sibling
properties is possible yet

I'm still waiting for more detail from them on the detailed issues they
were seeing, but in the meantime can you give me some hints on how I make
these kinds of changes, without knowing exactly what we're going to be
doing
yet?




--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to