How to map GHRequest from JSON?

In GHRequest it is documented that it’s possible to deserialize a JSON to GHRequest with Jackson. I also saw that there is an Can you explain why a GHRequestDeserializer is missing?

I would like to map a JSON request like this (probably wrong syntax) to a valid GHRequest object

  "points": [
    [1.111, 2.222],
    [3.333, 4.444]
  "profile": "car"

Is this possible? Are there tests for this behavior? If my approach is the intended use of your library and tests are missing, should I add a test?

This is done from Jackson automatically, there is no need for a GHRequestDeserializer.

There are no tests necessary as it is also implicitly tested whenever a POST request is send to a resource. I think we only have GHPointDeserializer that helps during a request deserialization to use the reverse array order.

(In master we additionally need to help jackson via a CustomModelAreasDeserializer to stay backward compatible)

Thanks for your feedback!

It’s probably my lack of experience with Jackson but trying this:

GHRequest request = new GHRequest(new GHPoint(53.33918, -6.22016), new GHPoint(53.33914, -6.21668));
ObjectMapper mapper = new ObjectMapper();
String serializedGHRequest = mapper.writeValueAsString(request);
GHRequest deserializedGHRequest = mapper.readValue(serializedGHRequest, GHRequest.class);

Gives me an

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "valid" (class com.graphhopper.util.shapes.GHPoint), not marked as ignorable (2 known properties: "lon", "lat"])

The serializedGHRequest looks like this:


ok sorry indeed it was my lack of jackson knowledge. I can just add

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

If you write the object to string you need to register the serializers. If you want to read the object from the string you need to register the deserializers (you can read about it in some jackson docs). Have a look into our class com.graphhopper.jackson.Jackson on the serializers and deserializers we use.

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

You’ll notice that the points field will be missing without the proper deserializer (I think). Please note that our (de)serializer expects a different format ala
points:[[lng,lat], ...]

Thank you for your response and your patience.

I begin to understand how Jackson serializers and deserializers work and how you use them.

I now use your ObjectMapper

ObjectMapper objectMapper = Jackson.newObjectMapper();

and serializing a GHRequest using GraphHopperWeb#requestToJson works fine in combination with deserializing using

objectMapper.readValue(serializedGHRequest, GHRequest.class);

I still get an error when deserializing a GHRequest that was serialized using

String serializedGHRequest = objectMapper.writeValueAsString(request);

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "empty" (class com.graphhopper.util.PMap), not marked as ignorable (0 known properties: ]) at [Source: (String)"{"points":[[-6.22016,53.33918],[-6.21668,53.33914]],"profile":"car","hints":{"empty":true},"headings":[],"point_hints":[],"curbsides":[],"snap_preventions":[],"locale":"en_US","algorithm":"","details":[]}"; line: 1, column: 90] (through reference chain: com.graphhopper.GHRequest["hints"]->com.graphhopper.util.PMap["empty"])

@karussell any feedback if this is a bug or just wrong usage from my side?