Java Code Examples for com.alibaba.fastjson.parser.JSONToken#COMMA

The following examples show how to use com.alibaba.fastjson.parser.JSONToken#COMMA . 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
@SuppressWarnings("unchecked")

    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        JSONLexer lexer = parser.lexer;

        if (lexer.token() == JSONToken.NULL) {
            lexer.nextToken(JSONToken.COMMA);
            return null;
        }

        if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
            throw new JSONException("syntax error");
        }
        lexer.nextToken();

        T obj;
        if (type == Point.class) {
            obj = (T) parsePoint(parser, fieldName);
        } else if (type == Rectangle.class) {
            obj = (T) parseRectangle(parser);
        } else if (type == Color.class) {
            obj = (T) parseColor(parser);
        } else if (type == Font.class) {
            obj = (T) parseFont(parser);
        } else {
            throw new JSONException("not support awt class : " + type);
        }

        ParseContext context = parser.getContext();
        parser.setContext(obj, fieldName);
        parser.setContext(context);

        return obj;
    }
 
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: JavaBeanDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
    protected static void parseArray(Collection collection, //
                              ObjectDeserializer deser, //
                              DefaultJSONParser parser, //
                              Type type, //
                              Object fieldName) {

        final JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
        int token = lexer.token();
        if (token == JSONToken.NULL) {
            lexer.nextToken(JSONToken.COMMA);
            token = lexer.token();
            return;
        }

        if (token != JSONToken.LBRACKET) {
            parser.throwException(token);
        }
        char ch = lexer.getCurrent();
        if (ch == '[') {
            lexer.next();
            lexer.setToken(JSONToken.LBRACKET);
        } else {
            lexer.nextToken(JSONToken.LBRACKET);
        }
        
        if (lexer.token() == JSONToken.RBRACKET) {
            lexer.nextToken();
            return;
        }

        int index = 0;
        for (;;) {
            Object item = deser.deserialze(parser, type, index);
            collection.add(item);
            index++;
            if (lexer.token() == JSONToken.COMMA) {
                ch = lexer.getCurrent();
                if (ch == '[') {
                    lexer.next();
                    lexer.setToken(JSONToken.LBRACKET);
                } else {
                    lexer.nextToken(JSONToken.LBRACKET);
                }
            } else {
                break;
            }
        }
        
        token = lexer.token();
        if (token != JSONToken.RBRACKET) {
            parser.throwException(token);
        }
        
        ch = lexer.getCurrent();
        if (ch == ',') {
            lexer.next();
            lexer.setToken(JSONToken.COMMA);
        } else {
            lexer.nextToken(JSONToken.COMMA);
        }
//        parser.accept(JSONToken.RBRACKET, JSONToken.COMMA);
    }
 
Example 6
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 7
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);
}
 
Example 8
Source File: JSONPath_s.java    From coming with MIT License 4 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
            JSONLexerBase lexer = (JSONLexerBase) parser.lexer;

            JSONArray array;
            if (context.object == null) {
                context.object = array = new JSONArray();
            } else {
                array = (JSONArray) context.object;
            }
            for (int i = array.size(); i < propertyNamesHash.length; ++i) {
                array.add(null);
            }

//            if (lexer.token() == JSONToken.LBRACKET) {
//                lexer.nextToken();
//                JSONArray array;
//
//                array = new JSONArray();
//                for (;;) {
//                    if (lexer.token() == JSONToken.LBRACE) {
//                        int index = lexer.seekObjectToField(propertyNamesHash);
//                        int matchStat = lexer.matchStat;
//                        if (matchStat == JSONLexer.VALUE) {
//                            Object value;
//                            switch (lexer.token()) {
//                                case JSONToken.LITERAL_INT:
//                                    value = lexer.integerValue();
//                                    lexer.nextToken();
//                                    break;
//                                case JSONToken.LITERAL_STRING:
//                                    value = lexer.stringVal();
//                                    lexer.nextToken();
//                                    break;
//                                default:
//                                    value = parser.parse();
//                                    break;
//                            }
//
//                            array.add(index, value);
//                            if (lexer.token() == JSONToken.RBRACE) {
//                                lexer.nextToken();
//                                continue;
//                            } else {
//                                lexer.skipObject();
//                            }
//                        } else {
//                            lexer.skipObject();
//                        }
//                    }
//
//                    if (lexer.token() == JSONToken.RBRACKET) {
//                        break;
//                    } else if (lexer.token() == JSONToken.COMMA) {
//                        lexer.nextToken();
//                        continue;
//                    } else {
//                        throw new JSONException("illegal json.");
//                    }
//                }
//
//                context.object = array;
//                return;
//            }

            for_:
            for (;;) {
                int index = lexer.seekObjectToField(propertyNamesHash);
                int matchStat = lexer.matchStat;
                if (matchStat == JSONLexer.VALUE) {
                    Object value;
                    switch (lexer.token()) {
                        case JSONToken.LITERAL_INT:
                            value = lexer.integerValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_FLOAT:
                            value = lexer.decimalValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_STRING:
                            value = lexer.stringVal();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        default:
                            value = parser.parse();
                            break;
                    }

                    array.set(index, value);

                    if (lexer.token() == JSONToken.COMMA) {
                        continue for_;
                    }
                }

                break;
            }
        }
 
Example 9
Source File: JSONPath_t.java    From coming with MIT License 4 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
            JSONLexerBase lexer = (JSONLexerBase) parser.lexer;

            JSONArray array;
            if (context.object == null) {
                context.object = array = new JSONArray();
            } else {
                array = (JSONArray) context.object;
            }
            for (int i = array.size(); i < propertyNamesHash.length; ++i) {
                array.add(null);
            }

//            if (lexer.token() == JSONToken.LBRACKET) {
//                lexer.nextToken();
//                JSONArray array;
//
//                array = new JSONArray();
//                for (;;) {
//                    if (lexer.token() == JSONToken.LBRACE) {
//                        int index = lexer.seekObjectToField(propertyNamesHash);
//                        int matchStat = lexer.matchStat;
//                        if (matchStat == JSONLexer.VALUE) {
//                            Object value;
//                            switch (lexer.token()) {
//                                case JSONToken.LITERAL_INT:
//                                    value = lexer.integerValue();
//                                    lexer.nextToken();
//                                    break;
//                                case JSONToken.LITERAL_STRING:
//                                    value = lexer.stringVal();
//                                    lexer.nextToken();
//                                    break;
//                                default:
//                                    value = parser.parse();
//                                    break;
//                            }
//
//                            array.add(index, value);
//                            if (lexer.token() == JSONToken.RBRACE) {
//                                lexer.nextToken();
//                                continue;
//                            } else {
//                                lexer.skipObject();
//                            }
//                        } else {
//                            lexer.skipObject();
//                        }
//                    }
//
//                    if (lexer.token() == JSONToken.RBRACKET) {
//                        break;
//                    } else if (lexer.token() == JSONToken.COMMA) {
//                        lexer.nextToken();
//                        continue;
//                    } else {
//                        throw new JSONException("illegal json.");
//                    }
//                }
//
//                context.object = array;
//                return;
//            }

            for_:
            for (;;) {
                int index = lexer.seekObjectToField(propertyNamesHash);
                int matchStat = lexer.matchStat;
                if (matchStat == JSONLexer.VALUE) {
                    Object value;
                    switch (lexer.token()) {
                        case JSONToken.LITERAL_INT:
                            value = lexer.integerValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_FLOAT:
                            value = lexer.decimalValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_STRING:
                            value = lexer.stringVal();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        default:
                            value = parser.parse();
                            break;
                    }

                    array.set(index, value);

                    if (lexer.token() == JSONToken.COMMA) {
                        continue for_;
                    }
                }

                break;
            }
        }