How to map GHResponse from JSON

After How to map GHRequest from JSON? - #5 by LWeber I have another question regarding GHResponse mapping. I expected the following code resulting in a valid GHResponse object.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

GHResponse response = hopper.route(request); // -> a correct response
String serializedResponse = objectMapper.writeValueAsString(response); // -> this looks fine
GHResponse deserializedResponse = objectMapper.readValue(serializedResponse, GHResponse.class); // -> error

However, I get an

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of "com.graphhopper.util.InstructionList" (no Creators, like default construct, exist): no default no-arguments constructor found

error. Is this a bug or can you point me to a workaround?

Thank you very much!

See our client-hc module on how we read the response into Java objects and also my comment in the other thread.

Hello @karussell,

thanks for your response! I was able to get the client-hc part working as expected :slight_smile:

So I can know serialize a GHRequest into a String (GraphHopperWeb#requestToJson) and deserialize a String into a GHResponse (part of GraphHopperWeb#route).

To make the full circle can you please tell me where in the repo (probably at server side) I find the code which

  1. deserialize the incoming String into a GHRequest object
  2. (performs the routing, this part works for me)
  3. serializes the resulting GHResponse into a String

(feel free to replace String with byte[] or whatever you’re using in your implementation)

Jackson is called from jersey/dropwizard. There is no explicit server-side code in GraphHopper that does these conversions, only the (de)serializers that you found.

See the deserialization on the client side:

Yeah I just found how ResponsePathSerializer.jsonObject(...) is called in which helped me to understand how you use the (de)serializers to convert GHResponse to JSON :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.