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

RFE : PrimitiveArrayDeserializers to deal with single String value #4650

Open
cowtowncoder opened this issue Jul 26, 2024 Discussed in #4649 · 2 comments
Open

RFE : PrimitiveArrayDeserializers to deal with single String value #4650

cowtowncoder opened this issue Jul 26, 2024 Discussed in #4649 · 2 comments

Comments

@cowtowncoder
Copy link
Member

Discussed in #4649

Originally posted by eeren-bm July 25, 2024
I think there's a bit of asymmetry when the lib is used to deserialized into primitive array vs List of objects. Please see the below example

        String longPrim = "2247483647";
        List<String> list1 = List.of("2247483647", "2247483648");
        Long longObj = 2247483647L;
        List<Long> longList = List.of(2247483647L, 2247483648L);

        String longPrimJson = "\"2247483647\"";
        String listStringJson = "[\"2247483647\", \"2247483648\"]";
        String longObjJson = "2247483647";
        String longListJson = "[2247483647, 2247483648]";

        ObjectMapper om = new ObjectMapper();
        om.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

//long[] primResult = om.convertValue(longPrim, long[].class);
        long[] primResult2 = om.convertValue(list1, long[].class);
        long[] primResult3 = om.convertValue(longObj, long[].class);
        long[] primResult4 = om.convertValue(longList, long[].class);

//long[] primResultFromJson = om.readValue(longPrimJson, long[].class);
        long[] primResult2FromJson = om.readValue(listStringJson, long[].class);
        long[] primResult3FromJson = om.readValue(longObjJson, long[].class);
        long[] primResult4FromJson = om.readValue(longListJson, long[].class);

        List<Long> longListResult = om.convertValue(longPrim, List.class);
        List<Long> longListResult2 = om.convertValue(list1, List.class);
        List<Long> longListResult3 = om.convertValue(longObj, List.class);
        List<Long> longListResult4 = om.convertValue(longList, List.class);

        List<Long> longListResultFromJson = om.readValue(longPrimJson, List.class);
        List<Long> longListResult2FromJson = om.readValue(listStringJson, List.class);
        List<Long> longListResult3FromJson = om.readValue(longObjJson, List.class);
        List<Long> longListResult4FromJson = om.readValue(longListJson, List.class);

The commented lines are lines I would expect to generate the similar results as List without throwing exceptions.

I believe the simple fix is to remove below lines from PrimitiveArrayDeserializers class' handleNonArray() method

// Empty String can become null...
        if (p.hasToken(JsonToken.VALUE_STRING)) {
            return _deserializeFromString(p, ctxt);
        }

as they would almost certainly throw exception as getValueInstantiator() method is not being overriden to generate a sensible result. And a single string value will be dealt with correctly without above lines.

Thanks for looking ! Please kindly let me know your thoughts

@eeren-bm
Copy link

hi, just wondering if this is a sensible request for enhancement? I believe it'd be fine in terms of backwards compatibility too, I am happy to raise a PR if that's ok

@cowtowncoder
Copy link
Member Author

@eeren-bm Yes, I think PR would make sense -- including tests to verify that handling for primitive arrays works as expected.

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