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

The following examples show how to use com.alibaba.fastjson.parser.JSONToken#LITERAL_INT . 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: 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 2
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 3
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 4
Source File: CalendarCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 5
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 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: BigDecimalCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 8
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 9
Source File: FloatCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 10
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 11
Source File: YearSerialization.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 12
Source File: IntegerCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 13
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 14
Source File: EnumDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 15
Source File: TimeDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 16
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 17
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 18
Source File: NumberDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 19
Source File: SqlDateDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
 
Example 20
Source File: ZoneOffsetSerialization.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}