Skip to content

XMLParserConfiguration's forceList resets the whole array when empty elements present in the middle or end #1032

@pratiktiwari13

Description

@pratiktiwari13

Hey folks!
When we use forceList and there happens to be either an empty tag or an empty element in the middle or the end of the array, the conversion resets the context object without accounting the presence of a previously collected array state.

Empty in the end

        final String input = "<root><id>1</id><id/></root>"; //or ending with a <id></id>

        HashSet<String> forceListCandidates = new HashSet<>();
        forceListCandidates.add("id");
        final JSONObject json = XML.toJSONObject(input,
                new XMLParserConfiguration()
                        .withKeepStrings(false)
                        .withcDataTagName("content")
                        .withForceList(forceListCandidates)
                        .withConvertNilAttributeToNull(true));

Output:

{"root":{"id":[]}}

Empty in the middle

        final String input = "<root><id>1</id><id/><id>2</id></root>"; //or <id></id> in the middle

        HashSet<String> forceListCandidates = new HashSet<>();
        forceListCandidates.add("id");
        final JSONObject json = XML.toJSONObject(input,
                new XMLParserConfiguration()
                        .withKeepStrings(false)
                        .withcDataTagName("content")
                        .withForceList(forceListCandidates)
                        .withConvertNilAttributeToNull(true));

Output:

{"root":{"id":[2]}}

Code snippet responsible for empty element
Image

Code snippet responsible for empty tag

Image

I've done the fixes on my local branch so if you don't mind I'm ready to pick this up after we've gone through this together!

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions