JSON / XML unmarshaller problem

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

JSON / XML unmarshaller problem

Jairo1
Hi,
I'm facing with the following issue:

I'm trying to unmarshal the JSON data received from web server using this approach:

****************
ClientResponse responseJ = server.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
//Get the entity of the response
String textEntityJ = (String)responseJ.getEntity(String.class);

JSONJAXBContext contextJson = null;
JSONUnmarshaller unmarshallerJson = null;
MyClass myClassJson = null;

//built JSONConfiguration based on mapped notation
contextJson = new JSONJAXBContext(JSONConfiguration.natural().build(), MyClass.class);
unmarshallerJson = contextJson.createJSONUnmarshaller();//create JSON Unmarshaller

myClassJson = unmarshallerJson.unmarshalFromJSON(new StringReader(textEntityJ), MyClass.class);
***********

Even I get the data in the 'textEntityJ' in the format {"version":["abb.123","xyz.456"]} the unmarshaller returns always MyClass object with empty List ( myClassJson.getVersion().size = 0).

MyClass structure:


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"version"
})
@XmlRootElement(name = "versions")
public class MyClass {

protected List<String> version;


public List<String> getVersion() {
if (version == null) {
version = new ArrayList<String>();
}
return this.version;
}

}


What could be wrong? The same issue I'm facing with XML approach. I receive data (as String) in the XML format, but unmarshaller failes in case when Java bean contains LIST object.

Thanks in advance for your help,
Jairo
Reply | Threaded
Open this post in threaded view
|

Re: JSON / XML unmarshaller problem

japod
Hi Jairo,

your use case seems a bit overcomplicated.

Please see [1] on how to configure Jackson based POJO JSON support
in Jersey (both server and client side should be covered).

Then all you need to do on the client side is:

MyClass v =
server.accept(MediaType.APPLICATION_JSON).get(MyClass.class);


see also [2] for an example.

Does it help?

~Jakub

[1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e894
[2]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.6/jacksonjsonprovider-1.6-project.zip



On 04/13/2011 05:50 PM, Jairo1 wrote:

> Hi,
> I'm facing with the following issue:
>
> I'm trying to unmarshal the JSON data received from web server using this
> approach:
>
> ****************
> ClientResponse responseJ =
> server.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
> //Get the entity of the response
> String textEntityJ = (String)responseJ.getEntity(String.class);
>
> JSONJAXBContext contextJson = null;
> JSONUnmarshaller unmarshallerJson = null;
> MyClass myClassJson = null;
>
> //built JSONConfiguration based on mapped notation
> contextJson = new JSONJAXBContext(JSONConfiguration.natural().build(),
> MyClass.class);
> unmarshallerJson = contextJson.createJSONUnmarshaller();//create JSON
> Unmarshaller
>
> myClassJson = unmarshallerJson.unmarshalFromJSON(new
> StringReader(textEntityJ), MyClass.class);
> ***********
>
> Even I get the data in the 'textEntityJ' in the format
> {"version":["abb.123","xyz.456"]} the unmarshaller returns always MyClass
> object with empty List ( myClassJson.getVersion().size = 0).
>
> MyClass structure:
>
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "", propOrder = {
> "version"
> })
> @XmlRootElement(name = "versions")
> public class MyClass {
>
> protected List version;
>
>
> public List getVersion() {
> if (version == null) {
> version = new ArrayList();
> }
> return this.version;
> }
>
> }
>
>
> What could be wrong? The same issue I'm facing with XML approach. I receive
> data (as String) in the XML format, but unmarshaller failes in case when
> Java bean contains LIST object.
>
> Thanks in advance for your help,
> Jairo
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/JSON-XML-unmarshaller-problem-tp6269059p6269059.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: JSON / XML unmarshaller problem

Jairo1
Hi Jakub,

at first,thanks for reply.

Yes,your suggested JSONConfiguration.FEATURE_POJO_MAPPING is working well. But I'm in situation that I don't know how the server side will be realized.
So will FEATURE_POJO_MAPPING work also for this case ?

I have posted my question because I'm facing the same problem with XML presentation (Client has to support JSON and XML).
Does exist something cute like 'JSONConfiguration.FEATURE_POJO_MAPPING' also for XML to not do overcomplicated unmashalling process ?

I found out that package-info.java of my java client is not rebuilt in the proper way (class is not created using JBuilder 2006). Could this cause my troubles?
Is it possible to replace this java file using other approach with the same effect?

Thanks a lot,
Jairo
Reply | Threaded
Open this post in threaded view
|

Re: JSON / XML unmarshaller problem

Pengfei Di
In reply to this post by japod
Hi Jairo,

if different reponse types are possible, you may use the http header to
parse your ClientResponse.

ClientResponse responseJ = server.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
//Get the entity of the response
if (responseJ..getStatus() = 200)
        MyClass v = responseJ.getEntity(MyClass.class);
else
        MyClass2 u = responseJ.getEntity(MyClass2.class);


Pengfei

On 04/13/2011 04:37 PM, Jakub Podlesak wrote:

> Hi Jairo,
>
> your use case seems a bit overcomplicated.
>
> Please see [1] on how to configure Jackson based POJO JSON support
> in Jersey (both server and client side should be covered).
>
> Then all you need to do on the client side is:
>
> MyClass v =
> server.accept(MediaType.APPLICATION_JSON).get(MyClass.class);
>
>
> see also [2] for an example.
>
> Does it help?
>
> ~Jakub
>
> [1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e894
> [2]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.6/jacksonjsonprovider-1.6-project.zip
>
>
>
> On 04/13/2011 05:50 PM, Jairo1 wrote:
>> Hi,
>> I'm facing with the following issue:
>>
>> I'm trying to unmarshal the JSON data received from web server using this
>> approach:
>>
>> ****************
>> ClientResponse responseJ =
>> server.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
>> //Get the entity of the response
>> String textEntityJ = (String)responseJ.getEntity(String.class);
>>
>> JSONJAXBContext contextJson = null;
>> JSONUnmarshaller unmarshallerJson = null;
>> MyClass myClassJson = null;
>>
>> //built JSONConfiguration based on mapped notation
>> contextJson = new JSONJAXBContext(JSONConfiguration.natural().build(),
>> MyClass.class);
>> unmarshallerJson = contextJson.createJSONUnmarshaller();//create JSON
>> Unmarshaller
>>
>> myClassJson = unmarshallerJson.unmarshalFromJSON(new
>> StringReader(textEntityJ), MyClass.class);
>> ***********
>>
>> Even I get the data in the 'textEntityJ' in the format
>> {"version":["abb.123","xyz.456"]} the unmarshaller returns always MyClass
>> object with empty List ( myClassJson.getVersion().size = 0).
>>
>> MyClass structure:
>>
>>
>> @XmlAccessorType(XmlAccessType.FIELD)
>> @XmlType(name = "", propOrder = {
>> "version"
>> })
>> @XmlRootElement(name = "versions")
>> public class MyClass {
>>
>> protected List version;
>>
>>
>> public List getVersion() {
>> if (version == null) {
>> version = new ArrayList();
>> }
>> return this.version;
>> }
>>
>> }
>>
>>
>> What could be wrong? The same issue I'm facing with XML approach. I receive
>> data (as String) in the XML format, but unmarshaller failes in case when
>> Java bean contains LIST object.
>>
>> Thanks in advance for your help,
>> Jairo
>>
>> --
>> View this message in context: http://jersey.576304.n2.nabble.com/JSON-XML-unmarshaller-problem-tp6269059p6269059.html
>> Sent from the Jersey mailing list archive at Nabble.com.
>>


--
Pengfei Di
Technology

match2blue software development GmbH
Leutragraben 1
07743 Jena

Tel: +49 3641 816 8092
Mobil: +49 1520 166 8691
Fax: +49 3641 573 3479
Email: [hidden email]
Web  : www.match2blue.com
Blog : http://blog.match2blue.com
Registergericht: Amtsgericht Jena
Registernummer: HRB 503726
Geschäftsführerin: Stephanie Renda



Reply | Threaded
Open this post in threaded view
|

Re: JSON / XML unmarshaller problem

Jairo1
In reply to this post by Jairo1
Hi,

I have found out interesting issue:

I'm using Netbeans IDE 6.8 for test REST web server.

When I use the same Netbeans also for REST client, I'm able to unmarshall JSON and XML format properly so I get the correct data:

JSON - MyClass versionsJ = server.accept(MediaType.APPLICATION_JSON).get(MyClass.class);

XML - MyClass versionsX = server.accept(MediaType.APPLICATION_XML).get(MyClass.class);

Only I need to be correct is package-info.java just with correct defined namespace.
I do not even need 'JSONConfiguration.FEATURE_POJO_MAPPING' for JSON to be configured !?


BUT
****

when I use JBuilder 2006 as the client I need to use 'FEATURE_POJO_MAPPING' for JSON approach for corect run. (Overcomplicated approach I descibed yesterday didn't work)
But I'm not able to unmarshal proper XML data at all - ampty List array is received (I assume because JBuilder can't rebuild package-info.java "package-info.java: package annotations should be in file package-info.java"). I used 'overcomplicated' approach and also direct one which worked in NetBeans client.

My questions are:
1.How it is possible in case of NetBeans client that JSON doesn't need FEATURE_POJO_MAPPING to be configured ?
2.Do you have a idea what could be wrong in case of JBuilder 2006 REST client? Why package-info.java can't be compiled?
3.In case of JBulder2006 I get JSON data,but XML not. Why? (remember I'm using List<String> in the annotated bean)

Thanks a lot,
Jaro


Reply | Threaded
Open this post in threaded view
|

Re: JSON / XML unmarshaller problem

Jairo1
My question 2. is partially answered:
I removed package-info.java class from my JBuilder project and added proper 'namespace' to MyClass bean.
This solved my problem with receiveing/unmarshalling correct XML and JSON data to my REST client.

After this change FEATURE_POJO_MAPPING is not necessary to deserialize JSON data in JBuilder client.


But issues asked in 1. and 3. are still mysteries for me.

1.It seems that FEATURE_POJO_MAPPING configuration is not needed in all cases. So when this is required?
2.JBuilder2006 doesn't accept annotation for package-info.java
3.It seems the problem was uncompiled package-info.java in JBuilder2006.  But why JSON format unmarshalled the data and XML approach not for incorrect package-info.java?
Reply | Threaded
Open this post in threaded view
|

Re: JSON / XML unmarshaller problem

japod
Hi Jairo,

FEATURE_POJO_MAPPING simply allows you to do JSON processing
with all Java objects. It is not restricted to JAXB beans only, as opposed
to the default Jersey JSON approach (maintained just for backwards
compatibility reason).

I guess you happen to use only JAXB beans, and then you do not see
the need to use the above feature. On the other hand, if you look
at the JSON generated using different methods (the FEATURE_POJO_MAPPING
feature switched on/off, different Jersey JSON notations), you might see
some differences. You may want to check [1] for some more detailed
information.

Anyway, i would strongly recommend using the Jackson based
FEATURE_POJO_MAPPING feature in Jersey even for JAXB beans,
for it's truly natural Java<->JSON mapping.

HTH,

~Jakub

[1]http://jersey.java.net/nonav/documentation/latest/json.html

On 04/14/2011 03:45 PM, Jairo1 wrote:

> My question 2. is partially answered:
> I removed package-info.java class from my JBuilder project and added proper
> 'namespace' to MyClass bean.
> This solved my problem with receiveing/unmarshalling correct XML and JSON
> data to my REST client.
>
> After this change FEATURE_POJO_MAPPING is not necessary to deserialize JSON
> data in JBuilder client.
>
>
> But issues asked in 1. and 3. are still mysteries for me.
>
> 1.It seems that FEATURE_POJO_MAPPING configuration is not needed in all
> cases. So when this is required?
> 2.JBuilder2006 doesn't accept annotation for package-info.java
> 3.It seems the problem was uncompiled package-info.java in JBuilder2006.
> But why JSON format unmarshalled the data and XML approach not for incorrect
> package-info.java?
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/JSON-XML-unmarshaller-problem-tp6269059p6272844.html
> Sent from the Jersey mailing list archive at Nabble.com.
>