[英]Is this a bug in the Jackson JsonParser, or am I doing something wrong?
I'm observing what appears to be some wierd behavior with the Jackson JsonParser
, specifically, capturing a correct JsonPointer
while in an array.我正在观察 Jackson
JsonParser
的一些奇怪行为,特别是在数组中捕获正确的JsonPointer
。
Given the following JSON snippet:给定以下 JSON 片段:
[
{
"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:我有一个简单的 Kotlin function 尝试迭代
nextToken()
如下:
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: jsonPointer
不区分数组索引,如 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.路径总是返回
0
的索引,无论是在第一个还是第 n 个元素中。
Is this a bug?这是一个错误吗? Or have I somehow managed to introduce one?
还是我以某种方式设法介绍了一个?
It is possible you are experiencing this issue:您可能会遇到此问题:
https://github.com/FasterXML/jackson-databind/issues/2525 https://github.com/FasterXML/jackson-databind/issues/2525
which specifically affects case of "reading" contents of a JsonNode
.这特别影响“读取”
JsonNode
内容的情况。 Handling is fixed for 2.11 (to be released by end of 2019 or early 2020), although not patched for 2.10 yet. 2.11 的处理是固定的(将于 2019 年底或 2020 年初发布),但尚未为 2.10 打补丁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.