Class OffsetJSONReader
-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public class OffsetJSONReader extends JSONReaderUTF8A JSONReader that extends JSONReaderUTF8 to track the exact byte offset where a parsed JSON object ends. Designed for non-greedy parsing: when the input contains trailing non-JSON content after a valid object, this reader captures the precise boundary.
This class lives in the
com.alibaba.fastjson2package becauseJSONReaderUTF8is package-private. It does not modify any existing source files.Supported features (passed through to the underlying reader):
- AllowUnQuotedFieldNames — unquoted keys like
{key:"val"} - IgnoreCheckClose — no-op on the readObject() path; trailing content is naturally ignored
- NonStringKeyAsString — numeric keys are handled as unquoted names when AllowUnQuotedFieldNames is set
' }OffsetResult r = OffsetJSONReader.parseObject( "{\"test\":\"111\"} trailing garbage", Feature.AllowUnQuotedFieldNames, Feature.IgnoreCheckClose, Feature.NonStringKeyAsString ); Map<String, Object> map = r.object; // {"test":"111"} int endOffset = r.endOffset; // byte position right after '
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classOffsetJSONReader.OffsetResultHolds the result of a non-greedy JSON object parse.
-
Field Summary
Fields Modifier and Type Field Description public intjsonEndOffsetpublic final booleanjsonbpublic final booleanutf8
-
Constructor Summary
Constructors Constructor Description OffsetJSONReader(JSONReader.Context ctx, Array<byte> bytes, int offset, int length)Constructs an OffsetJSONReader over a byte range.
-
Method Summary
Modifier and Type Method Description intgetJsonEndOffset()Returns the byte offset immediately after the closing '}' of the most recently parsed top-level JSON object, or -1 if no object has been parsed yet. voidnext()Intercepts next to capture the byte offset at the exact moment a JSON object's closing '}' is detected. static OffsetJSONReader.OffsetResultparseObject(Array<byte> utf8Bytes, Array<JSONReader.Feature> features)Non-greedy JSON object parse over the full byte array. static OffsetJSONReader.OffsetResultparseObject(Array<byte> utf8Bytes, int off, int len, Array<JSONReader.Feature> features)Non-greedy JSON object parse over a byte range. static OffsetJSONReader.OffsetResultparseObject(String json, Array<JSONReader.Feature> features)Non-greedy JSON object parse from a String. -
Methods inherited from class com.alibaba.fastjson2.JSONReaderUTF8
close, getFieldName, getNameHashCodeLCase, getRawInt, getRawLong, getString, getStringLength, info, isNull, isReference, next, nextIfArrayEnd, nextIfArrayStart, nextIfComma, nextIfInfinity, nextIfMatch, nextIfMatchIdent, nextIfName4Match10, nextIfName4Match11, nextIfName4Match12, nextIfName4Match13, nextIfName4Match14, nextIfName4Match15, nextIfName4Match16, nextIfName4Match17, nextIfName4Match18, nextIfName4Match19, nextIfName4Match2, nextIfName4Match20, nextIfName4Match21, nextIfName4Match22, nextIfName4Match23, nextIfName4Match24, nextIfName4Match25, nextIfName4Match26, nextIfName4Match27, nextIfName4Match28, nextIfName4Match29, nextIfName4Match3, nextIfName4Match30, nextIfName4Match31, nextIfName4Match32, nextIfName4Match33, nextIfName4Match34, nextIfName4Match35, nextIfName4Match36, nextIfName4Match37, nextIfName4Match38, nextIfName4Match39, nextIfName4Match4, nextIfName4Match40, nextIfName4Match41, nextIfName4Match42, nextIfName4Match43, nextIfName4Match5, nextIfName4Match6, nextIfName4Match7, nextIfName4Match8, nextIfName4Match9, nextIfName8Match0, nextIfName8Match1, nextIfName8Match2, nextIfNull, nextIfNullOrEmptyString, nextIfObjectEnd, nextIfObjectStart, nextIfSet, nextIfValue4Match10, nextIfValue4Match11, nextIfValue4Match2, nextIfValue4Match3, nextIfValue4Match4, nextIfValue4Match5, nextIfValue4Match6, nextIfValue4Match7, nextIfValue4Match8, nextIfValue4Match9, nextWithoutComment, of, readBase64, readBigDecimal, readBoolValue, readDoubleValue, readFieldName, readFieldNameHashCode, readFieldNameHashCodeE, readFieldNameHashCodeUnquote, readFloatValue, readHex, readIfNull, readInt32, readInt32Value, readInt64, readInt64Value, readLocalDate, readLocalDate10, readLocalDate8, readLocalDate9, readMillis19, readNaN, readNull, readNullOrNewDate, readNumber, readNumber0, readOffsetDateTime, readOffsetTime, readPattern, readReference, readString, readString, readUUID, readValueHashCode, skipComment, skipName, skipValue -
Methods inherited from class com.alibaba.fastjson2.JSONReader
addResolveTask, autoTypeFilter, checkAutoType, current, endArray, errorOnNoneSerializable, features, getBigDecimal, getBigInteger, getContext, getDoubleValue, getFloatValue, getInt32Value, getInt64Value, getLocale, getNumber, getObjectReader, getObjectReaderAutoType, getOffset, getType, getZoneId, handleResolveTasks, hasAutoTypeBeforeHandler, hasComma, isArray, isBinary, isBool, isDate, isEnabled, isEnd, isIgnoreNoneSerializable, isInitStringFieldAsEmpty, isInt, isJSONB, isNumber, isObject, isString, isSupportAutoType, isSupportAutoTypeOrHandler, isSupportBeanArray, isSupportSmartMatch, isTypeRedirect, mark, nextIfMatchTypedAny, of, ofJSONB, read, read, readAny, readArray, readArray, readArray, readBigInteger, readBinary, readBool, readCalendar, readCharValue, readCharacter, readDate, readDouble, readFieldNameUnquote, readFloat, readInstant, readInt16, readInt16Value, readInt32ValueArray, readInt64ValueArray, readInt8, readInt8Value, readJSONArray, readJSONObject, readList, readLocalDateTime, readLocalTime, readMillisFromString, readNumber, readObject, readObject, readReference, readStringArray, readTypeHashCode, readZonedDateTime, reset, setTypeRedirect, startArray, wasNull -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
OffsetJSONReader
OffsetJSONReader(JSONReader.Context ctx, Array<byte> bytes, int offset, int length)
Constructs an OffsetJSONReader over a byte range.- Parameters:
ctx- the reader context (features, provider, etc.bytes- the UTF-8 encoded inputoffset- start offset in the arraylength- number of bytes to read
-
-
Method Detail
-
getJsonEndOffset
int getJsonEndOffset()
Returns the byte offset immediately after the closing '}' of the most recently parsed top-level JSON object, or -1 if no object has been parsed yet.
-
next
void next()
Intercepts next to capture the byte offset at the exact moment a JSON object's closing '}' is detected.
The core insight: in
JSONReader.readObject()(line 3719), whench == ''} the reader callsnext()to advance past the brace. At that instantthis.offsetpoints to the byte immediately following '}' — the precise JSON end boundary. We save this value beforesuper.next()overwrites it with the lookahead position.
-
parseObject
static OffsetJSONReader.OffsetResult parseObject(Array<byte> utf8Bytes, Array<JSONReader.Feature> features)
Non-greedy JSON object parse over the full byte array.
- Parameters:
utf8Bytes- UTF-8 encoded JSON text (trailing content allowed)features- optional reader features- Returns:
the parsed map and the byte offset where JSON ended
-
parseObject
static OffsetJSONReader.OffsetResult parseObject(Array<byte> utf8Bytes, int off, int len, Array<JSONReader.Feature> features)
Non-greedy JSON object parse over a byte range.
- Parameters:
utf8Bytes- UTF-8 encoded JSON text (trailing content allowed)off- start offsetlen- number of bytes to readfeatures- optional reader features- Returns:
the parsed map and the byte offset where JSON ended
-
parseObject
static OffsetJSONReader.OffsetResult parseObject(String json, Array<JSONReader.Feature> features)
Non-greedy JSON object parse from a String. The string is converted to UTF-8 bytes internally; the returned
endOffsetis a byte offset within that UTF-8 representation.- Parameters:
json- the JSON string (trailing content allowed)features- optional reader features- Returns:
the parsed map and the byte offset where JSON ended
-
-
-
-