users@jersey.java.net

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

From: Gabriel Falkenberg <gabriel.falkenberg_at_gmail.com>
Date: Sun, 20 Nov 2011 13:54:11 +0100

Thanks Jakub, using Jackson solved the problem perfectly!

Best Regards,
Gabriel Falkenberg


On Fri, Nov 18, 2011 at 3:35 PM, Jakub Podlesak
<jakub.podlesak_at_oracle.com>wrote:

> **
> Hi Gabriel,
>
> You can switch to using the Jackson based JSON POJO support in Jersey (see
> [1]).
> Jackson (see [2]) then provides the @JsonProperty annotation to specify
> your desired
> JSON property name (including names containing dots, etc.).
>
> I have not tested, but the following should then (after you switch the
> POJO feature on)
> work for you:
>
> // no JAXB annotation needed here unless you need XML as well as JSON
> public class UserJson {
> public String email;
>
> @JsonProperty("some extra param")
> public String firstExtraParam;
>
> @JsonProperty("some.extra.param")
> public String theOtherExtraParam;
> }
>
> HTH,
>
> ~Jakub
>
> [1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e889
> [2]http://jackson.codehaus.com/
>
>
> On 17.11.2011 21:29, Gabriel Falkenberg wrote:
>
> 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
>
>