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

JsonParserBuilder
Builder class for creating instances of JsonParser.
Please refer to JsonParserBuilder#build() for details on how the build logic is formed.
public JsonParser build() {
if (strict()) {
return new JsonStrictParser(objectsKeysCanBeEncoded());
} else if (isSupportNoQuoteKeys() || isAllowHashComment() || isAllowSlashSlashComment() || isAllowSlashStarComment() || parseKey != null) {
final ParseFunction[] funcTable = this.getFuncTable();
funcTable[ParseConstants.STRING_START_TOKEN] = JsonParserFunctions::parseString;
funcTable[ParseConstants.NULL_START] = JsonParserFunctions::parseNull;
funcTable[ParseConstants.TRUE_BOOLEAN_START] = JsonParserFunctions::parseTrue;
funcTable[ParseConstants.FALSE_BOOLEAN_START] = JsonParserFunctions::parseFalse;
for (int i = ParseConstants.NUM_0; i < ParseConstants.NUM_9 + 1; i++) {
funcTable[i] = JsonParserFunctions::parseNumber;
}
funcTable[ParseConstants.MINUS] = JsonParserFunctions::parseNumber;
funcTable[ParseConstants.PLUS] = JsonParserFunctions::parseNumber;
if (isAllowHashComment()) {
funcTable['#'] = (source, tokens) -> source.findChar('\n');
}
if (isSupportNoQuoteKeys() && getParseKey() == null) {
setParseKey(JsonParserFunctions::parseKeyNoQuote);
}
if (isAllowSlashStarComment() || isAllowSlashSlashComment()) {
funcTable['/'] = (source, tokens) -> {
int next = source.next();
if (next == '/') {
source.findChar('\n');
} else if (next == '*') {
loop: while (source.findChar('*')) {
switch(source.next()) {
case '/':
break loop;
case ParseConstants.ETX:
throw new UnexpectedCharacterException("Comment parse", "End of stream", source);
}
}
}
};
}
return new JsonFuncParser(objectsKeysCanBeEncoded(), Arrays.copyOf(funcTable, funcTable.length), this.getDefaultFunc(), this.getParseKey());
} else {
return new JsonFastParser(objectsKeysCanBeEncoded());
}
}The build() method in the JsonParserBuilder class is responsible for creating and returning an instance of JsonParser based on the configuration set in the builder. Here is a step-by-step description of what the method is doing:
- If the
strict()flag is set to true, a newJsonStrictParserinstance is created with theobjectsKeysCanBeEncoded()flag and returned. - If the
isSupportNoQuoteKeys(),isAllowHashComment(),isAllowSlashSlashComment(), orisAllowSlashStarComment()flags are set to true, or if theparseKeyis not null, additional configuration is applied to theJsonParser. - An array of
ParseFunctioncalledfuncTableis retrieved using thegetFuncTable()method. - The
funcTableis populated with different parse functions for each token or character:- The
STRING_START_TOKENis mapped toJsonParserFunctions::parseString. - The
NULL_STARTis mapped toJsonParserFunctions::parseNull. - The
TRUE_BOOLEAN_STARTis mapped toJsonParserFunctions::parseTrue. - The
FALSE_BOOLEAN_STARTis mapped toJsonParserFunctions::parseFalse. - The numbers from
NUM_0toNUM_9are mapped toJsonParserFunctions::parseNumber. - The
MINUSis mapped toJsonParserFunctions::parseNumber. - The
PLUSis mapped toJsonParserFunctions::parseNumber. - If the
isAllowHashComment()flag is true, the#character is mapped to a lambda function that finds the next character on a new line. - If the
isSupportNoQuoteKeys()flag is true and theparseKeyis null, theparseKeyNoQuotefunction is set as theparseKeyin the builder. - If the
isAllowSlashStarComment()orisAllowSlashSlashComment()flags are true, the/character is mapped to a lambda function that handles comments. If the next character is/, it finds the next character on a new line. If the next character is*, it processes the comment until it finds*/.
- The
- Finally, if none of the above conditions are met, a new
JsonFastParserinstance is created with theobjectsKeysCanBeEncoded()flag and returned.
The returned JsonParser instance has different behavior based on the configuration options set in the builder.

public JsonEventParser buildEventParser() {
if (strict()) {
return new JsonEventStrictParser(objectsKeysCanBeEncoded(), tokenEventListener());
} else {
return new JsonEventFastParser(objectsKeysCanBeEncoded(), tokenEventListener());
}
}The buildEventParser method in the JsonParserBuilder class is designed to create and return an instance of the JsonEventParser interface. Here is a step-by-step description of what the method does based on its body:
-
Check if the
strict()method returns true.- If true, proceed to step 2.
- If false, proceed to step 3.
-
Create a new instance of the
JsonEventStrictParserclass.- Pass the result of the
objectsKeysCanBeEncoded()method as the first argument. - Pass the result of the
tokenEventListener()method as the second argument. - Return the created
JsonEventStrictParserinstance.
- Pass the result of the
-
Create a new instance of the
JsonEventFastParserclass.- Pass the result of the
objectsKeysCanBeEncoded()method as the first argument. - Pass the result of the
tokenEventListener()method as the second argument. - Return the created
JsonEventFastParserinstance.
- Pass the result of the
Note that the choice between using JsonEventStrictParser and JsonEventFastParser depends on the return value of the strict() method. If strict() is true, the strict parser is used; otherwise, the fast parser is used.

- 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