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

The following examples show how to use com.alibaba.fastjson.parser.JSONLexer#nextTokenWithColon() . 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: AwtCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private Object parseRef(DefaultJSONParser parser, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
    String ref = lexer.stringVal();
    parser.setContext(parser.getContext(), fieldName);
    parser.addResolveTask(new DefaultJSONParser.ResolveTask(parser.getContext(), ref));
    parser.popContext();
    parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
    lexer.nextToken(JSONToken.RBRACE);
    parser.accept(JSONToken.RBRACE);
    return null;
}
 
Example 3
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());
    }

}
 
Example 4
Source File: TimeDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    JSONLexer lexer = parser.lexer;
    
    if (lexer.token() == JSONToken.COMMA) {
        lexer.nextToken(JSONToken.LITERAL_STRING);
        
        if (lexer.token() != JSONToken.LITERAL_STRING) {
            throw new JSONException("syntax error");
        }
        
        lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        
        if (lexer.token() != JSONToken.LITERAL_INT) {
            throw new JSONException("syntax error");
        }
        
        long time = lexer.longValue();
        lexer.nextToken(JSONToken.RBRACE);
        if (lexer.token() != JSONToken.RBRACE) {
            throw new JSONException("syntax error");
        }
        lexer.nextToken(JSONToken.COMMA);
        
        return (T) new java.sql.Time(time);
    }
    
    Object val = parser.parse();

    if (val == null) {
        return null;
    }

    if (val instanceof java.sql.Time) {
        return (T) val;
    } else if (val instanceof BigDecimal) {
        return (T) new java.sql.Time(TypeUtils.longValue((BigDecimal) val));
    } else if (val instanceof Number) {
        return (T) new java.sql.Time(((Number) val).longValue());
    } else if (val instanceof String) {
        String strVal = (String) val;
        if (strVal.length() == 0) {
            return null;
        }
        
        long longVal;
        JSONScanner dateLexer = new JSONScanner(strVal);
        if (dateLexer.scanISO8601DateIfMatch()) {
            longVal = dateLexer.getCalendar().getTimeInMillis();
        } else {
            boolean isDigit = true;
            for (int i = 0; i< strVal.length(); ++i) {
                char ch = strVal.charAt(i);
                if (ch < '0' || ch > '9') {
                    isDigit = false;
                    break;
                }
            }
            if (!isDigit) {
                dateLexer.close();
                return (T) java.sql.Time.valueOf(strVal);    
            }
            
            longVal = Long.parseLong(strVal);
        }
        dateLexer.close();
        return (T) new java.sql.Time(longVal);
    }
    
    throw new JSONException("parse error");
}
 
Example 5
Source File: AwtCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
protected Font parseFont(DefaultJSONParser parser) {
    JSONLexer lexer = parser.lexer;
    
    int size = 0, style = 0;
    String name = null;
    for (;;) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }

        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }


        if (key.equalsIgnoreCase("name")) {
            if (lexer.token() == JSONToken.LITERAL_STRING) {
                name = lexer.stringVal();
                lexer.nextToken();
            } else {
                throw new JSONException("syntax error");
            }
        } else if (key.equalsIgnoreCase("style")) {
            if (lexer.token() == JSONToken.LITERAL_INT) {
                style = lexer.intValue();
                lexer.nextToken();
            } else {
                throw new JSONException("syntax error");
            }
        } else if (key.equalsIgnoreCase("size")) {
            if (lexer.token() == JSONToken.LITERAL_INT) {
                size = lexer.intValue();
                lexer.nextToken();
            } else {
                throw new JSONException("syntax error");
            }
        } else {
            throw new JSONException("syntax error, " + key);
        }

        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }

    return new Font(name, style, size);
}
 
Example 6
Source File: AwtCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
protected Color parseColor(DefaultJSONParser parser) {
    JSONLexer lexer = parser.lexer;
    
    int r = 0, g = 0, b = 0, alpha = 0;
    for (;;) {
        if (lexer.token() == JSONToken.RBRACE) {
            lexer.nextToken();
            break;
        }

        String key;
        if (lexer.token() == JSONToken.LITERAL_STRING) {
            key = lexer.stringVal();
            lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
        } else {
            throw new JSONException("syntax error");
        }

        int val;
        if (lexer.token() == JSONToken.LITERAL_INT) {
            val = lexer.intValue();
            lexer.nextToken();
        } else {
            throw new JSONException("syntax error");
        }

        if (key.equalsIgnoreCase("r")) {
            r = val;
        } else if (key.equalsIgnoreCase("g")) {
            g = val;
        } else if (key.equalsIgnoreCase("b")) {
            b = val;
        } else if (key.equalsIgnoreCase("alpha")) {
            alpha = val;
        } else {
            throw new JSONException("syntax error, " + key);
        }

        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LITERAL_STRING);
        }
    }

    return new Color(r, g, b, alpha);
}