Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TokenFilters to keep empty arrays and objects #715

Closed
nik9000 opened this issue Sep 10, 2021 · 4 comments · Fixed by #729
Closed

Allow TokenFilters to keep empty arrays and objects #715

nik9000 opened this issue Sep 10, 2021 · 4 comments · Fixed by #729
Labels
2.14 Issue planned (at earliest) for 2.14
Milestone

Comments

@nik9000
Copy link
Contributor

nik9000 commented Sep 10, 2021

Include version information for Jackson version you use: We use 2.10.4 but 2.13 doesn't support this either

Maybe this is possible in a way I don't know about, but I was hoping I could write a TokenFilter that could preserve empty arrays and objects. It looks like now if a TokenFilter#includeProperty doesn't return INCLUDE_ALL for an empty array then the array is removed. I'd love it if the TokenFilter could make that choice- maybe something like adding this to TokenFilter:

    public boolean includeEmptyArray(boolean contentsFiltered) throws IOException {
        return false;
    }

There is already a filterFinishArray but I don't think I can control the underlying filter with it. And I'm not sure if I can tell if the filter has filtered the contents of the array or not.

@nik9000
Copy link
Contributor Author

nik9000 commented Sep 10, 2021

Something like this seems to work, but I really don't know the code base so i can't be sure it makes sense.

I see INCLUDE_NON_NULL which is new to me but I don't think it does what I want. It looks like it preserves the path to filtered stuff and I don't want that.

@cowtowncoder
Copy link
Member

One quick note: is this related to/same as #708 ? If so I wish I had time to work on it; help would be appreciated.
Although time-wise might be getting too late to address for 2.13.

@nik9000
Copy link
Contributor Author

nik9000 commented Sep 11, 2021

is this related to/same as #708 ?

I had another look this morning and I think so. I'll try and open up a PR soon. I think the code that I linked in the gist works fine for the builder side. Filtering the parsers are a little wow. I'll keep poking at it. There's a whole giant switch statement I haven't looked at yet.

@cowtowncoder
Copy link
Member

Yes, the code is.... scary. It violates my usual "never write as clever code as you can since the person debugging code must be twice as clever as the person who wrote it (hence write only half as clever you could)".

nik9000 added a commit to nik9000/jackson-core that referenced this issue Sep 12, 2021
This creates two new method on `TokenFilter` which you can override to
decide if empty arrays and objects should be included or excluded. An
override like this, for example, will include all arrays and objects
that were sent empty but strip any arrays or objects that were
*filtered* to be empty:
```
        @OverRide
        public boolean includeEmptyArray(boolean contentsFiltered) {
            return !contentsFiltered;
        }

        @OverRide
        public boolean includeEmptyObject(boolean contentsFiltered) {
            return !contentsFiltered;
        }
```

The default to preserve backwards compatibility is to always *exclude*
empty objects.

Closes FasterXML#715
nik9000 added a commit to nik9000/jackson-core that referenced this issue Dec 16, 2021
This creates two new method on `TokenFilter` which you can override to
decide if empty arrays and objects should be included or excluded. An
override like this, for example, will include all arrays and objects
that were sent empty but strip any arrays or objects that were
*filtered* to be empty:
```
        @OverRide
        public boolean includeEmptyArray(boolean contentsFiltered) {
            return !contentsFiltered;
        }

        @OverRide
        public boolean includeEmptyObject(boolean contentsFiltered) {
            return !contentsFiltered;
        }
```

The default to preserve backwards compatibility is to always *exclude*
empty objects.

Closes FasterXML#715
cowtowncoder pushed a commit that referenced this issue Jan 3, 2022
This creates two new method on `TokenFilter` which you can override to
decide if empty arrays and objects should be included or excluded. 

Closes #715
@cowtowncoder cowtowncoder added this to the 2.14.0 milestone Jan 3, 2022
@cowtowncoder cowtowncoder added the 2.14 Issue planned (at earliest) for 2.14 label Jan 3, 2022
cowtowncoder added a commit that referenced this issue Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.14 Issue planned (at earliest) for 2.14
Projects
None yet
2 participants