-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Use @JsonSetter(nulls=...)
to specify handling of null
values during deserialization
#1402
Comments
Is there any ongoing progress to be able to deserialize |
@raindev This would be option (3), although it may be that most users would prefer initializing empty collection values in constructor, and opt for simple skip (1) instead. Either way could solve the null problem; former would require bit more complex handling (deserializer implementation would need to know how to produce default value; and it may or may not be easily accessible). |
@cowtowncoder, but as I understand it is not implemented yet, right? Can I help, if it is not? |
@raindev It's very high on my todo list, but correct, it is not yet implemented. Help is always welcome, although in this case I do have reasonable idea of how to proceed: basically it would use approach somewhat similar to #1399, modifying For what it is worth, annotation side has already been added, so information is already available during construction of So... if you want to have a look, feel free to: I rarely keep much uncommitted code locally, so everything implemented by me so far is in |
Completed finally! @raindev Feature is now complete to the degree I planned to implement it. |
@cowtowncoder, that's great news, many thanks! |
@raindev That's ok -- I know the feeling. I hope you get a chance to try out the feature at least. |
@cowtowncoder Does this handle I don't think that's the case on the latest snapshot, but this project I'm using it on is such a cobbled together mess that I can't be sure of anything. Also, any plans to make this a global setting? I'd rather not annotate all of my fields with JsonSetter. |
@TheKeeperOfPie As to global setting, yes, that is already supported. There are three levels:
and those are tested to some degree in unit tests. As to empty String... unfortunately empty String is not Could you please file another issue for this, so I won't forget? You can add a ref to this issue as background. |
Can anyone recommend any workaround until this issue is released in v2.9.0? |
@ryanprayogo This is a new feature so I am not quite sure what you mean by work around... but if I had to guess the traditional thing to do has been to add a setter method to do whatever conversion or skipping of |
Hi, Does it works with xml deserialisation? We would like to deserialise @XmlElement(name = "tags")
@JsonSetter(nulls = Nulls.SKIP)
private Tags tags = new Tags(); And Tags class look like: @Data
public class Tags {
List<Tag> tags = new ArrayList<>();
} but it's stay to |
@allienna The feature itself should work with all formats as this is on databind level, not at streaming API. One concern I have here is that since |
Is there a way to make it work - deserialize Ideally I would like to set up
Help appreciated. |
@maciejwalkowiak What does "missing key" mean? That the property is not included? If so, no, that will not work -- missing property is NOT the same as one with As to constructor properties: handling should work. If not, a new issue with reproduction should be filed -- it is easier to track smaller flaws (or missing functionality) that way. |
Exactly - missing key meaning that the key is not present in JSON. In some cases it means something different, in some cases deserializing it to empty collection makes absolute sense - this is why it would be useful on the annotation level. I will file separate issue with constructor properties. |
@maciejwalkowiak Understood: missing information is important. Unfortunately its handling is... quite different, from impl perspective. But this is a known gap, and I hope it can be addressed. At this point it is only really handled in context of Creator properties, where some checking is possible. |
Reading through this issue it seems the only way to deserialize null arrays as empty collections is via the Is there not a way to apply this to an object mapper as a deserialization feature? |
@Ramblurr you can define per-type and global config overrides for |
Is there any way to do this at class level? |
@Tharsalbird please use mailing lists for usage questions in future. But I think what you are asking is possible by using |
Hi, I've noticed a behavior I think should be different with in that case, I would expect that if there is an unknown enum the setter will skip the null set, as I told him I don't want you to set null. but what is probably happening is that it sees a value which is not null so it tries to set it, then when it can't set it, it becomes null. I realize that it will be probably hard to fix, but on the other hand, the whole point of the nulls attribute is to do something else with nulls... |
@ndori Could you please file a new issue, explaining the problem as above. And if possible with really simple test case? It sounds like a flaw and I would like to keep track of it separately; you can add a reference to this issue for background. |
Still not great, no way to use custom defaults other than overriding |
(note: replaces #1269, earlier #988 and #830)
It looks like there is strong desire to define alternate methods for handling incoming
null
values; instead of just assigning as Javanull
s to:Since applicability is probably not universal for all cases (that is, different types could well follow different rules), it seems that approach similar to
@JsonInclude
would work.However, I don't think use of
@JsonInclude
itself works here, as its settings are partially output-specific; so ideas are reusable, implementation not.Instead, I think use of existing
@JsonSetter
by adding:Nulls
value enumeration for a small set of standard behaviors (and perhaps, in future, for custom handler?)nulls
property for dealing with value itself (POJO, Collection, wrapper type)contentNulls
for contents of structured types: array/collection elements, Map values, POJO propertiesChanges to annotation are simple; changes to deserializers would be sizable.
The text was updated successfully, but these errors were encountered: