Skip to content

Optionally keeping token locations in TreeTraversingParser #5895

@indyteo

Description

@indyteo

Is your feature request related to a problem? Please describe.

Currently, the TreeTraversingParser always returns TokenStreamLocation.NA when calling current[Token]Location().
https://github.com/FasterXML/jackson-databind/blob/3.x/src/main/java/tools/jackson/databind/node/TreeTraversingParser.java#L180C5-L188C6
Thus, locations are lost if doing parser = context.treeAsTokens(parser.readValueAsTree()) (obviously no one would just do that like this, but it is the most simple way to illustrate that).

The location is particularly relevant for error messages, and in that scenario all subsequent errors would be harder to debug without that info.

Describe the solution you'd like

If the JsonNode class had an optional field to refer to its origin as a TokenStreamLocation, it would be possible for the BaseNodeDeserializer to fill that field automatically when deserializing the tree.
Then, the TreeTraversingParser would be able to use that info to provide accurate locations instead.

If we consider that it could have an impact on performance to compute that extra info (which I believe is already precomputed anyway, at least in YAML I've looked at the code, and the SnakeYAML engine already have the Mark in each event), a DeserializationFeature toggle could be added to control whether to keep that info or not.

Usage example

Usage wouldn't be any different from how it can be done now, it would just maybe return something useful (and still nothing otherwise, it would be a kind of "best-effort").

Additional context

If you are ok about this feature being added, I can contribute it. I've already dived into the code to analyze whether it was already possible right now, and have a pretty clear vision of what would need to change (and I believe it's not that much).

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    to-evaluateIssue that has been received but not yet evaluated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions