Skip to content

Allow specifying custom exclusion comparator via @JsonInclude, using JsonInclude.Include.CUSTOMย #888

@cowtowncoder

Description

@cowtowncoder

(note, possible related to #885. Also note that this supercedes #558)

Choices used with @JsonInclude are currently somewhat limited, and specifically require either hard-coded criteria (null, or default property values of a POJO) or support from JsonSerializer for type (for "empty" check via `JsonSerializer.isEmpty(...)).
This makes it difficult to have custom criteria for exclusion without using other mechanisms like JSON Views or Json filter.

But it would seem possible to instead allow use of some kind of custom object that is called to determine whether given value is to be included/excluded.
While there are multiple possible ways to go about with this -- specifically, whether to create a new interface for comparing -- but one very simple way would be to simply use Object.equals(); method of comparator, and use return value to indicate inclusion (if true returned) vs exclusion (false).

The only other thing needed would seem to be to modify @JsonInclude so that:

  1. There is a way specify the Class of custom comparator (default of Void.class to indicate "no custom comparator")
  2. (optional) Add yet another JsonInclude.Include enum choice of CUSTOM
  3. Also probably need to implement Jackson 2.7: use AnnotationIntrospector.findPropertyInclusion()ย #839

Of these, (2) is optional: we could choose to always check custom comparator, and if one defined, ignore enum. But from readability stand-point it seems better to add a new choice.

Also: for maximum power, we could choose to instead require implementation of some special interface, through which we could pass context (SerializerProvider). While this would be powerful, it seems that perhaps it is better not get carried away at this point: given that there are more powerful mechanisms for doing that (JSON Filter), we may want to keep this feature simple.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions