Skip to content

Add @JsonIncludeProperties(propertyNames) (reverse of @JsonIgnoreProperties)Β #1296

Closed

Description

@cowtowncoder

In some/many cases it'd be easier to just name properties to include, reverse of @JsonIgnoreProperties. And similarly, it should be possible to define baselines by type as well as property overrides; and if ever we support inheritance-merging in class hierarchy, use similar merging strategies (merge vs replace).

NOTE: for this (2.12.0) implementation, no merging exists for class annotations -- sub-class definitions will always simply replace super-class/super-interface definitions. Similarly there is no way to specify inclusions using "ConfigOverride" system, although ignoral is possible. Latter would likely be relatively easy to implement (so feel free to file a follow-up request); former (annotation merging) is a more difficult ask (but likewise feel free to file a feature request).

Activity

  1. changed the title [-]Support `@JsonInclude(propertyNames)` if added as annotations[/-] [+]Add `@JsonIncludeProperties(propertyNames)` (reverse of `@JsonIgnoreProperties`)[/+] on Oct 5, 2016
  2. SUGE2016 commented on Aug 3, 2017

    @SUGE2016

    what is the solution? finally

  3. lukaszlenart commented on Dec 14, 2017

    @lukaszlenart

    It would be nice to have something like this where I can allow only a given class type hierarchy to be serializable/deserializable without defining all the properties.

  4. cowtowncoder commented on Dec 14, 2017

    @cowtowncoder
    MemberAuthor

    @lukaszlenart Huh? I am not sure what you mean. Jackson can use fields as properties as well as getters; visibility level needed is configurable and just defaults to public.

  5. lukaszlenart commented on Dec 15, 2017

    @lukaszlenart

    Sorry for that :( What I meant is that I would like to allow serialise/deserialise only given classes, create a whitelist not blacklisting as you do now. Something like we have for XStream where only selected classes can be serialised/deserialised to/from XML.

  6. cowtowncoder commented on Dec 15, 2017

    @cowtowncoder
    MemberAuthor

    Ok. I guess I understand what you are asking, but not necessarily what the benefit would be.
    For common cases, starting with declared root type to deserialize (or type of instance to serialize), then following property types works well. So what would be the benefit of having to whitelist all types accepted? (assuming that would include "well-known" types like String, boolean/Boolean, all number + wrapper types, various Collection, Map types and so forth).

    For what it is worth you could implement something like this by sub-classing isIgnorableType() of [Jackson]AnnotationIntrospector; it is method called to hande @JsonIgnoreType annotation.

    Anyway: assuming idea would seem useful, maybe you could file another issue for it as it is distinct from this one. Implementation could be quite simple -- I assume registering a handler, with "simple" implementation (direct type lookup), would suffice.

  7. lukaszlenart commented on Dec 20, 2017

    @lukaszlenart

    I thought that I have posted an answer but it's gone :(

    Anyway, having a whitelist is far safer than blocking the whole world. This gives more control and allows users precisely filter which classes can serialize/deserialize.

    Your suggestion with overriding [Jackson]AnnotationIntrospector won't work because to keep backward compatibility at some point I have to call super.isIgnoreableType() which allows any type except those marked with @JsonIgnoreType.

    I'm going to open an issue to support whitelisting.

    Thank you for your work and support!

  8. cowtowncoder commented on Dec 20, 2017

    @cowtowncoder
    MemberAuthor

    @lukaszlenart Safer, certainly, but also much more work for common cases where safety is not an issue (caller is trusted).

    But just wrt this issue, I think it makes sense to separate what is requested here (which is not related to filtering by type), and then type restrictions -- while they may sometimes be used for some purpose, I assume more often they are not. There are legit reasons for both. So it's good to have separate issues for mostly separate discussions.

    Thank you for opening the new issue.

  9. lukaszlenart commented on Dec 21, 2017

    @lukaszlenart

    It would be a nice addition for those who need to expose public APIs.

  10. Bas83 commented on Mar 6, 2018

    @Bas83

    Would love this as well.

  11. claytonfbell commented on Mar 24, 2018

    @claytonfbell

    Agree, this would be very useful for exposing public APIs.

  12. cowtowncoder commented on Mar 26, 2018

    @cowtowncoder
    MemberAuthor

    Anyone with an itch is totally free to work on this.
    Would need to go in 3.0 (new annotations not allowed in patch release), in master.

  13. sunphy commented on Apr 2, 2018

    @sunphy

    Yes,@JsonIncludeProperties(propertyNames) is very useful!

  14. nnurmano commented on Apr 12, 2018

    @nnurmano

    I support this feature as well

  15. 16 remaining items

  16. PunKHS commented on Dec 5, 2019

    @PunKHS

    +1

  17. cowtowncoder commented on Dec 5, 2019

    @cowtowncoder
    MemberAuthor

    At this point it has been established that users would find such annotation/property useful; locking comments as further +1s are just noise.

  18. locked as too heated and limited conversation to collaborators on Dec 5, 2019
  19. added
    most-wantedTag to indicate that there is heavy user +1'ing action (5 or more)
    on Apr 7, 2020
  20. cowtowncoder commented on Jul 17, 2020

    @cowtowncoder
    MemberAuthor

    Being worked on as #2771, planning to get included in 2.12.

  21. added this to the 2.12.0 milestone on Jul 24, 2020
  22. cowtowncoder commented on Jul 24, 2020

    @cowtowncoder
    MemberAuthor

    Merged; will do a bit of refactoring on naming and perhaps the way related pieces (ignore vs include sets) are handled. Will be included in 2.12.0.

  23. cowtowncoder commented on Dec 3, 2020

    @cowtowncoder
    MemberAuthor

    Was included in 2.12.0; outlined as one of 5 "Most Wanted" new features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    most-wantedTag to indicate that there is heavy user +1'ing action (5 or more)

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions