Skip to content

Commit 9140ee3

Browse files
committed
Add getStartLocation() in JsonStreamContext, to allow access for non-standard read contexts
1 parent 937f386 commit 9140ee3

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/main/java/com/fasterxml/jackson/core/JsonStreamContext.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public abstract class JsonStreamContext
4242

4343
protected JsonStreamContext() { }
4444

45+
/**
46+
* Copy constructor used by sub-classes for creating copies for
47+
* buffering.
48+
*
49+
* @since 2.9
50+
*/
51+
protected JsonStreamContext(JsonStreamContext base) {
52+
_type = base._type;
53+
_index = base._index;
54+
}
55+
4556
/*
4657
/**********************************************************
4758
/* Public API, accessors
@@ -203,11 +214,29 @@ public JsonPointer pathAsPointer() {
203214
* "root value index"
204215
*
205216
* @param includeRoot Whether root-value offset is included as the first segment or not;
206-
*
207217
*
208218
* @since 2.9
209219
*/
210220
public JsonPointer pathAsPointer(boolean includeRoot) {
211221
return JsonPointer.forPath(this, includeRoot);
212222
}
223+
224+
/**
225+
* Optional method that may be used to access starting location of this context:
226+
* for example, in case of JSON `Object` context, offset at which `[` token was
227+
* read or written. Often used for error reporting purposes.
228+
* Implementations that do not keep track of such location are expected to return
229+
* {@link JsonLocation#NA}; this is what the default implementation does.
230+
*
231+
* @return Location pointing to the point where the context
232+
* start marker was found (or written); never `null`.
233+
*<p>
234+
* NOTE: demoted from <code>JsonReadContext</code> in 2.9, to allow use for
235+
* "non-standard" read contexts.
236+
*
237+
* @since 2.9
238+
*/
239+
public JsonLocation getStartLocation(Object srcRef) {
240+
return JsonLocation.NA;
241+
}
213242
}

src/main/java/com/fasterxml/jackson/core/json/JsonReadContext.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public JsonReadContext createChildObjectContext(int lineNr, int colNr) {
135135

136136
/*
137137
/**********************************************************
138-
/* Abstract method implementation
138+
/* Abstract method implementations, overrides
139139
/**********************************************************
140140
*/
141141

@@ -146,6 +146,19 @@ public JsonReadContext createChildObjectContext(int lineNr, int colNr) {
146146

147147
@Override public JsonReadContext getParent() { return _parent; }
148148

149+
@Override
150+
public JsonLocation getStartLocation(Object srcRef) {
151+
// We don't keep track of offsets at this level (only reader does)
152+
long totalChars = -1L;
153+
return new JsonLocation(srcRef, totalChars, _lineNr, _columnNr);
154+
}
155+
156+
/*
157+
/**********************************************************
158+
/* Extended API
159+
/**********************************************************
160+
*/
161+
149162
/**
150163
* Method that can be used to both clear the accumulated references
151164
* (specifically value set with {@link #setCurrentValue(Object)})
@@ -162,22 +175,6 @@ public JsonReadContext clearAndGetParent() {
162175
return _parent;
163176
}
164177

165-
/*
166-
/**********************************************************
167-
/* Extended API
168-
/**********************************************************
169-
*/
170-
171-
/**
172-
* @return Location pointing to the point where the context
173-
* start marker was found
174-
*/
175-
public JsonLocation getStartLocation(Object srcRef) {
176-
// We don't keep track of offsets at this level (only reader does)
177-
long totalChars = -1L;
178-
return new JsonLocation(srcRef, totalChars, _lineNr, _columnNr);
179-
}
180-
181178
public DupDetector getDupDetector() {
182179
return _dups;
183180
}

src/main/java/com/fasterxml/jackson/core/util/DefaultPrettyPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
237237
public DefaultPrettyPrinter createInstance() {
238238
return new DefaultPrettyPrinter(this);
239239
}
240-
240+
241241
/*
242242
/**********************************************************
243243
/* PrettyPrinter impl

0 commit comments

Comments
 (0)