Skip to content

Add ArrayNode JsonNode.asArray() helper method #4868

Closed
@cowtowncoder

Description

@cowtowncoder

(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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions