JsonMappingException Infinite recursion When Trying To Convert Vehicle Routing Solution to Json

491 views Asked by At

I was trying to convert vehicle routing solution object that has been processed by optaplanner solver to json string, but whenever I tried it, it keep prompt me this error:

Stack Trace:

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: app.chameleon.marketing.salesman.routing.optimization.server.model.VehicleRoutingSolution["customerList"]->java.util.ArrayList[0]->app.chameleon.marketing.salesman.routing.optimization.client.model.Customer["nextCustomer"]->

...

app.chameleon.marketing.salesman.routing.optimization.client.model.Customer["previousStandstill"]) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:653)

...

Caused by: java.lang.StackOverflowError

I have tried to use @JsonManagedReference and @JsonBackReference, but I can't figured it out how to use it. Since every time I used it, some members of Customer class didn't converted to json (e.g. previousStandstill or nextCustomer).

If anyone experienced this issue, please share your solution. Any comments will be appreciated. Thank you.

1

There are 1 answers

2
Geoffrey De Smet On

StackOverflowError means the VRP dataset has too many levels of nested elements for JSON serialization with that marshaller, probably because the JSON marshaller is written poorly (because it uses recursion instead of looping to deal with nested elements, so it can not handle more than 1024 nested levels).

On the other hand, one can also argue that the VRP data structure isn't really XML/JSON mapping friendly, because it has a custom linked list (and every element in that linked list ends up as another nesting in the XML/JSON dataset). A custom converter (probably copied from java.util.LinkedList's converter) can flatten that list to avoid the nesting.