#2373·gson

Proposal to expose Position information used for error messages in JSonReader in public API

Author: jurgenvinjuCreated Apr 12, 2023Updated Sep 10, 2026
Labelsenhancement

Thanks for maintaining this library!

Problem solved by the feature

  • Many projects use JSON notation for their DSL's syntax
  • Reusing Gson implies high quality and compatible parsing for those DSLs
  • IDE support for DSLs requires position information (offset/length, start line/column, end line/column
  • For example to create Language Servers for the LSP for the given (embedded) DSLs

Feature description

Proposal to expose the following fields of JsonReader:

private int pos = 0;
private int lineNumber = 0;
private int lineStart = 0;

With these public methods:

public int getPosition() {
  return pos;
}

public int getLineNumber() {
  return lineNumber + 1; // posix has the first line at 1
}

public int getColumnNumber() {
  return pos - lineStart; // posix has columns start at 0
}

Using these methods just before beginObject() and just after endObject() you get an accurate measurement of the span of every object in the character stream.

Alternatives / workarounds

  • We currently use the reflection API to access these fields in the usethesource/rascal meta-programming project.
  • That seems to work fine, but it's not as nice as it could be :-)