Unexpected end-of-input: expected close marker for OBJECT
Asked Answered
S

2

7

I'm trying to create a geopoint in my app engine application but when I try to deserialize it I got this annoying message:

Uncaught exception from servlet
org.codehaus.jackson.JsonParseException: Unexpected end-of-input: expected close marker for OBJECT (from [Source: java.io.StringReader@1a21658; line: 1, column: 0]).

This is my JSON code:

{
    "id": 31,
    "name": "pepe",
    "mail": "[email protected]",
    "password": 123,
    "age":10,
    "birthday": "01-06-1991",
    "desc" : " bla bla",
    "gp": {
     "latitude": 64.124596,
     "longitude": -147.8632
     }
}

and this is the declaration of geopoint and my custom deserialize method:

GeoPoint gp;

public GeoPoint getGp() {
    return gp;
}

@JsonDeserialize(using = CustomGeoPintDeserializer.class)
public void setGp(GeoPoint gp) {
    this.gp = gp;
}

public  class CustomGeoPintDeserializer extends JsonDeserializer<GeoPoint> {

    @Override
    public GeoPoint deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

        System.out.println("ENTRA");
        ObjectMapper mapper = new ObjectMapper();
        JsonNode actualObj = mapper.readValue(jsonParser.getText(), JsonNode.class);

        double latitude = actualObj.get("latitude").getDoubleValue();

        double longitude = actualObj.get("longitude").getDoubleValue();


        return new GeoPoint(latitude,longitude);
    }
}
Shannonshanny answered 27/8, 2014 at 11:59 Comment(1)
How GeoPoint class looks like? What is the package of this class?Segregation
I
2

It looks like your GeoPoint class has constructor like this: public GeoPoint(double latitude, double longitude). In this case we can use MixIn feature. We have to create additional abstract class in which we will provide mapping between your POJO and JSON.

abstract class GeoPointMixIn {

    GeoPointMixIn(@JsonProperty("latitude") double latitude, @JsonProperty("longitude") double longitude) {

    }
}

And simple usage:

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(GeoPoint.class, GeoPointMixIn.class);

System.out.println(mapper.readValue(JSON, YourRootPojo.class));

As you can see you do not have to implement custom deserializer for GeoPoint class.

Ilk answered 27/8, 2014 at 12:59 Comment(2)
which version of jackson do I need to use mapper.addMixInAnnotations?Marnimarnia
I was using version 2.4.1 (fasterxml) but I think that this method exists since version 2.0.0.Segregation
T
2

For me it was much simple solution.

I forgot to close brackets in my SwaggerUI request like that:

{
    "jobGroup": "Authorizer-CCC",
    "jobName": "Authorizer-2NO5-101"
Tented answered 28/4, 2017 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.