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

JSON.Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS does not work #27

Closed
gpiancastelli opened this issue Oct 20, 2015 · 7 comments
Closed

JSON.Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS does not work #27

gpiancastelli opened this issue Oct 20, 2015 · 7 comments
Milestone

Comments

@gpiancastelli
Copy link

The following code, called on Jackson Jr. 2.6.3

JSON.std.with(JSON.Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS)
        .arrayOfFrom(Object.class, "[]");

crashes with the following exception

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Object[]
    at com.fasterxml.jackson.jr.ob.impl.JSONReader.readArrayOf(JSONReader.java)
    at com.fasterxml.jackson.jr.ob.JSON.arrayOfFrom(JSON.java)

However, if I remove the feature and ask Jackson Jr. to read a list from the JSON array by using listOfFrom, everything works fine.

Since even calling arrayOfFrom without the feature does not work (which I presume is due to JSON arrays indeed being read as Java Lists... but, then, what's the purpose of the arrayOfFrom method?), how else I am supposed to make Jackson Jr. return me a Java array from a JSON array?

@cowtowncoder
Copy link
Member

Thank you for reporting this: it sounds like a bug and your code should work as you expect.

@cowtowncoder
Copy link
Member

Ok, looking at the unit test, usage that is tested is:

    ob = JSON.std.with(Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS).anyFrom(INPUT);
    // gives Object[]

so a possible short-term work-around would be to use anyFrom().

This just as a sidenote; will try to resolve the actual problem next.

@cowtowncoder
Copy link
Member

Hmmh. Interesting. Code works just fine with non-empty JSON Array -- problem only occurs with empty one.

@gpiancastelli
Copy link
Author

Well, I had the same problem with a non-empty JSON array, but since it was filled with some model objects specific to the application I was working on (rather, their JSON representation, of course) I guessed it was easier to report the bug with an empty array example.

@cowtowncoder cowtowncoder added this to the 2.6.4 milestone Oct 21, 2015
@cowtowncoder
Copy link
Member

Turned out to be a copy+paste error for array reader; interestingly enough, only special cases of 0 and 1 elements were affected, but not larger arrays. And this is why unit tests passed; they had 2 elements in the JSON Array....

Fixed for 2.6.4.

@cowtowncoder
Copy link
Member

@gpiancastelli no that's completely fine, I was just hunting down the bug, trying to see why unit test was not catching the problem. :)

@gpiancastelli
Copy link
Author

@cowtowncoder but now that you pointed out the specifics of the bug, I double checked my "real" example, and indeed the array contained only one of those model objects I said it was filled with! Everything comes together.

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