7
7
8
8
import java .nio .charset .Charset ;
9
9
10
+ import com .fasterxml .jackson .core .io .InputSourceReference ;
11
+
10
12
/**
11
13
* Object that encapsulates Location information used for reporting
12
14
* parsing (or potentially generation) errors, as well as current location
15
17
public class JsonLocation
16
18
implements java .io .Serializable
17
19
{
18
- private static final long serialVersionUID = 1L ;
20
+ private static final long serialVersionUID = 2L ; // in 2.13
19
21
20
22
/**
21
23
* Include at most first 500 characters/bytes from contents; should be enough
@@ -33,7 +35,8 @@ public class JsonLocation
33
35
* NOTE: before 2.9, Location was given as String "N/A"; with 2.9 it was
34
36
* removed so that source should be indicated as "UNKNOWN".
35
37
*/
36
- public final static JsonLocation NA = new JsonLocation (null , -1L , -1L , -1 , -1 );
38
+ public final static JsonLocation NA = new JsonLocation (InputSourceReference .unknown (),
39
+ -1L , -1L , -1 , -1 );
37
40
38
41
protected final long _totalBytes ;
39
42
protected final long _totalChars ;
@@ -42,32 +45,67 @@ public class JsonLocation
42
45
protected final int _columnNr ;
43
46
44
47
/**
45
- * Displayable description for input source: file path, URL.
46
- *<p>
47
- * NOTE: <code>transient</code> since 2.2 so that Location itself is Serializable.
48
+ * Reference to input source; never null (but may be that of
49
+ * {@link InputSourceReference#unknown()}).
50
+ *
51
+ * @since 2.13 (before we have {@code _sourceRef} (Object-valued)
48
52
*/
49
- final transient Object _sourceRef ;
53
+ protected final InputSourceReference _inputSource ;
50
54
51
- public JsonLocation (Object srcRef , long totalChars , int lineNr , int colNr )
55
+ public JsonLocation (InputSourceReference inputSource , long totalChars ,
56
+ int lineNr , int colNr )
52
57
{
53
- /* Unfortunately, none of legal encodings are straight single-byte
54
- * encodings. Could determine offset for UTF-16/UTF-32, but the
55
- * most important one is UTF-8...
56
- * so for now, we'll just not report any real byte count
57
- */
58
- this (srcRef , -1L , totalChars , lineNr , colNr );
58
+ this (inputSource , -1L , totalChars , lineNr , colNr );
59
59
}
60
60
61
- public JsonLocation (Object sourceRef , long totalBytes , long totalChars ,
61
+ public JsonLocation (InputSourceReference inputSource , long totalBytes , long totalChars ,
62
62
int lineNr , int columnNr )
63
63
{
64
- _sourceRef = sourceRef ;
64
+ // 14-Mar-2021, tatu: Defensive programming, but also for convenience...
65
+ if (inputSource == null ) {
66
+ inputSource = InputSourceReference .unknown ();
67
+ }
68
+ _inputSource = inputSource ;
65
69
_totalBytes = totalBytes ;
66
70
_totalChars = totalChars ;
67
71
_lineNr = lineNr ;
68
72
_columnNr = columnNr ;
69
73
}
70
74
75
+ @ Deprecated
76
+ public JsonLocation (Object srcRef , long totalChars , int lineNr , int columnNr ) {
77
+ this (_wrap (srcRef ), totalChars , lineNr , columnNr );
78
+ }
79
+
80
+ @ Deprecated
81
+ public JsonLocation (Object srcRef , long totalBytes , long totalChars ,
82
+ int lineNr , int columnNr ) {
83
+ this (_wrap (srcRef ), totalBytes , totalChars , lineNr , columnNr );
84
+ }
85
+
86
+ protected static InputSourceReference _wrap (Object srcRef ) {
87
+ if (srcRef instanceof InputSourceReference ) {
88
+ return (InputSourceReference ) srcRef ;
89
+ }
90
+ return new InputSourceReference (false , srcRef );
91
+ }
92
+
93
+ /**
94
+ * Accessor for information about the original input source content is being
95
+ * read from. Returned reference is never {@code null} but may not contain
96
+ * useful information.
97
+ *<p>
98
+ * NOTE: not getter, on purpose, to avoid inlusion if serialized using
99
+ * default Jackson serializer.
100
+ *
101
+ * @return Object with information about input source.
102
+ *
103
+ * @since 2.13 (to replace {@code getSourceRef})
104
+ */
105
+ public InputSourceReference inputSource () {
106
+ return _inputSource ;
107
+ }
108
+
71
109
/**
72
110
* Reference to the original resource being read, if one available.
73
111
* For example, when a parser has been constructed by passing
@@ -77,8 +115,13 @@ public JsonLocation(Object sourceRef, long totalBytes, long totalChars,
77
115
* construct the parser instance.
78
116
*
79
117
* @return Source reference this location was constructed with, if any; {@code null} if none
118
+ *
119
+ * @deprecated Since 2.13 Use {@link #inputSource} instead
80
120
*/
81
- public Object getSourceRef () { return _sourceRef ; }
121
+ @ Deprecated
122
+ public Object getSourceRef () {
123
+ return _inputSource .getSource ();
124
+ }
82
125
83
126
/**
84
127
* @return Line number of the location (1-based)
@@ -129,7 +172,7 @@ public String sourceDescription() {
129
172
@ Override
130
173
public int hashCode ()
131
174
{
132
- int hash = (_sourceRef == null ) ? 1 : _sourceRef . hashCode () ;
175
+ int hash = (_inputSource == null ) ? 1 : 2 ;
133
176
hash ^= _lineNr ;
134
177
hash += _columnNr ;
135
178
hash ^= (int ) _totalChars ;
@@ -145,9 +188,9 @@ public boolean equals(Object other)
145
188
if (!(other instanceof JsonLocation )) return false ;
146
189
JsonLocation otherLoc = (JsonLocation ) other ;
147
190
148
- if (_sourceRef == null ) {
149
- if (otherLoc ._sourceRef != null ) return false ;
150
- } else if (!_sourceRef .equals (otherLoc ._sourceRef )) return false ;
191
+ if (_inputSource == null ) {
192
+ if (otherLoc ._inputSource != null ) return false ;
193
+ } else if (!_inputSource .equals (otherLoc ._inputSource )) return false ;
151
194
152
195
return (_lineNr == otherLoc ._lineNr )
153
196
&& (_columnNr == otherLoc ._columnNr )
@@ -172,7 +215,7 @@ public String toString()
172
215
173
216
protected StringBuilder _appendSourceDesc (StringBuilder sb )
174
217
{
175
- final Object srcRef = _sourceRef ;
218
+ final Object srcRef = _inputSource . getSource () ;
176
219
177
220
if (srcRef == null ) {
178
221
sb .append ("UNKNOWN" );
0 commit comments