users@jersey.java.net

[Jersey] Can I not have dots in keys when deserializing json?

From: Gabriel Falkenberg <gabriel.falkenberg_at_gmail.com>
Date: Thu, 17 Nov 2011 21:29:04 +0100

Hi,

I have a question:

* Should it be possible, using jersey-json, to deserialize JSON containing
objects with keys that have dots in their name such as "com.example" or
"some.string.with.keys"?

I've am not able to do so. Here is a minimal example of what I'm trying to
achieve.

This is the json I use:

{
 "user": {
   "email": "test_at_example.com",
   "some extra param": "some extra value",
   "some.extra.param.with.dots": "some extra value"
 }
}

Please note that if I remove the dots from the third key above everything
works fine unfortunately the json I work with have dots and cannot be
changed. I have mapped the properties I'm interested in using a Java bean
like this:

package com.example.jaxb.json;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "user")
public class UserJson {

  private String email;

  @XmlElement(name = "email", nillable = false)
  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }
}

When I try to deserialize the Json like this:

    JSONJAXBContext context = new
JSONJAXBContext(JSONConfiguration.mappedJettison().build(), UserJson.class);
    JSONUnmarshaller jsonUnmarshaller = context.createJSONUnmarshaller();
     UserJson user =
jsonUnmarshaller.unmarshalFromJSON(inputStreamReaderFromFileName("/invalid-user.json"),
UserJson.class);

I get this NPE:

java.lang.NullPointerException
 at org.codehaus.jettison.Node.getNamespaceURI(Node.java:86)
at
org.codehaus.jettison.AbstractXMLStreamReader.getNamespaceURI(AbstractXMLStreamReader.java:133)
 at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:237)
at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:181)
 at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:366)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:338)
 at
com.sun.jersey.json.impl.JSONUnmarshallerImpl.unmarshal(JSONUnmarshallerImpl.java:113)
at
com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:99)
 at
com.example.JsonTest.shouldDeserializeJsonWithDotsInKey(JsonTest.java:39)

I've tried to upgrade to the latest version of jersey-json (1.10 as of this
writing) and I've also tried to override the version
of com.sun.xml.bind:jaxb-impl to the latest version (2.2.4) but the result
is the same.

In the example I'm only mapping one property of the User class. Extra
attributes in the json causes no problem unless they contain dots. I've
tried mapping the key with dots using @XmlElement(name = "
some.extra.param.with.dots", nillable = true) but with the same result.

I've looked at the source code for the files mentioned in the stack trace
but I'm afraid I'm to unfamiliar with the code to understand what the
problem is.

If anyone could give me any sort of pointer I'd be very grateful.

Best Regards,
Gabriel Falkenberg