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

Support deserializing [] as null or empty collection when the java type is a not an object #540

Closed
fabienrenaud opened this issue Sep 12, 2014 · 8 comments
Milestone

Comments

@fabienrenaud
Copy link

In PHP, it is possible to associate arrays. Those associates arrays can then be json encoded and turn to be JSON objects.
But when the associate array is empty, for various processing reasons, json_encode won't be able to tell that this particular empty associate array is an object, so it encodes it as [].
Then all hell breaks in Java because deserialize [] into a non array class doesn't work.

Task: Support that such as it deserialize to null when the java type is an non array object, and as empty collection when the java type is a collection.

@cowtowncoder
Copy link
Member

So, would it be correct to say that for Maps, and perhaps POJOs, following JSON:

[ ]

should behave as a special case that would be equivalent to

{ }

If so, this could make sense, and could perhaps be added as a feature like, say, DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_EMPTY_OBJECT.
This is bit tricky for couple of reasons (Type Id handling, for example, can use array wrapping; and detection of non-empty arrays in streaming manner needs care); but it might be doable otherwise.

@fabienrenaud
Copy link
Author

Maps and pojos are expected to be represented as objects in JSON. But in case they are empty, PHP may say

[]

So, yes, the proposed option makes sense :)
"EMPTY_OBJECT" thought would require some more information about how it is handle for different types... For any non array objects and non collection java type, i would use null. For collection java types, i would use the matching empty collection.

@cowtowncoder
Copy link
Member

For collection objects that would already work. I guess use of null might be safest option for POJOs.

I loathe PHP. This kind of nonsense reminds me of why.

@fabienrenaud
Copy link
Author

Do you think it could be supported in v2.5?
No pressure here, I just wonder when I should plan to stop hacking around that "bug" in my code :)

@cowtowncoder
Copy link
Member

@fabienrenaud Yes I think so; I think that this is both high priority and reasonable to implement. But if latter turns out to be false I will add an update. So, yes, I hope to get this in by 2.5.

cowtowncoder added a commit that referenced this issue Sep 26, 2014
cowtowncoder added a commit that referenced this issue Sep 26, 2014
@cowtowncoder cowtowncoder added this to the 2.5.0 milestone Sep 26, 2014
@cowtowncoder
Copy link
Member

Turns out it was quite easy, so it's done.

@fabienrenaud
Copy link
Author

Awesome :)
Any idea when 2.5 will be in maven repos?

@cowtowncoder
Copy link
Member

2.5.0 has not been released, and there is no date set for starting with release candidates. So I think it will take a while. But perhaps it could get released in October.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants