jackson-databind
jackson-databind copied to clipboard
Provide comparator for TreeMap deserialisation
Issue replaces https://github.com/FasterXML/jackson-core/issues/489, as the issue actually belonged here
Both SortedMap and TreeMap can be deserialised, but by default use no special comparator is provided (i.e. natural order is used). This is because the default constructor is called for instantiating the TreeMap. Now you could write special deserializers or subclass TreeMap and provide the comparator there, but that seems to be a pretty heavy change for something as easy as providing a constructor parameter for the value instantiator. Am I overseeing a certain way of how this can be achieved? And if not, could we consider introducing new annotations that allow users to specify which comparator to use for instantiated SortedSets, SortedMaps, etc?
Based on my stackoverflow question: https://stackoverflow.com/questions/48151413/deserialize-a-sortedset-with-a-provided-comparator
Original response from @cowtowncoder
At the moment I don't think there is a way to achieve that for types that are not
Comparablenaturally (likeStrings).I am not sure what'd be the best way; while allowing annotations is a possibility this seems like niche case whereas ideally I think annotations would have wider applicability. So perhaps another configuration mechanism should be used. 2.10 introduces bit more flexible construction method for
ObjectMappers. That would give mechanism for global registration.But regardless, ability to configure this does sound like a good idea.
One minor thing: this actually belongs to
jackson-databind(jackson-coreonly covers streaming parser/generator, not databinding). Could you re-create it there please? (I don't know of github functionality that allows me to do that unfortunately).