Skip to content

io_nats_jparse_parser

Rick Hightower edited this page Jul 18, 2023 · 1 revision

io.nats.jparse.parser

class diagram

JsonParserBuilder

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()

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:

  1. If the strict() flag is set to true, a new JsonStrictParser instance is created with the objectsKeysCanBeEncoded() flag and returned.
  2. If the isSupportNoQuoteKeys(), isAllowHashComment(), isAllowSlashSlashComment(), or isAllowSlashStarComment() flags are set to true, or if the parseKey is not null, additional configuration is applied to the JsonParser.
  3. An array of ParseFunction called funcTable is retrieved using the getFuncTable() method.
  4. The funcTable is populated with different parse functions for each token or character:
    • The STRING_START_TOKEN is mapped to JsonParserFunctions::parseString.
    • The NULL_START is mapped to JsonParserFunctions::parseNull.
    • The TRUE_BOOLEAN_START is mapped to JsonParserFunctions::parseTrue.
    • The FALSE_BOOLEAN_START is mapped to JsonParserFunctions::parseFalse.
    • The numbers from NUM_0 to NUM_9 are mapped to JsonParserFunctions::parseNumber.
    • The MINUS is mapped to JsonParserFunctions::parseNumber.
    • The PLUS is mapped to JsonParserFunctions::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 the parseKey is null, the parseKeyNoQuote function is set as the parseKey in the builder.
    • If the isAllowSlashStarComment() or isAllowSlashSlashComment() 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 */.
  5. Finally, if none of the above conditions are met, a new JsonFastParser instance is created with the objectsKeysCanBeEncoded() flag and returned.

The returned JsonParser instance has different behavior based on the configuration options set in the builder.

sequence diagram

public JsonEventParser buildEventParser()

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:

  1. Check if the strict() method returns true.

    • If true, proceed to step 2.
    • If false, proceed to step 3.
  2. Create a new instance of the JsonEventStrictParser class.

    • 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 JsonEventStrictParser instance.
  3. Create a new instance of the JsonEventFastParser class.

    • 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 JsonEventFastParser instance.

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.

sequence diagram

Clone this wiki locally