Is this a bug in the Jackson JsonParser, or am I doing something wrong?

246 Views Asked by At

I'm observing what appears to be some wierd behavior with the Jackson JsonParser, specifically, capturing a correct JsonPointer while in an array.

Given the following JSON snippet:

[
    {
        "name": "a",
        "children": [
            {
                "name": "b"
            },
            {
                "name": "c"
            },
            {
                "name": "d"
            }
        ]
    },
    {
        "name": "e",
        "children": [
            {
                "name": "f"
            },
            {
                "name": "g",
                "children": [
                    {
                        "name": "h"
                    },
                    {
                        "name": "i"
                    }
                ]
            }
        ]
    },
    {
        "name": "j"
    }
]

I have a simple Kotlin function that attempts to iterate on nextToken() as follows:

fun main()
{
    val jsonParser = jacksonObjectMapper().readTree(JSON).traverse()

    while (jsonParser.nextToken() != null)
    {
        val jsonPointer = jsonParser.parsingContext?.pathAsPointer(true) ?: continue
        val tokenName = jsonParser.currentToken.name
        println("${jsonPointer.toString().padEnd(40)} $tokenName")
    }
}

Now, here's what's strange; the jsonPointer isn't distinguishing array indices as indicated in the output:

                                         START_ARRAY
                                         START_OBJECT
/0/name                                  FIELD_NAME
/0/name                                  VALUE_STRING
/0/children                              FIELD_NAME
/0/children                              START_ARRAY
/0/children                              START_OBJECT
/0/children/0/name                       FIELD_NAME
/0/children/0/name                       VALUE_STRING
/0/children                              END_OBJECT
/0/children                              START_OBJECT
/0/children/0/name                       FIELD_NAME
/0/children/0/name                       VALUE_STRING
/0/children                              END_OBJECT
/0/children                              START_OBJECT
/0/children/0/name                       FIELD_NAME
/0/children/0/name                       VALUE_STRING
/0/children                              END_OBJECT
/0/children                              END_ARRAY
                                         END_OBJECT
                                         START_OBJECT
/0/name                                  FIELD_NAME
/0/name                                  VALUE_STRING
/0/children                              FIELD_NAME
/0/children                              START_ARRAY
/0/children                              START_OBJECT
/0/children/0/name                       FIELD_NAME
/0/children/0/name                       VALUE_STRING
/0/children                              END_OBJECT
/0/children                              START_OBJECT
/0/children/0/name                       FIELD_NAME
/0/children/0/name                       VALUE_STRING
/0/children/0/children                   FIELD_NAME
/0/children/0/children                   START_ARRAY
/0/children/0/children                   START_OBJECT
/0/children/0/children/0/name            FIELD_NAME
/0/children/0/children/0/name            VALUE_STRING
/0/children/0/children                   END_OBJECT
/0/children/0/children                   START_OBJECT
/0/children/0/children/0/name            FIELD_NAME
/0/children/0/children/0/name            VALUE_STRING
/0/children/0/children                   END_OBJECT
/0/children/0/children                   END_ARRAY
/0/children                              END_OBJECT
/0/children                              END_ARRAY
                                         END_OBJECT
                                         START_OBJECT
/0/name                                  FIELD_NAME
/0/name                                  VALUE_STRING
                                         END_OBJECT

The paths are always giving back an index of 0, whether in the first or n-th element.

Is this a bug? Or have I somehow managed to introduce one?

1

There are 1 best solutions below

3
On BEST ANSWER

It is possible you are experiencing this issue:

https://github.com/FasterXML/jackson-databind/issues/2525

which specifically affects case of "reading" contents of a JsonNode. Handling is fixed for 2.11 (to be released by end of 2019 or early 2020), although not patched for 2.10 yet.