-
Notifications
You must be signed in to change notification settings - Fork 0
io_nats_jparse_parser_indexoverlay

The JsonStrictParser class is an implementation of the JsonParser interface. It uses a strict JSON parsing algorithm and does not accept JSON strings that are not strictly compliant with the JSON RFC.
private List<Token> scan(final CharSource source, TokenList tokens) {
nestLevel = 0;
int ch = source.nextSkipWhiteSpace();
switch(ch) {
case OBJECT_START_TOKEN:
parseObject(source, tokens);
break;
case ARRAY_START_TOKEN:
parseArray(source, tokens);
break;
case TRUE_BOOLEAN_START:
parseTrue(source, tokens);
break;
case FALSE_BOOLEAN_START:
parseFalse(source, tokens);
break;
case NULL_START:
parseNull(source, tokens);
break;
case STRING_START_TOKEN:
parseString(source, tokens);
break;
case NUM_0:
case NUM_1:
case NUM_2:
case NUM_3:
case NUM_4:
case NUM_5:
case NUM_6:
case NUM_7:
case NUM_8:
case NUM_9:
case MINUS:
case PLUS:
parseNumber(source, tokens);
break;
default:
throw new UnexpectedCharacterException("Scanning JSON", "Unexpected character", source, (char) ch);
}
source.checkForJunk();
return tokens;
}The scan method is defined in the JsonStrictParser class in the package io.nats.jparse.parser.indexoverlay. It takes two parameters: a CharSource object and a TokenList object.
Here is a step-by-step description of what the scan method does based on its body:
-
It initializes the
nestLevelvariable to 0. -
It calls the
nextSkipWhiteSpacemethod on thesourceobject to get the next non-whitespace character and assigns it to thechvariable. -
It uses a switch statement to perform different actions based on the value of
ch:-
If
chis equal toOBJECT_START_TOKEN, it calls theparseObjectmethod, passing thesourceandtokensobjects. -
If
chis equal toARRAY_START_TOKEN, it calls theparseArraymethod, passing thesourceandtokensobjects. -
If
chis equal toTRUE_BOOLEAN_START, it calls theparseTruemethod, passing thesourceandtokensobjects. -
If
chis equal toFALSE_BOOLEAN_START, it calls theparseFalsemethod, passing thesourceandtokensobjects. -
If
chis equal toNULL_START, it calls theparseNullmethod, passing thesourceandtokensobjects. -
If
chis equal toSTRING_START_TOKEN, it calls theparseStringmethod, passing thesourceandtokensobjects. -
If
chis equal to any of the number-related tokens (NUM_0,NUM_1, ...,NUM_9,MINUS,PLUS), it calls theparseNumbermethod, passing thesourceandtokensobjects. -
If none of the above cases match, it throws an
UnexpectedCharacterExceptionwith a message indicating that an unexpected character was encountered.
-
-
After the switch statement, it calls the
checkForJunkmethod on thesourceobject to check if there is any remaining junk after parsing the JSON. -
Finally, it returns the
tokensobject.
That's the step-by-step description of what the scan method does based on its body.

private void parseArray(final CharSource source, final TokenList tokens) {
levelCheck(source);
final int startSourceIndex = source.getIndex();
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
boolean done = false;
while (!done) {
done = parseArrayItem(source, tokens);
if (!done) {
done = source.findCommaOrEndForArray();
}
}
final Token arrayToken = new Token(startSourceIndex, source.getIndex(), TokenTypes.ARRAY_TOKEN);
tokens.set(tokenListIndex, arrayToken);
}The parseArray method in class io.nats.jparse.parser.indexoverlay.JsonStrictParser is responsible for parsing an array from a given character source.
Here is a step-by-step description of what this method does:
- It starts by calling the
levelCheckmethod, which verifies that the parser is at the correct level before parsing the array. - It stores the current index of the character source and the index of the token list.
- It adds a placeholder token to the token list using the
tokens.placeHolder()method. - It enters a loop which continues until the parsing of the array is done.
- Inside the loop, it calls the
parseArrayItemmethod to parse each item in the array. The method returns a boolean indicating whether the parsing is done or not. - If the parsing is not done, it calls the
source.findCommaOrEndForArray()method to find the next comma or the end of the array in the character source. - The loop continues until the parsing of the array is done.
- After the loop, it creates a
Tokenobject representing the parsed array, using the start index and the current index of the character source. The token type is set toTokenTypes.ARRAY_TOKEN. - Finally, it updates the placeholder token in the token list with the newly created array token using the
tokens.set(tokenListIndex, arrayToken)method.
In summary, the parseArray method iterates over the items in the array, parses each item using the parseArrayItem method, and then creates a token representing the parsed array.

private boolean parseArrayItem(CharSource source, TokenList tokens) {
char startChar = source.getCurrentChar();
int ch = source.nextSkipWhiteSpace();
switch(ch) {
case OBJECT_START_TOKEN:
parseObject(source, tokens);
break;
case ARRAY_START_TOKEN:
parseArray(source, tokens);
break;
case TRUE_BOOLEAN_START:
parseTrue(source, tokens);
break;
case FALSE_BOOLEAN_START:
parseFalse(source, tokens);
break;
case NULL_START:
parseNull(source, tokens);
break;
case STRING_START_TOKEN:
parseString(source, tokens);
break;
case NUM_0:
case NUM_1:
case NUM_2:
case NUM_3:
case NUM_4:
case NUM_5:
case NUM_6:
case NUM_7:
case NUM_8:
case NUM_9:
case MINUS:
case PLUS:
parseNumber(source, tokens);
if (source.getCurrentChar() == ARRAY_END_TOKEN || source.getCurrentChar() == ARRAY_SEP) {
if (source.getCurrentChar() == ARRAY_END_TOKEN) {
source.next();
return true;
}
}
break;
case ARRAY_END_TOKEN:
if (startChar == ARRAY_SEP) {
throw new UnexpectedCharacterException("Parsing Array Item", "Trailing comma", source, (char) ch);
}
source.next();
return true;
default:
throw new UnexpectedCharacterException("Parsing Array Item", "Unexpected character", source, (char) ch);
}
return false;
}The parseArrayItem method is defined in the JsonStrictParser class in the io.nats.jparse.parser.indexoverlay package. It takes two parameters, CharSource source and TokenList tokens, and returns a boolean value.
Here is a step-by-step description of what the parseArrayItem method does based on its body:
- It retrieves the current character from the
sourceusing thegetCurrentCharmethod and assigns it to thestartCharvariable. - It reads the next character from the
sourcewhile skipping any whitespaces using thenextSkipWhiteSpacemethod and assigns it to thechvariable. - It performs a switch-case statement based on the value of
chto determine the action to take:- If
chis equal toOBJECT_START_TOKEN, it calls theparseObjectmethod passingsourceandtokensas arguments. - If
chis equal toARRAY_START_TOKEN, it calls theparseArraymethod passingsourceandtokensas arguments. - If
chis equal toTRUE_BOOLEAN_START, it calls theparseTruemethod passingsourceandtokensas arguments. - If
chis equal toFALSE_BOOLEAN_START, it calls theparseFalsemethod passingsourceandtokensas arguments. - If
chis equal toNULL_START, it calls theparseNullmethod passingsourceandtokensas arguments. - If
chis equal toSTRING_START_TOKEN, it calls theparseStringmethod passingsourceandtokensas arguments. - If
chis equal to any of the numeric characters (0-9) or the symbols '-' or '+', it calls theparseNumbermethod passingsourceandtokensas arguments.- After parsing the number, if the current character is either
ARRAY_END_TOKENorARRAY_SEP, it checks if the current character isARRAY_END_TOKEN.- If it is, it advances the
sourceusing thenextmethod and returnstrue.
- If it is, it advances the
- After parsing the number, if the current character is either
- If
chis equal toARRAY_END_TOKEN, it first checks if thestartCharisARRAY_SEP.- If it is, it throws an
UnexpectedCharacterExceptionwith the message "Parsing Array Item: Trailing comma",sourceand the character as arguments. - If it is not, it advances the
sourceusing thenextmethod and returnstrue.
- If it is, it throws an
- If none of the above cases match, it throws an
UnexpectedCharacterExceptionwith the message "Parsing Array Item: Unexpected character",source, and the character as arguments.
- If
- If none of the cases match, it returns
false.
The purpose of the parseArrayItem method is to parse an item within a JSON array and add tokens to the TokenList for further processing.
private boolean parseKey(final CharSource source, final TokenList tokens) {
final char startChar = source.getCurrentChar();
int ch = source.nextSkipWhiteSpace();
final int startIndex = source.getIndex() - 1;
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
boolean found = false;
switch(ch) {
case STRING_START_TOKEN:
final int strStartIndex = startIndex + 1;
final int strEndIndex;
if (objectsKeysCanBeEncoded) {
strEndIndex = source.findEndOfEncodedString();
} else {
strEndIndex = source.findEndString();
}
tokens.add(new Token(strStartIndex + 1, strEndIndex, TokenTypes.STRING_TOKEN));
found = true;
break;
case OBJECT_END_TOKEN:
if (startChar == OBJECT_ATTRIBUTE_SEP) {
throw new UnexpectedCharacterException("Parsing key", "Unexpected character found", source);
}
tokens.undoPlaceholder();
return true;
default:
throw new UnexpectedCharacterException("Parsing key", "Unexpected character found", source);
}
boolean done = source.findObjectEndOrAttributeSep();
if (!done && found) {
tokens.set(tokenListIndex, new Token(startIndex + 1, source.getIndex(), TokenTypes.ATTRIBUTE_KEY_TOKEN));
} else if (found && done) {
throw new UnexpectedCharacterException("Parsing key", "Not found", source);
}
return done;
}The parseKey method in the JsonStrictParser class is responsible for parsing a key from a CharSource and adding it to a TokenList. Here is a step-by-step description of this method:
- The method starts by getting the current character from the
CharSource. - It then skips any whitespace characters and gets the next character from the
CharSource. - The current index of the
CharSourceis stored as the starting index of the key. - The current index of the
TokenListis stored as the token list index. - A placeholder token is added to the
TokenList. - A switch statement is used to handle different cases based on the value of the next character:
- If the next character is a
STRING_START_TOKEN, the method proceeds to extract the string key. - If the next character is an
OBJECT_END_TOKEN, the method checks if the start character is anOBJECT_ATTRIBUTE_SEPand throws an exception if so. Otherwise, it undoes the placeholder token and returnstrue. - If none of the above cases match, an exception is thrown.
- If the next character is a
- If a string key is found, the method determines the end index of the string and adds a new token to the
TokenListrepresenting the string key. - A boolean variable
doneis set based on whether the method can find the end of the object or the attribute separator. - If the parsing is not done (
!done) and a key is found (foundistrue), the method updates the placeholder token in theTokenListwith the start and end indices of the key. - If a key is found (
foundistrue) and the parsing is done (doneistrue), an exception is thrown. - Finally, the method returns the value of
done.
This method is responsible for parsing a key from a JSON object and updating the TokenList with the parsed key token.

private boolean parseValue(final CharSource source, TokenList tokens) {
int ch = source.nextSkipWhiteSpace();
final int startIndex = source.getIndex();
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
switch(ch) {
case OBJECT_START_TOKEN:
parseObject(source, tokens);
break;
case ARRAY_START_TOKEN:
parseArray(source, tokens);
break;
case TRUE_BOOLEAN_START:
parseTrue(source, tokens);
break;
case FALSE_BOOLEAN_START:
parseFalse(source, tokens);
break;
case NULL_START:
parseNull(source, tokens);
break;
case STRING_START_TOKEN:
parseString(source, tokens);
break;
case NUM_0:
case NUM_1:
case NUM_2:
case NUM_3:
case NUM_4:
case NUM_5:
case NUM_6:
case NUM_7:
case NUM_8:
case NUM_9:
case MINUS:
case PLUS:
parseNumber(source, tokens);
break;
default:
throw new UnexpectedCharacterException("Parsing Value", "Unexpected character", source, ch);
}
source.skipWhiteSpace();
switch(source.getCurrentChar()) {
case OBJECT_END_TOKEN:
if (source.getIndex() == tokenListIndex) {
throw new UnexpectedCharacterException("Parsing Value", "Key separator before value", source);
}
tokens.set(tokenListIndex, new Token(startIndex, source.getIndex(), TokenTypes.ATTRIBUTE_VALUE_TOKEN));
return true;
case OBJECT_ATTRIBUTE_SEP:
if (source.getIndex() == tokenListIndex) {
throw new UnexpectedCharacterException("Parsing Value", "Key separator before value", source);
}
tokens.set(tokenListIndex, new Token(startIndex, source.getIndex(), TokenTypes.ATTRIBUTE_VALUE_TOKEN));
return false;
default:
throw new UnexpectedCharacterException("Parsing Value", "Unexpected character", source, source.getCurrentChar());
}
}The parseValue method in the JsonStrictParser class is responsible for parsing a single JSON value from a given character source (source) and adding the corresponding token to a token list (tokens). Here is a step-by-step description of what the method does:
- Read the next character from the character source while skipping any white space characters.
- Get the current index of the character source (
startIndex) and the current index of the token list (tokenListIndex). - Add a placeholder token to the token list at the current index.
- Use a switch statement to check the value of the character read in step 1.
- If the character is the start of an object (
OBJECT_START_TOKEN), call theparseObjectmethod to parse the object and add the corresponding token(s) to the token list. - If the character is the start of an array (
ARRAY_START_TOKEN), call theparseArraymethod to parse the array and add the corresponding token(s) to the token list. - If the character is the start of the boolean value
true(TRUE_BOOLEAN_START), call theparseTruemethod to parse the boolean value and add the corresponding token(s) to the token list. - If the character is the start of the boolean value
false(FALSE_BOOLEAN_START), call theparseFalsemethod to parse the boolean value and add the corresponding token(s) to the token list. - If the character is the start of the value
null(NULL_START), call theparseNullmethod to parse the null value and add the corresponding token(s) to the token list. - If the character is the start of a string (
STRING_START_TOKEN), call theparseStringmethod to parse the string value and add the corresponding token(s) to the token list. - If the character is a digit (
NUM_0toNUM_9) or a sign (MINUSorPLUS), call theparseNumbermethod to parse the number value and add the corresponding token(s) to the token list. - If the character does not match any of the above cases, throw an
UnexpectedCharacterExceptionwith an appropriate error message.
- If the character is the start of an object (
- Skip any white space characters after parsing the value.
- Use a switch statement to check the current character in the character source.
- If the character is the end of an object (
OBJECT_END_TOKEN), check if the current index of the character source is the same as thetokenListIndex. If they are equal, throw anUnexpectedCharacterExceptionwith an error message indicating "Key separator before value". Otherwise, set the token at thetokenListIndexto be a new token representing the parsed value and return true from the method. - If the character is the attribute separator (
OBJECT_ATTRIBUTE_SEP), check if the current index of the character source is the same as thetokenListIndex. If they are equal, throw anUnexpectedCharacterExceptionwith an error message indicating "Key separator before value". Otherwise, set the token at thetokenListIndexto be a new token representing the parsed value and return false from the method. - If the current character does not match any of the above cases, throw an
UnexpectedCharacterExceptionwith an appropriate error message.
- If the character is the end of an object (
private void parseObject(final CharSource source, TokenList tokens) {
levelCheck(source);
final int startSourceIndex = source.getIndex();
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
boolean done = false;
while (!done) {
done = parseKey(source, tokens);
if (!done)
done = parseValue(source, tokens);
}
source.next();
tokens.set(tokenListIndex, new Token(startSourceIndex, source.getIndex(), TokenTypes.OBJECT_TOKEN));
}The parseObject method is a private method defined in the class io.nats.jparse.parser.indexoverlay.JsonStrictParser. This method takes two parameters: source, which is an instance of the CharSource class, and tokens, which is an instance of the TokenList class.
The purpose of this method is to parse an object from the given source and add the corresponding token to the tokens list.
Here is a step-by-step description of what the parseObject method is doing based on its body:
-
The method starts by calling the
levelCheckmethod passing thesourceas a parameter, which ensures that the nesting level of the JSON object is within certain bounds. -
The next two lines of code store the current index of the
sourceand the index of thetokenslist. -
The method then adds a placeholder token to the
tokenslist using theplaceHoldermethod. -
The method enters a loop that continues until the parsing is done.
-
Inside the loop, the method first calls the
parseKeymethod passing thesourceandtokensas parameters. TheparseKeymethod is not shown in the provided code, but it is assumed to return a boolean value indicating whether the key parsing is done or not. The return value is stored in thedonevariable. -
If the key parsing is not done (i.e.,
doneis false), the method calls theparseValuemethod passing thesourceandtokensas parameters. TheparseValuemethod is not shown in the provided code, but it is assumed to return a boolean value indicating whether the value parsing is done or not. The return value is also stored in thedonevariable. -
The loop continues until both the key and value parsing is done.
-
After the loop is done, the next line of code calls the
nextmethod on thesourceto advance to the next character in the input. -
Finally, the last line of code sets the token at the
tokenListIndexin thetokenslist to a newTokenobject. ThisTokenobject is created with thestartSourceIndex, which is the starting index of the source, the current index of the source, and theTokenTypes.OBJECT_TOKENtype.
Overall, the parseObject method is responsible for parsing a JSON object from the source and adding a token representing that object to the tokens list.

private void levelCheck(CharSource source) {
nestLevel++;
if (nestLevel > NEST_LEVEL) {
throw new UnexpectedCharacterException("Next level violation", "Too many levels " + nestLevel, source);
}
}This method is defined in the JsonStrictParser class, which resides in the io.nats.jparse.parser.indexoverlay package. It performs the following steps:
- Increments the value of the
nestLevelvariable by 1. - Checks if the value of
nestLevelis greater thanNEST_LEVEL. - If the condition evaluates to true, it throws an
UnexpectedCharacterException.- The exception message is set to "Next level violation".
- The exception details include "Too many levels" followed by the value of
nestLevel. - The
sourceparameter is also passed to the exception constructor.
This method is used to ensure that the nesting level in a JSON input does not exceed a predefined threshold (NEST_LEVEL). If the nesting level exceeds this threshold, an exception is thrown.

The JsonFastParser class is an implementation of the JsonParser interface that provides methods for scanning and parsing JSON strings. It offers functionality for scanning a character source and returning a list of tokens, as well as parsing a character source and returning a root node representing the parsed JSON. The class also includes default methods for parsing and scanning strings and extends the ParseConstants interface, which defines constants used for parsing JSON strings.
private List<Token> scan(final CharSource source, TokenList tokens) {
int ch = source.nextSkipWhiteSpace();
switch(ch) {
case OBJECT_START_TOKEN:
parseObject(source, tokens);
break;
case ARRAY_START_TOKEN:
parseArray(source, tokens);
break;
case TRUE_BOOLEAN_START:
parseTrue(source, tokens);
break;
case FALSE_BOOLEAN_START:
parseFalse(source, tokens);
break;
case NULL_START:
parseNull(source, tokens);
break;
case STRING_START_TOKEN:
parseString(source, tokens);
break;
case NUM_0:
case NUM_1:
case NUM_2:
case NUM_3:
case NUM_4:
case NUM_5:
case NUM_6:
case NUM_7:
case NUM_8:
case NUM_9:
case MINUS:
case PLUS:
parseNumber(source, tokens);
break;
default:
throw new UnexpectedCharacterException("Scanning JSON", "Unexpected character", source, (char) ch);
}
return tokens;
}The method scan is defined in the JsonFastParser class in the package io.nats.jparse.parser.indexoverlay. It takes two parameters, a CharSource object named source and a TokenList object named tokens. The purpose of this method is to scan the given input source character by character and determine the appropriate action to take based on the encountered character.
Here is a step-by-step description of what the scan method does based on its body:
-
The method starts by calling the
nextSkipWhiteSpacemethod on thesourceobject, which returns the next non-whitespace character from the input source. This character is stored in the variablech. -
A switch statement is used to determine the action to take based on the value of
ch. -
If
chis equal toOBJECT_START_TOKEN, theparseObjectmethod is called with thesourceandtokensobjects as parameters. This method parses an object from the input source and adds the corresponding tokens to thetokenslist. -
If
chis equal toARRAY_START_TOKEN, theparseArraymethod is called with thesourceandtokensobjects as parameters. This method parses an array from the input source and adds the corresponding tokens to thetokenslist. -
If
chis equal toTRUE_BOOLEAN_START, theparseTruemethod is called with thesourceandtokensobjects as parameters. This method parses atrueboolean value from the input source and adds the corresponding token to thetokenslist. -
If
chis equal toFALSE_BOOLEAN_START, theparseFalsemethod is called with thesourceandtokensobjects as parameters. This method parses afalseboolean value from the input source and adds the corresponding token to thetokenslist. -
If
chis equal toNULL_START, theparseNullmethod is called with thesourceandtokensobjects as parameters. This method parses anullvalue from the input source and adds the corresponding token to thetokenslist. -
If
chis equal toSTRING_START_TOKEN, theparseStringmethod is called with thesourceandtokensobjects as parameters. This method parses a string from the input source and adds the corresponding token to thetokenslist. -
If
chis equal toNUM_0,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9,MINUS, orPLUS, theparseNumbermethod is called with thesourceandtokensobjects as parameters. This method parses a number from the input source and adds the corresponding token to thetokenslist. -
If none of the above cases match, an
UnexpectedCharacterExceptionis thrown with an error message indicating that the character is unexpected. -
Finally, the
tokenslist is returned.
In summary, the scan method scans the input source character by character and performs different parsing actions based on the encountered character, such as parsing objects, arrays, booleans, strings, numbers, or throwing an exception for unexpected characters. The method then returns the list of tokens that were parsed.

private void parseArray(final CharSource source, final TokenList tokens) {
final int startSourceIndex = source.getIndex();
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
boolean done = false;
while (!done) {
done = parseArrayItem(source, tokens);
}
final Token arrayToken = new Token(startSourceIndex, source.getIndex(), TokenTypes.ARRAY_TOKEN);
tokens.set(tokenListIndex, arrayToken);
}The parseArray method in the JsonFastParser class is responsible for parsing an array from a given CharSource and storing the resulting tokens in a TokenList object.
Here is a step-by-step description of what the method does:
- It starts by storing the current index of the
CharSourcein a variable calledstartSourceIndex. - It also stores the current index of the
TokenListin a variable calledtokenListIndex. - It adds a placeholder token to the
TokenListusing theplaceHolder()method. This placeholder token will be replaced with the actual array token later. - It initializes a boolean variable
doneto false. This variable will be used to determine when to stop parsing the array. - It enters a while loop that continues until the
donevariable becomes true. - Inside the while loop, it calls the
parseArrayItemmethod, passing thesourceandtokensobjects. This method is responsible for parsing a single item of the array. - The return value of the
parseArrayItemmethod is then used to update thedonevariable. If theparseArrayItemmethod returns true, it means that it has successfully parsed an array item. If it returns false, it means that there are no more array items to parse, and the loop should be terminated. - After the while loop finishes, it creates a
Tokenobject calledarrayTokenwith the start and end indices of the array in theCharSource, and the typeTokenTypes.ARRAY_TOKEN. - Finally, it replaces the placeholder token in the
TokenListat thetokenListIndexwith thearrayTokenusing thesetmethod.
This completes the step-by-step description of the parseArray method in the JsonFastParser class.

private boolean parseArrayItem(CharSource source, TokenList tokens) {
char ch = (char) source.nextSkipWhiteSpace();
forLoop: for (; ch != ETX; ch = (char) source.nextSkipWhiteSpace()) {
switch(ch) {
case OBJECT_START_TOKEN:
parseObject(source, tokens);
break forLoop;
case ARRAY_START_TOKEN:
parseArray(source, tokens);
break forLoop;
case TRUE_BOOLEAN_START:
parseTrue(source, tokens);
break forLoop;
case FALSE_BOOLEAN_START:
parseFalse(source, tokens);
break forLoop;
case NULL_START:
parseNull(source, tokens);
break forLoop;
case STRING_START_TOKEN:
parseString(source, tokens);
break forLoop;
case NUM_0:
case NUM_1:
case NUM_2:
case NUM_3:
case NUM_4:
case NUM_5:
case NUM_6:
case NUM_7:
case NUM_8:
case NUM_9:
case MINUS:
case PLUS:
parseNumber(source, tokens);
break forLoop;
case ARRAY_END_TOKEN:
source.next();
return true;
case ARRAY_SEP:
source.next();
return false;
default:
throw new UnexpectedCharacterException("Parsing Array Item", "Unexpected character", source, ch);
}
}
if (source.getCurrentChar() == ARRAY_END_TOKEN) {
source.next();
return true;
}
return false;
}The method parseArrayItem in the JsonFastParser class is used to parse an individual item within a JSON array. Here is a step-by-step description of what this method does:
-
The method takes two parameters:
source, which is the source of the characters to be parsed, andtokens, which is a list to store the parsed tokens. -
It starts by reading the next character from the
sourceand assigns it to the variablech. The whitespace characters are skipped. -
It enters a
forloop, labeled asforLoop, which iterates until the characterchisETX(End of Text). -
Inside the loop, there is a
switchstatement that checks the value of the characterch. -
If
chis equal toOBJECT_START_TOKEN, it calls theparseObjectmethod to parse the JSON object, and thenbreak forLoopis executed, which exits theforloop. -
If
chis equal toARRAY_START_TOKEN, it calls theparseArraymethod to parse the JSON array, and thenbreak forLoopis executed. -
If
chis equal toTRUE_BOOLEAN_START, it calls theparseTruemethod to parse a boolean value of true, and thenbreak forLoopis executed. -
If
chis equal toFALSE_BOOLEAN_START, it calls theparseFalsemethod to parse a boolean value of false, and thenbreak forLoopis executed. -
If
chis equal toNULL_START, it calls theparseNullmethod to parse a null value, and thenbreak forLoopis executed. -
If
chis equal toSTRING_START_TOKEN, it calls theparseStringmethod to parse a string value, and thenbreak forLoopis executed. -
If
chis a numeric digit (NUM_0toNUM_9) or a minus or plus sign (MINUSorPLUS), it calls theparseNumbermethod to parse a numeric value, and thenbreak forLoopis executed. -
If
chis equal toARRAY_END_TOKEN, it moves to the next character in thesourceand returnstrue, indicating that the parsing of the array item is complete. -
If
chis equal toARRAY_SEP, it moves to the next character in thesourceand returnsfalse, indicating that there are more items in the array to be parsed. -
If none of the above cases match, it throws an
UnexpectedCharacterExceptionwith an error message indicating that an unexpected character was encountered while parsing the array item. -
After the
forloop, it checks if the current character in thesourceis equal toARRAY_END_TOKEN. If it is, the method moves to the next character, returnstrue, indicating that the parsing of the array item is complete. -
If the current character is not
ARRAY_END_TOKEN, the method returnsfalse, indicating that there are additional array items to be parsed.
That's the step-by-step description of the parseArrayItem method.
private boolean parseKey(final CharSource source, final TokenList tokens) {
int ch = source.nextSkipWhiteSpace();
final int startIndex = source.getIndex() - 1;
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
boolean found = false;
switch(ch) {
case STRING_START_TOKEN:
final int strStartIndex = startIndex + 1;
final int strEndIndex;
if (objectsKeysCanBeEncoded) {
strEndIndex = source.findEndOfEncodedString();
} else {
strEndIndex = source.findEndString();
}
tokens.add(new Token(strStartIndex + 1, strEndIndex, TokenTypes.STRING_TOKEN));
found = true;
break;
case OBJECT_END_TOKEN:
tokens.undoPlaceholder();
return true;
default:
throw new UnexpectedCharacterException("Parsing key", "Unexpected character found", source);
}
boolean done = source.findObjectEndOrAttributeSep();
if (!done && found) {
tokens.set(tokenListIndex, new Token(startIndex + 1, source.getIndex(), TokenTypes.ATTRIBUTE_KEY_TOKEN));
} else if (found && done) {
throw new UnexpectedCharacterException("Parsing key", "Not found", source);
}
return done;
}The parseKey method in the class io.nats.jparse.parser.indexoverlay.JsonFastParser is used to parse a key from a JSON object. Here is a step-by-step description of what the method does based on its body:
-
It begins by reading the next character from the input source and skipping any whitespace characters.
-
The method keeps track of the starting index of the key and the current index of the token list.
-
It adds a placeholder token to the token list to reserve a position for the parsed key.
-
The method then checks the value of the currently read character (ch) using a switch statement.
-
If the character is the start of a string (STRING_START_TOKEN), the method proceeds to parse the string key. It determines the start position of the string key and then finds the end position by either decoding the string or finding the end of the string. It adds a new token to the token list with the start and end positions of the string key, marked as a STRING_TOKEN. It sets the "found" variable to true.
-
If the character is an object end token (OBJECT_END_TOKEN), it means that the parsing of the current JSON object is complete. The method removes the placeholder token from the token list and returns true to indicate that the parsing of the key is done.
-
If the character is neither a string start token nor an object end token, it throws an UnexpectedCharacterException with an appropriate error message indicating that an unexpected character was found while parsing the key.
-
After handling the character, the method proceeds to find the end of the current JSON object or the separator between attributes.
-
If the end of the object or attribute separator is not found and a key has been found, it updates the token list with the correct start and end positions of the key as an ATTRIBUTE_KEY_TOKEN.
-
If a key has been found but the end of the object or attribute separator is found, it throws an UnexpectedCharacterException with an appropriate error message indicating that the end was not found.
-
Finally, the method returns the value of the "done" variable, which indicates whether the parsing of the key is complete or not.
This method provides a step-by-step process for parsing a key from a JSON object and handles different scenarios like string keys, object ends, and unexpected characters.

private boolean parseValue(final CharSource source, TokenList tokens) {
int ch = source.nextSkipWhiteSpace();
final int startIndex = source.getIndex();
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
switch(ch) {
case OBJECT_START_TOKEN:
parseObject(source, tokens);
break;
case ARRAY_START_TOKEN:
parseArray(source, tokens);
break;
case TRUE_BOOLEAN_START:
parseTrue(source, tokens);
break;
case FALSE_BOOLEAN_START:
parseFalse(source, tokens);
break;
case NULL_START:
parseNull(source, tokens);
break;
case STRING_START_TOKEN:
parseString(source, tokens);
break;
case NUM_0:
case NUM_1:
case NUM_2:
case NUM_3:
case NUM_4:
case NUM_5:
case NUM_6:
case NUM_7:
case NUM_8:
case NUM_9:
case MINUS:
case PLUS:
parseNumber(source, tokens);
break;
default:
throw new UnexpectedCharacterException("Parsing Value", "Unexpected character", source, ch);
}
ch = source.skipWhiteSpace();
switch(ch) {
case OBJECT_END_TOKEN:
if (source.getIndex() == tokenListIndex) {
throw new UnexpectedCharacterException("Parsing Value", "Key separator before value", source);
}
tokens.set(tokenListIndex, new Token(startIndex, source.getIndex(), TokenTypes.ATTRIBUTE_VALUE_TOKEN));
return true;
case OBJECT_ATTRIBUTE_SEP:
if (source.getIndex() == tokenListIndex) {
throw new UnexpectedCharacterException("Parsing Value", "Key separator before value", source);
}
tokens.set(tokenListIndex, new Token(startIndex, source.getIndex(), TokenTypes.ATTRIBUTE_VALUE_TOKEN));
return false;
default:
throw new UnexpectedCharacterException("Parsing Value", "Unexpected character", source, source.getCurrentChar());
}
}The parseValue method in the JsonFastParser class is responsible for parsing a JSON value from the provided source and populating the given tokens list with the parsed data.
Here is a step-by-step description of what the method does based on the given code:
-
Read the next character from the source while skipping any white space characters.
-
Get the current index of the source and the index of the
tokenslist. -
Insert a placeholder token at the current index in the
tokenslist. -
Use a switch statement to handle different types of characters:
- If the character is
{(indicating the start of an object), call theparseObjectmethod to parse the object. - If the character is
[(indicating the start of an array), call theparseArraymethod to parse the array. - If the character is
t(indicating the start of atrueboolean value), call theparseTruemethod to parse thetruevalue. - If the character is
f(indicating the start of afalseboolean value), call theparseFalsemethod to parse thefalsevalue. - If the character is
n(indicating the start of anullvalue), call theparseNullmethod to parse thenullvalue. - If the character is
"(indicating the start of a string), call theparseStringmethod to parse the string. - If the character is a digit (indicating the start of a number) or a minus or plus sign, call the
parseNumbermethod to parse the number. - If none of the above cases match, throw an
UnexpectedCharacterException.
- If the character is
-
Skip any remaining white space characters in the source.
-
Use another switch statement to handle different types of characters:
- If the character is
}(indicating the end of an object), check if the current index of the source is the same as the index of thetokenslist. If they are the same, throw anUnexpectedCharacterExceptionindicating that a key separator was found before a value. Otherwise, update the token at thetokenListIndexwith the start index and end index of the parsed value and returntrue. - If the character is
:(indicating a key separator), check if the current index of the source is the same as the index of thetokenslist. If they are the same, throw anUnexpectedCharacterExceptionindicating that a key separator was found before a value. Otherwise, update the token at thetokenListIndexwith the start index and end index of the parsed value and returnfalse. - If none of the above cases match, throw an
UnexpectedCharacterExceptionindicating that an unexpected character was found.
- If the character is
In summary, the parseValue method parses a JSON value from the source and updates the tokens list with the start and end indexes of the parsed value. It also checks for unexpected characters or incorrect placement of key separators before values.

private void parseObject(final CharSource source, TokenList tokens) {
final int startSourceIndex = source.getIndex();
final int tokenListIndex = tokens.getIndex();
tokens.placeHolder();
boolean done = false;
while (!done) {
done = parseKey(source, tokens);
if (!done)
done = parseValue(source, tokens);
}
source.next();
tokens.set(tokenListIndex, new Token(startSourceIndex, source.getIndex(), TokenTypes.OBJECT_TOKEN));
}The parseObject method in the JsonFastParser class is responsible for parsing a JSON object. Below is a step-by-step breakdown of what this method does based on its body:
-
The method starts by storing the current index of the input source and the index of the token list.
-
It creates a placeholder token in the token list to mark the starting point of the object.
-
It initiates a loop that continues until the parsing of the object is complete. The variable
doneis used as a flag to indicate if the parsing is done. -
Within the loop, the method calls the
parseKeymethod, passing the input source and token list. TheparseKeymethod is expected to parse the next key in the object and return a boolean value indicating whether the parsing is done. -
If the
parseKeymethod returnsfalse, indicating that the parsing of the key is not done, the method calls theparseValuemethod. TheparseValuemethod is responsible for parsing the corresponding value of the key and returns a boolean value indicating whether the parsing is done. -
The loop continues until either the
parseKeyorparseValuemethods indicate that the parsing is done. -
After the loop, the method calls
source.next()to move the input source to the next character. -
Finally, the method updates the token in the token list with the index range of the parsed object, using the
setmethod. The token is marked as an "OBJECT_TOKEN" to indicate that it represents a JSON object.
Note: The specific implementation details of the parseKey and parseValue methods are not provided in the given code snippet. These methods are likely responsible for parsing the key-value pairs within the JSON object.
- Java Open AI Client
- Using ChatGpt embeddings and hyde to improve search results
- Anthropics Claude Chatbot Gets Upgrade
- Elon Musks XAi new frontier for artificial intelligence
- Using Mockito to test JAI Java Open AI Client
- Fine tuning journey with Open AI API
- Using Open AI to create callback functions, the basis for plugins
- Using Java Open AI Client Async
- Fastest Java JSON Parser
- Java Open AI API Client on Github
- Medium: Introducing Java Open AI Client
- Medium: Using ChatGPT, Embeddings, and HyDE to Improve Search Results