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

Improve support for XML lists via JsonNode #695

Open
ofrias opened this issue Dec 17, 2024 · 4 comments
Open

Improve support for XML lists via JsonNode #695

ofrias opened this issue Dec 17, 2024 · 4 comments

Comments

@ofrias
Copy link

ofrias commented Dec 17, 2024

Hello. I understand that Jackson 3 plans to better support formats other than JSON. In particular, for XML, it would be nice to have a method like JsonNode.getArray(String element) to get XML node lists in ArrayNode format automatically, even for 1 element lists which are currenty returned by JsonNode.get("element") as an ObjectNode. For more context please see this SO question. Thanks!

@cowtowncoder
Copy link
Member

Thank you for your suggestion. We can consider something like this (getAsArray() perhaps). But it cannot quite be XML-specific, altho this could be generally useful across data formats.

@cowtowncoder cowtowncoder changed the title Improve support for XML lists Improve support for XML lists via JsonNode Dec 24, 2024
@cowtowncoder
Copy link
Member

Created FasterXML/jackson-databind#4868 as follow-up

@cowtowncoder
Copy link
Member

... and closed that issue. There are reason it is not practical to do.

I'll re-open this issue however.

@cowtowncoder cowtowncoder reopened this Feb 13, 2025
@ofrias
Copy link
Author

ofrias commented Feb 13, 2025

Since the other issue is closed, I add here my reasoning.

With the current implementation, JsonNode.get("element") returns an ArrayNode for this XML:

    <list>
        <element>
            <a>value 1a</a>
            <b>value 1b</b>
        </element>
        <element>
            <a>value 2a</a>
            <b>value 2b</b>
        </element>
    </list>

but an ObjectNode for this XML:

    <list>
        <element>
            <a>value a</a>
            <b>value b</b>
        </element>
    </list>

But both XML are have in fact the exact same structure, the only difference is the number of elements in the list.

My proposal is to add to JsonNode a method asArray(String element) returning an ArrayNode for both cases.

Since JsonNode does not contain a reference to the JsonNodeFactory, it could just implement a empty method throwing an Exception (like many other methods in the class). The implementation will be in ObjectNode and could be something similar to:

    public JsonNode asArray(String element) {
        JsonNode node = get(element);
        if (node == null) {
            return this._nodeFactory.arrayNode();
        }
        if (node.isArray()) {
            return node;
        } else {
            return this._nodeFactory.arrayNode().add(node);
        }
    }

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