I'm using com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString() to generate JSON from Java POJOs.
One of my objects - lets call it ResultSheet - contains a property Map<ResultId,Integer> results. ResultId represents a composite PK (of two FKs).
It appears that ObjectMapper upon encountering ResultId realizes that this can't be represented in JSON directly because JSON map keys have to be strings. It is therefore outputing the toString() of ResultId instead. (In this particular case this produces a string in the form "42:43" but only because this happens to be how toString() is currently implemented.)
How can I stop it doing this? The current behavour is hiding the fact that the structure being marshalled is not properly representable as a JSON structure. It is also leaking the current toString() implementation into the API of my web service.
I would rather have an exception thrown telling me when a value used as a map key is not a String - or, at least, when it is not trivial to convert to a String. (I'm not so concerned about Long keys being toString()-ed, for example.)
I've looked into the SerializationFeature settings but none of them seem to address this issue. https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/SerializationFeature.html
Is there are way to prevent this? Can it be done in a general way i.e. not just for this specific class but in any case where this might arise on future, as new classes, some of which might be used as map keys, are added?
Note, making toString() throw an exception would not be an acceptable hack in this instance.