com.alibaba.fastjson.parser.JSONLexer Java Examples

The following examples show how to use com.alibaba.fastjson.parser.JSONLexer. 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: CalendarCodec.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    Object value = DateCodec.instance.deserialze(parser, type, fieldName);

    if (value instanceof Calendar) {
        return (T) value;
    }

    Date date = (Date) value;
    if (date == null) {
        return null;
    }

    JSONLexer lexer = parser.lexer;
    Calendar calendar = Calendar.getInstance(lexer.getTimeZone(), lexer.getLocale());
    calendar.setTime(date);

    if (type == XMLGregorianCalendar.class) {
        return (T) createXMLGregorianCalendar((GregorianCalendar) calendar);
    }

    return (T) calendar;
}
 
Example #2
Source File: JSONReader.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public String readString() {
    Object object;
    if (context == null) {
        object = parser.parse();
    } else {
        readBefore();
        JSONLexer lexer = parser.lexer;
        if (context.state == JSONStreamContext.StartObject && lexer.token() == JSONToken.IDENTIFIER) {
            object = lexer.stringVal();
            lexer.nextToken();
        } else {
            object = parser.parse();
        }
        readAfter();
    }

    return TypeUtils.castToString(object);
}
 
Example #3
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 读取字符串
 *
 * @param lexer    文法
 * @param field    字段
 * @param nullable 是否可以null
 */
protected String parseString(final JSONLexer lexer, final String field, final boolean nullable) {
    String result = null;
    switch (lexer.token()) {
        case JSONToken.LITERAL_STRING:
            result = lexer.stringVal();
            lexer.nextToken();
            break;
        case JSONToken.NULL:
            if (!nullable) {
                throw new SerializerException("syntax error: invalid " + field);
            }
            lexer.nextToken();
            break;
        default:
            throw new SerializerException("syntax error: invalid " + field);
    }
    return result;
}
 
Example #4
Source File: BigDecimalCodec.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        BigDecimal decimalValue = lexer.decimalValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) decimalValue;
    }

    if (lexer.token() == JSONToken.LITERAL_FLOAT) {
        BigDecimal val = lexer.decimalValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) val;
    }

    Object value = parser.parse();
    return value == null //
        ? null //
        : (T) TypeUtils.castToBigDecimal(value);
}
 
Example #5
Source File: JSON.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public static JSONArray parseArray(String text) {
    if (text == null) {
        return null;
    }

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());

    JSONArray array;

    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        array = null;
    } else if (lexer.token() == JSONToken.EOF) {
        array = null;
    } else {
        array = new JSONArray();
        parser.parseArray(array);

        parser.handleResovleTask(array);
    }

    parser.close();

    return array;
}
 
Example #6
Source File: JavaBeanDeserializer.java    From uavstack with Apache License 2.0 6 votes vote down vote up
protected Enum scanEnum(JSONLexerBase lexer, char[] name_chars, ObjectDeserializer fieldValueDeserilizer) {
    EnumDeserializer enumDeserializer = null;
    if (fieldValueDeserilizer instanceof EnumDeserializer) {
        enumDeserializer = (EnumDeserializer) fieldValueDeserilizer;
    }

    if (enumDeserializer == null) {
        lexer.matchStat = JSONLexer.NOT_MATCH;
        return null;
    }

    long enumNameHashCode = lexer.scanFieldSymbol(name_chars);
    if (lexer.matchStat > 0) {
        return enumDeserializer.getEnumByHashCode(enumNameHashCode);
    } else {
        return null;
    }
}
 
Example #7
Source File: JSON.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public static <T> List<T> parseArray(String text, Class<T> clazz) {
    if (text == null) {
        return null;
    }

    List<T> list;

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    JSONLexer lexer = parser.lexer;
    int token = lexer.token();
    if (token == JSONToken.NULL) {
        lexer.nextToken();
        list = null;
    } else if (token == JSONToken.EOF && lexer.isBlankInput()) {
        list = null;
    } else {
        list = new ArrayList<T>();
        parser.parseArray(clazz, list);

        parser.handleResovleTask(list);
    }

    parser.close();

    return list;
}
 
Example #8
Source File: ArrayListTypeFieldDeserializer.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    JSONLexer lexer = parser.lexer;
    final int token = lexer.token();
    if (token == JSONToken.NULL
            || (token == JSONToken.LITERAL_STRING && lexer.stringVal().length() == 0)) {
        setValue(object, null);
        return;
    }

    ArrayList list = new ArrayList();

    ParseContext context = parser.getContext();

    parser.setContext(context, object, fieldInfo.name);
    parseArray(parser, objectType, list);
    parser.setContext(context);

    if (object == null) {
        fieldValues.put(fieldInfo.name, list);
    } else {
        setValue(object, list);
    }
}
 
Example #9
Source File: LongCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.lexer;

    Long longObject;
    try {
        final int token = lexer.token();
        if (token == JSONToken.LITERAL_INT) {
            long longValue = lexer.longValue();
            lexer.nextToken(JSONToken.COMMA);
            longObject = Long.valueOf(longValue);
        } else if (token == JSONToken.LITERAL_FLOAT) {
            BigDecimal number = lexer.decimalValue();
            longObject = TypeUtils.longValue(number);
            lexer.nextToken(JSONToken.COMMA);
        } else {
            if (token == JSONToken.LBRACE) {
                JSONObject jsonObject = new JSONObject(true);
                parser.parseObject(jsonObject);
                longObject = TypeUtils.castToLong(jsonObject);
            } else {
                Object value = parser.parse();

                longObject = TypeUtils.castToLong(value);
            }
            if (longObject == null) {
                return null;
            }
        }
    } catch (Exception ex) {
        throw new JSONException("parseLong error, field : " + fieldName, ex);
    }
    
    return clazz == AtomicLong.class //
        ? (T) new AtomicLong(longObject.longValue()) //
        : (T) longObject;
}
 
Example #10
Source File: BigIntegerCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        String val = lexer.numberString();
        lexer.nextToken(JSONToken.COMMA);
        return (T) new BigInteger(val);
    }

    Object value = parser.parse();
    return value == null //
        ? null //
        : (T) TypeUtils.castToBigInteger(value);
}
 
Example #11
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 #12
Source File: AbstractInvocationCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deserialze(final DefaultJSONParser parser, final Type type, final Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    switch (lexer.token()) {
        case JSONToken.NULL:
            lexer.nextToken();
            return null;
        case JSONToken.LBRACE:
            return (T) parse(parser, lexer);
        default:
            return null;
    }
}
 
Example #13
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 #14
Source File: ValuedEnumDeserializer.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    String stringVal = lexer.stringVal();
    
    return (T) EnumUtil.valueOf((Class<T>) type , stringVal);
}
 
Example #15
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 解析对象数组
 *
 * @param parser 解析器
 * @param lexer  语法
 * @param types  类型
 * @param field  字段
 */
protected Object[] parseObjects(final DefaultJSONParser parser, final JSONLexer lexer, final Type[] types, final String field) {
    Object[] result = null;
    //空数组
    if (lexer.token() == JSONToken.NULL) {
        if (types.length == 0) {
            lexer.nextToken();
        } else {
            throw new SerializerException("syntax error: invalid " + field);
        }
    } else {
        //解析参数
        JSONReader reader = new JSONReader(parser);
        reader.startArray();
        int i = 0;
        result = new Object[types.length];
        while (reader.hasNext()) {
            if (i >= result.length) {
                throw new SerializerException("syntax error: invalid " + field);
            }
            result[i] = reader.readObject(types[i]);
            i++;
        }
        reader.endArray();
    }
    return result;
}
 
Example #16
Source File: FastJsonSObjectCodec.java    From actframework 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.getLexer();
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String text = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
        return (T) resolver.resolve(text);
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example #17
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 读取字符串数组
 *
 * @param parser 解析器
 * @param lexer  文法
 * @param field  字段
 */
protected String[] parseStrings(final DefaultJSONParser parser, final JSONLexer lexer, final String field) {
    String result[] = null;
    switch (lexer.token()) {
        case JSONToken.LBRACKET:
            result = parser.parseObject(String[].class);
            break;
        case JSONToken.NULL:
            lexer.nextToken();
            break;
        default:
            throw new SerializerException("syntax error: invalid " + field);
    }
    return result;
}
 
Example #18
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 读取MAP
 *
 * @param parser 解析器
 * @param lexer  文法
 * @param field  字段
 */
protected Map<String, Object> parseMap(final DefaultJSONParser parser, final JSONLexer lexer, final String field) {
    Map<String, Object> result = null;
    switch (lexer.token()) {
        case JSONToken.LBRACE:
            result = parser.parseObject();
            break;
        case JSONToken.NULL:
            lexer.nextToken();
            break;
        default:
            throw new SerializerException("syntax error: invalid " + field);
    }
    return result;
}
 
Example #19
Source File: AbstractResponsePayloadCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 解析应答
 *
 * @param parser   解析器
 * @param lexer    文法
 * @param typeName 名称
 * @return 应答对象
 */
protected Object parseResponse(final DefaultJSONParser parser, final JSONLexer lexer, final String typeName) {
    if (typeName == null || typeName.isEmpty()) {
        return null;
    }
    try {
        return parseObject(parser, lexer, getType(typeName));
    } catch (ClassNotFoundException e) {
        //泛化调用情况下,类可能不存在
        //TODO 需要判断是泛化调用
        return parser.parse();
    }
}
 
Example #20
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 #21
Source File: AbstractResponsePayloadCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deserialze(final DefaultJSONParser parser, final Type type, final Object fieldName) {
    JSONLexer lexer = parser.getLexer();
    switch (lexer.token()) {
        case JSONToken.NULL:
            lexer.nextToken();
            return null;
        case JSONToken.LBRACE:
            return (T) parse(parser, lexer);
        default:
            return null;
    }
}
 
Example #22
Source File: FastJsonKeywordCodec.java    From actframework 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.getLexer();
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String text = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);
        return (T) Keyword.of(text);
    } else {
        throw new UnsupportedOperationException();
    }
}
 
Example #23
Source File: JSONReader.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public JSONReader(JSONLexer lexer){
    this(new DefaultJSONParser(lexer));
}
 
Example #24
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 #25
Source File: BooleanCodec.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) {
    final JSONLexer lexer = parser.lexer;

    Boolean boolObj;

    try {
        if (lexer.token() == JSONToken.TRUE) {
            lexer.nextToken(JSONToken.COMMA);
            boolObj = Boolean.TRUE;
        } else if (lexer.token() == JSONToken.FALSE) {
            lexer.nextToken(JSONToken.COMMA);
            boolObj = Boolean.FALSE;
        } else if (lexer.token() == JSONToken.LITERAL_INT) {
            int intValue = lexer.intValue();
            lexer.nextToken(JSONToken.COMMA);

            if (intValue == 1) {
                boolObj = Boolean.TRUE;
            } else {
                boolObj = Boolean.FALSE;
            }
        } else {
            Object value = parser.parse();

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

            boolObj = TypeUtils.castToBoolean(value);
        }
    } catch (Exception ex) {
        throw new JSONException("parseBoolean error, field : " + fieldName, ex);
    }

    if (clazz == AtomicBoolean.class) {
        return (T) new AtomicBoolean(boolObj.booleanValue());
    }

    return (T) boolObj;
}
 
Example #26
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 #27
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;
            }
        }
 
Example #28
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 #29
Source File: IntegerCodec.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) {
    final JSONLexer lexer = parser.lexer;

    final int token = lexer.token();

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


    Integer intObj;
    try {
        if (token == JSONToken.LITERAL_INT) {
            int val = lexer.intValue();
            lexer.nextToken(JSONToken.COMMA);
            intObj = Integer.valueOf(val);
        } else if (token == JSONToken.LITERAL_FLOAT) {
            BigDecimal number = lexer.decimalValue();
            intObj = TypeUtils.intValue(number);
            lexer.nextToken(JSONToken.COMMA);
        } else {
            if (token == JSONToken.LBRACE) {
                JSONObject jsonObject = new JSONObject(true);
                parser.parseObject(jsonObject);
                intObj = TypeUtils.castToInt(jsonObject);
            } else {
                Object value = parser.parse();
                intObj = TypeUtils.castToInt(value);
            }
        }
    } catch (Exception ex) {
        throw new JSONException("parseInt error, field : " + fieldName, ex);
    }

    
    if (clazz == AtomicInteger.class) {
        return (T) new AtomicInteger(intObj.intValue());
    }
    
    return (T) intObj;
}
 
Example #30
Source File: ASMDeserializerFactory.java    From uavstack with Apache License 2.0 4 votes vote down vote up
private void _quickNextTokenComma(Context context, MethodVisitor mw) {
    Label quickElse_ = new Label(), quickElseIf0_ = new Label(), quickElseIf1_ = new Label(), quickElseIf2_ = new Label(), quickEnd_ = new Label();
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
    mw.visitInsn(DUP);
    mw.visitVarInsn(ISTORE, context.var("ch"));
    mw.visitVarInsn(BIPUSH, ',');
    mw.visitJumpInsn(IF_ICMPNE, quickElseIf0_);

    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.COMMA);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);
    
    mw.visitLabel(quickElseIf0_);
    mw.visitVarInsn(ILOAD, context.var("ch"));
    mw.visitVarInsn(BIPUSH, '}');
    mw.visitJumpInsn(IF_ICMPNE, quickElseIf1_);

    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.RBRACE);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);
    
    mw.visitLabel(quickElseIf1_);
    mw.visitVarInsn(ILOAD, context.var("ch"));
    mw.visitVarInsn(BIPUSH, ']');
    mw.visitJumpInsn(IF_ICMPNE, quickElseIf2_);

    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.RBRACKET);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);
    
    mw.visitLabel(quickElseIf2_);
    mw.visitVarInsn(ILOAD, context.var("ch"));
    mw.visitVarInsn(BIPUSH, JSONLexer.EOI);
    mw.visitJumpInsn(IF_ICMPNE, quickElse_);

    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.EOF);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);

    mw.visitLabel(quickElse_);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "nextToken", "()V");

    mw.visitLabel(quickEnd_);
}