google / google/gson

Add info about the class of the currently processed object in FieldAttributes so decisions can be made in ExclusionStrategy

Open
#1,272 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

There's an issue with ExclusionStrategy. You can't filter specific fields based on the class currently processed. As is today, feels a bit useless for complex hierarchies - only works for pretty simple models. Say you have these classes:

Shape
int area;

Rectangle extends Shape
int length;
int width;

Square extends Rectangle

Say that now you want to convert the field area only for classes Shape and Rectangle, but you don't want to include it for Square. Because `area` was defined in `Shape`, when calling `field.getDeclaringClass()` you'll get Shape.class. There is no way to know what object is currently processed or what class that object is. So your decisions are pretty limited.

I'm suggesting that object class is included as a property in FieldAttributes. That doesn't break the contract, all previous code will work. It is also logically a field attribute (what class that field is part of) so I think it's logically correct to be there. That addition would enable much better decision logic based on the actual class rather than the class where that Field was declared.

Implementation would be very simple too.

Any concerns about this?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.