users@jersey.java.net

Re: [Jersey] JAXB JSON Marshalling/Unmarshalling

From: Ni Yue <ni2yue4_at_gmail.com>
Date: Fri, 22 May 2009 18:01:15 +0800

Jakub,

Thanks for your quick reply.
I checked out the sample link you send but still can not figure out what's
the problem. The only difference is I used mapped jettison notation in
JAXBContentResolver, and add a namespaceMap:

public JAXBContextResolver() throws Exception {
> Map<String, String> namespaceMap = new HashMap<String, String>();
> namespaceMap.put("http://www.w3.org/2001/XMLSchema", "xs");
> namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance",
> "xsi");
> namespaceMap.put("http://core.example.com", "core");
> JSONConfiguration config =
> JSONConfiguration.mappedJettison().xml2JsonNs(namespaceMap).build();
> this.m_context = new JSONJAXBContext(
> config,
> m_types);
> }
>

I am not sure I miss something or not. I attached Jersey's source code,
debugged and found even if I set mappedJettison configuration in
JAXBContextResolver, in JSONRootElementProvider, the Marshaller in writeTo
method is not instance of JSONMarshaller, and it will use
JsonXmlStreamWriter to produce mapped notation.

> @Override
> protected void writeTo(Object t, MediaType mediaType, Charset c,
> Marshaller m, OutputStream entityStream)
> throws JAXBException, IOException {
> if (m instanceof JSONMarshaller) {
> JSONMarshaller jm = (JSONMarshaller)m;
> jm.setJsonEnabled(true);
> jm.marshal(t,
> new OutputStreamWriter(entityStream, c));
> } else {
> m.marshal(t, JsonXmlStreamWriter.createWriter(
> new OutputStreamWriter(entityStream, c), true));
> }
> }


Any ideas? Thanks.

2009/5/22 Jakub Podlesak <Jakub.Podlesak_at_sun.com>

> On Fri, May 22, 2009 at 04:54:58PM +0800, Ni Yue wrote:
> > Hello everyone,
> >
> > I am using Jersey to serve JSON request/response and met some issue.
> Because
> > in my xml schema, there're multiple namespaces, and I would like to use
> > mapped jettison notation to produce JSON response. But it seems mapped
> > notation is always used during marshalling to JSON even if I specified
> > mapped jettison configuration in my JAXBContextResolver. How can I make
> > Jersey to use different notation during producing JSON response? I found
>
> The mechanism should work fine on the server side. See [1] for an example.
> For the client side you need to explicitly register the context resover,
> but i suspect, it is not your case, right?
>
> Anyway, without seeing your code, it is hard to say, what is wrong.
> Feel free to send me a private e-mail revealing more details, so that
> i can help you.
>
> Thanks,
>
> ~Jakub
>
> [1]
> http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.1.0-ea/json-from-jaxb-1.1.0-ea-project.zip
>
> > only when unmarshalling JSON request, different JSON unmarshaller will be
> > pickuped according to notation. I am using Jersey 1.0.3. Thanks in
> advance.
> >
> > Regards,
> > Samuel Ni
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>