com.alibaba.fastjson.parser.JSONToken Java Examples

The following examples show how to use com.alibaba.fastjson.parser.JSONToken. 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: 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 #2
Source File: PageDeserializer.java    From phone with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked"})
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {

    	if (parser.getLexer().token() == JSONToken.NULL) {
            parser.getLexer().nextToken(JSONToken.COMMA);
            return null;
        }
        String input = parser.getInput();
//        final String inputValue = input.replaceAll("\\{.+list:\\[(.++)\\]\\}", "$1");
//        ReflectionUtil.setField(ReflectionUtil.findField(parser.getClass(), "input"), parser,inputValue );
        Page<T> list = null;
        if (null==parser.getInput()||"null".equals(input)) {
			return null;
		}
        JSONObject jo = parser.parseObject();
        JSONArray ja = jo.getJSONArray("list");
        if (ja!=null) {
            List<T> vList = (List<T>) ja;
			list = new Page<>();
			list.addAll(vList);
	        list.setTotal(jo.getIntValue("total"));
		}

        return (T) list;
    }
 
Example #3
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 #4
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 #5
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 #6
Source File: JSONReader.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private void readBefore() {
    int state = context.state;
    // before
    switch (state) {
        case PropertyKey:
            parser.accept(JSONToken.COLON);
            break;
        case PropertyValue:
            parser.accept(JSONToken.COMMA, JSONToken.IDENTIFIER);
            break;
        case ArrayValue:
            parser.accept(JSONToken.COMMA);
            break;
        case StartObject:
            break;
        case StartArray:
            break;
        default:
            throw new JSONException("illegal state : " + state);
    }
}
 
Example #7
Source File: JSONReader.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public boolean hasNext() {
    if (context == null) {
        throw new JSONException("context is null");
    }

    final int token = parser.lexer.token();
    final int state = context.state;
    switch (state) {
        case StartArray:
        case ArrayValue:
            return token != JSONToken.RBRACKET;
        case StartObject:
        case PropertyValue:
            return token != JSONToken.RBRACE;
        default:
            throw new JSONException("illegal state : " + state);
    }
}
 
Example #8
Source File: JSONReader.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private void startStructure() {
    final int state = context.state;
    switch (state) {
        case PropertyKey:
            parser.accept(JSONToken.COLON);
            break;
        case PropertyValue:
        case ArrayValue:
            parser.accept(JSONToken.COMMA);
            break;
        case StartArray:
        case StartObject:
            break;
        default:
            throw new JSONException("illegal state : " + context.state);
    }
}
 
Example #9
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 #10
Source File: CollectionCodec.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    if (parser.lexer.token() == JSONToken.NULL) {
        parser.lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    
    if (type == JSONArray.class) {
        JSONArray array = new JSONArray();
        parser.parseArray(array);
        return (T) array;
    }

    Collection list = TypeUtils.createCollection(type);

    Type itemType = TypeUtils.getCollectionItemType(type);
    parser.parseArray(itemType, list, fieldName);

    return (T) list;
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
Source File: JSONReader.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public void startArray() {
    if (context == null) {
        context = new JSONStreamContext(null, StartArray);
    } else {
        startStructure();

        context = new JSONStreamContext(context, StartArray);
    }
    this.parser.accept(JSONToken.LBRACKET);
}
 
Example #19
Source File: DefaultFieldDeserializer.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public int getFastMatchToken() {
    if (fieldValueDeserilizer != null) {
        return fieldValueDeserilizer.getFastMatchToken();
    }

    return JSONToken.LITERAL_INT;
}
 
Example #20
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 #21
Source File: JSONReader.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public void startObject() {
    if (context == null) {
        context = new JSONStreamContext(null, JSONStreamContext.StartObject);
    } else {
        startStructure();
        context = new JSONStreamContext(context, JSONStreamContext.StartObject);
    }

    this.parser.accept(JSONToken.LBRACE, JSONToken.IDENTIFIER);
}
 
Example #22
Source File: ASMDeserializerFactory.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private void _quickNextToken(Context context, MethodVisitor mw, int token) {
    Label quickElse_ = new Label(), quickEnd_ = new Label();
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
    if (token == JSONToken.LBRACE) {
        mw.visitVarInsn(BIPUSH, '{');
    } else if (token == JSONToken.LBRACKET) {
        mw.visitVarInsn(BIPUSH, '[');
    } else {
        throw new IllegalStateException();
    }

    mw.visitJumpInsn(IF_ICMPNE, quickElse_);

    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(token);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);

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

    mw.visitLabel(quickEnd_);
}
 
Example #23
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 #24
Source File: ASMDeserializerFactory.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private void _deserialize_endCheck(Context context, MethodVisitor mw, Label reset_) {
    mw.visitIntInsn(ILOAD, context.var("matchedCount"));
    mw.visitJumpInsn(IFLE, reset_);

    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "token", "()I");
    mw.visitLdcInsn(JSONToken.RBRACE);
    mw.visitJumpInsn(IF_ICMPNE, reset_);

    // mw.visitLabel(nextToken_);
    _quickNextTokenComma(context, mw);
}
 
Example #25
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 #26
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 #27
Source File: JSONReader.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public void endArray() {
    this.parser.accept(JSONToken.RBRACKET);
    endStructure();
}
 
Example #28
Source File: MiscCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_STRING;
}
 
Example #29
Source File: BigIntegerCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example #30
Source File: FloatCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}