如何在 Jackson json 库中执行 peek() ?

发布于 2024-09-07 00:53:56 字数 711 浏览 8 评论 0原文

我正在使用 Streaming API JsonParser Jackson 库在 java 中进行一些自定义 json 解析。

有没有一种方法可以实现类似于 peek() 方法的功能,其中返回下一个标记但光标位置不向前移动?

其用例类似于以下内容:

JsonParser parser = new JsonFactory().createParser(inputStream);
while(parser.peek() != JsonToken.START_ARRAY){
   ...do something with the current token...
}

我在 Jackson 中看到的代码示例使用 nextToken() 方法来处理上述情况,不幸的是,该方法也会将光标在流中向前移动。

peek() 是否可以通过 Jackson 开箱即用,或者可以通过其他方法实现?

注意。对其他图书馆不感兴趣,所以请不要“图书馆 x 做这一切和厨房水槽”类型的答案。

I'm using the Streaming API JsonParser of the Jackson library to do some custom json parsing in java.

Is there a way of achieving functionality similar to a peek() method, where the next token is returned but the cursor position is not moved forward?

The use case for this is similar to the following:

JsonParser parser = new JsonFactory().createParser(inputStream);
while(parser.peek() != JsonToken.START_ARRAY){
   ...do something with the current token...
}

The code examples I've seen for Jackson use the nextToken() method for the above case, which unfortunately also moves the cursor forward in the stream.

Is peek() possible with Jackson out-of-the-box, or perhaps achievable with an additional method?

NB. not interested in other libraries, so no "library x does all this and the kitchen sink" type answers please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2024-09-14 00:53:56

不,目前没有这样的功能。您也许可以通过使用委托包装器来做到这一点?有“org.codehaus.jackson.util.JsonParserDelegate”,它可以使其相对容易实现,具体取决于您想要保留有关当前元素的信息。

不管它的价值如何,很可能添加 JsonParser.peek() ;请求此功能的最佳方法是在 http://jira.codehaus.org/browse/ 提交功能请求杰克逊。很可能尚未有人请求此功能。

这可能很容易(足够)实现,因为它应该只需要单个字符前瞻。

No, currently there is no such functionality. You could perhaps do that by using a delegating wrapper? There is 'org.codehaus.jackson.util.JsonParserDelegate' which could make it relatively easy to implement, depending on what information you want to retain about current element.

For whatever it is worth, it might well be possible to get JsonParser.peek() added; best way to request this would be to file a feature request at http://jira.codehaus.org/browse/JACKSON. Most likely this feature hasn't been requested by anyone yet.

Reason this might be easy (enough) to implement is that it should only require a single character look-ahead.

靖瑶 2024-09-14 00:53:56

在 jackson 2.14.x 中,您可以执行以下操作:

JsonParser p = // ...
ObjectNode currentJsonObject = p.readValueAsTree();

JsonParser peekingParser = currentObject.traverse(codec);
// set peekingParser to the first token, see JsonParser#travers(ObjectCode) for info
peekingParser.nextToken();
peekIntoCurrentContent(peekingParser);

// this parser starts at the same point as p.readValueAsTree() was called above
JsonParser remainingContentParser = JsonParserSequence.createFlattended(true, currentObject.traverse(codec), p);
consumeAllRemainingContent(remainingContentParser);

但是请注意,由于 JsonParser 的方式,只有当 p 位于上述代码开头的数组或对象时,这才会一致地工作。 #readValueAsTree() 已实现。但这至少可以涵盖很多情况。

In jackson 2.14.x you can do something like this:

JsonParser p = // ...
ObjectNode currentJsonObject = p.readValueAsTree();

JsonParser peekingParser = currentObject.traverse(codec);
// set peekingParser to the first token, see JsonParser#travers(ObjectCode) for info
peekingParser.nextToken();
peekIntoCurrentContent(peekingParser);

// this parser starts at the same point as p.readValueAsTree() was called above
JsonParser remainingContentParser = JsonParserSequence.createFlattended(true, currentObject.traverse(codec), p);
consumeAllRemainingContent(remainingContentParser);

Note that this however will only work consistently if p is at an array or object start at the beginning of the code above due to how JsonParser#readValueAsTree() is implemented. But this will at least cover a lot of cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文