Java Code Examples for com.alibaba.fastjson.parser.JSONLexer#scanSymbol()

The following examples show how to use com.alibaba.fastjson.parser.JSONLexer#scanSymbol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractResponsePayloadCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 解析应答
 *
 * @param parser 解析器
 * @param lexer  文法
 * @return 应答
 */
protected ResponsePayload parse(final DefaultJSONParser parser, final JSONLexer lexer) {
    ResponsePayload payload = new ResponsePayload();
    String key;
    int token;
    try {
        String typeName = null;
        for (; ; ) {
            // lexer.scanSymbol
            key = lexer.scanSymbol(parser.getSymbolTable());
            if (key == null) {
                token = lexer.token();
                if (token == JSONToken.RBRACE) {
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                } else if (token == JSONToken.COMMA) {
                    if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                        continue;
                    }
                }
            }
            lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
            if (RES_CLASS.equals(key)) {
                typeName = parseString(lexer, RES_CLASS, false);
            } else if (RESPONSE.equals(key)) {
                payload.setResponse(parseResponse(parser, lexer, typeName));
            } else if (EXCEPTION.equals(key)) {
                payload.setException((Throwable) parseObject(parser, lexer, getThrowableType(typeName)));
            }
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
        }
        return payload;
    } catch (ClassNotFoundException e) {
        throw new SerializerException(e.getMessage());
    }
}
 
Example 2
Source File: AbstractInvocationCodec.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
/**
 * 解析Invocation
 *
 * @param parser 解析器
 * @param lexer  文法
 * @return
 */
protected Call parse(final DefaultJSONParser parser, final JSONLexer lexer) {
    Call invocation = createInvocation();
    String key;
    int token;
    try {
        for (; ; ) {
            // lexer.scanSymbol
            key = lexer.scanSymbol(parser.getSymbolTable());
            if (key == null) {
                token = lexer.token();
                if (token == JSONToken.RBRACE) {
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                } else if (token == JSONToken.COMMA) {
                    if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                        continue;
                    }
                }
            }
            lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);

            if (classNameKey.equals(key)) {
                invocation.setClassName(parseString(lexer, classNameKey, false));
            } else if (aliasKey.equals(key)) {
                invocation.setAlias(parseString(lexer, aliasKey, true));
            } else if (methodNameKey.equals(key)) {
                invocation.setMethodName(parseString(lexer, methodNameKey, false));
            } else if (argsTypeKey.equals(key)) {
                invocation.setArgsType(parseStrings(parser, lexer, argsTypeKey));
            } else if (argsKey.equals(key)) {
                invocation.setArgs(parseObjects(parser, lexer, invocation.computeTypes(), argsKey));
            } else if (attachmentsKey.equals(key)) {
                invocation.addAttachments(parseMap(parser, lexer, attachmentsKey));
            }
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
        }
        return invocation;
    } catch (ClassNotFoundException | NoSuchMethodException | MethodOverloadException e) {
        throw new SerializerException(e.getMessage());
    }

}