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

The following examples show how to use com.alibaba.fastjson.parser.JSONLexer#stringVal() . 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: 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 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: 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 4
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 5
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 6
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 7
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 8
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);
}