Skip to content

Conversation

hgwood
Copy link
Contributor

@hgwood hgwood commented Dec 20, 2015

This is follow-up to #1010.

This implementation proposed in #1010 is incomplete. This is due to the test case not being sufficient. Here's the test case from #1010:

    public class ArrayOrObject {
        private final List<Object> objects;
        private final Object object;

        @JsonCreator public ArrayOrObject(List<Object> objects) {
            this.objects = objects;
            this.object = null;
        }

        @JsonCreator public ArrayOrObject(Object object) {
            this.objects = null;
            this.object = object;
        }
    }

The problem is that, to my surprise, a JSON array is deserializable into an instance of Object, so this test passes if the deserialization of both {} and [] go through the second creator, which is obviously not what I expect. Therefore, I have changed the test to the following:

    public class SomeObject {
        public String someField;
    }

    public class ArrayOrObject {
        private final List<SomeObject> objects;
        private final SomeObject object;

        @JsonCreator public ArrayOrObject(List<SomeObject> objects) {
            this.objects = objects;
            this.object = null;
        }

        @JsonCreator public ArrayOrObject(SomeObject object) {
            this.objects = null;
            this.object = object;
        }
    }

Now Jackson refuses to deserialize arrays into instances of SomeObject. Starting from there I completed the implementation so that the new test case passes.

@cowtowncoder cowtowncoder added this to the 2.7.0-rc3 milestone Dec 21, 2015
@cowtowncoder
Copy link
Member

Ah, yes, java.lang.Object is indeed the "wild card", capable of being bound from any and all JSON constructs.

Things may get even dicier with polymorphic types, as Type Id may be WRAPPER_ARRAY, in which case JSON Array is used for all kinds of Java types, including scalars.
But perhaps that is not problematic.

Thank you for further testing & fix, will merge.

cowtowncoder added a commit that referenced this pull request Dec 21, 2015
Fixed array delegate creator not being fully functional
@cowtowncoder cowtowncoder merged commit ddef782 into FasterXML:master Dec 21, 2015
@hgwood
Copy link
Contributor Author

hgwood commented Dec 22, 2015

Thanks for those tips. I'll try to write further tests to demonstrate if WRAPPER_ARRAY is a problem or not.

@hgwood hgwood deleted the array_obj_fix branch December 22, 2015 09:12
@cowtowncoder
Copy link
Member

@hgwood Thank you in advance!

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

Successfully merging this pull request may close these issues.

2 participants