FasterXML / FasterXML/jackson-databind

Optionally keeping token locations in `TreeTraversingParser`

Open
#5,895 3 comments 0 reactions 0 assignees View on GitHub
to-evaluate
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

### 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`](https://github.com/FasterXML/jackson-databind/blob/3.x/src/main/java/tools/jackson/databind/JsonNode.java) class had an optional field to refer to its origin as a `TokenStreamLocation`, it would be possible for the [`BaseNodeDeserializer`](https://github.com/FasterXML/jackson-databind/blob/3.x/src/main/java/tools/jackson/databind/deser/jackson/BaseNodeDeserializer.java) 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.