openplans/OpenTripPlanner — Issue #532: odd arrays in json response
Author: andrewbyrd
Created: 2011-10-10 09:14:00+00:00
State: open
Labels: api, webapp

When benchmarking OTP I want to parse each response to count the number of itineraries it includes. There are two things that make this more complicated than it should be:

1. The list of itineraries is serialized as a JSON array of objects when it contains more than one itinerary, but as a JSON object when it contains only one itinerary.

2. When it is serialized as an array, the 'itineraries' key is associated with an object containing a single 'itinerary' key, which in turn maps to an array of itineraries.

"itineraries": { "itinerary" : { the elements of a single itinerary } }
vs
"itineraries": { "itinerary" : [{...},{...},{...}] }

see:
http://stackoverflow.com/questions/5641430/jaxb-single-element-in-array
http://stackoverflow.com/questions/5801766/json-data-problem
http://stackoverflow.com/questions/3946102/jaxb-json-force-brackets-on-arrays
http://stackoverflow.com/questions/2199453/how-can-i-customize-serialization-of-a-list-of-jaxb-objects-to-json

Apparently this is considered a feature, and the solution is to switch to another JSON library (Jackson) that does what you would expect.

I just set the accept header to application/xml and parse that for now, but we may want to address this eventually.