0

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.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
the.wizard
  • 1,079
  • 1
  • 9
  • 25

1 Answers1

0

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.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • I using jackson as the json marshaller, I think it was a good one. Do you have any other recommendation? – the.wizard Jun 24 '15 at 14:30
  • IIRC, XStream (which supports JSON too) doesn't run into the stackoverflow exception, but the dataset still becomes huge due to all the indentation. A better is way to go for a custom converter (see edited answer). – Geoffrey De Smet Jun 25 '15 at 07:48