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

Add ArrayNode JsonNode.asArray() helper method #4868

Open
cowtowncoder opened this issue Dec 27, 2024 · 3 comments
Open

Add ArrayNode JsonNode.asArray() helper method #4868

cowtowncoder opened this issue Dec 27, 2024 · 3 comments

Comments

@cowtowncoder
Copy link
Member

(note: part of https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-3 changes)

(inspired by FasterXML/jackson-dataformat-xml#695)

With some JSON structures (as well as other formats, most notably XML), there is sometimes need to process/express things such that single-values that are not express as (JSON) Arrays would be handled same as 1-element arrays. Example shown with XML is that following documents:

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

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

become different JsonNodes structurally: basically equivalent of JSON like so:

{ "list":
    "element": [
        { ... },
       { .... }
   ]
}

{ "list":
     "element": {
          "a": "value a",
          "b": "value b"
     }
}

So to help handling with such case -- or, possibly, modifying such content, we could add new method:

ArrayNode asArray();

which will return:

  • this for ArrayNode
  • new ArrayNode() (empty ArrayNode) for MissingNode ("missing" node)
  • new ArrayNode().add(this) for all other JsonNode implementations

to allow unifying processing of otherwise structurally mismatching JsonNode trees.

@yawkat
Copy link
Member

yawkat commented Dec 30, 2024

I don't think we should encourage this kind of code by adding API to it. Wrapping all node types except for arrays in a single-element array is a bit weird.

@JooHyukKim
Copy link
Member

@yawkat Because I think suggestion from xml-module kinda makes sense, makes me more curious why this would be weird or a bad idea? 👀

@cowtowncoder
Copy link
Member Author

It seems weird from plain JSON perspective, but as @JooHyukKim pointed it makes more sense for XML handling; or more generally for "single Object as-is, two or more as Array" usage patterns which are pretty common (esp. with scripting languages).

While I personally also dislike such polymorphism (if something allows multiple values I feel it ought to be Array even if it contains just 1 element) I think there's something to be said for supporting actual use cases.

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

3 participants