Java 类com.alibaba.fastjson.serializer.LongCodec 实例源码

项目:GitHub    文件:LongDeserializerTest.java   
public void test_bigdecimal() throws Exception {
    Assert.assertEquals(0, JSON.parseObject("0", Long.class).intValue());
    Assert.assertEquals(0, JSON.parseObject("0.0", Long.class).intValue());
    Assert.assertEquals(0, JSON.parseObject("'0'", Long.class).intValue());

    Assert.assertEquals(null, JSON.parseObject("null", Long.class));

    DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);
    Assert.assertEquals(null, LongCodec.instance.deserialze(parser, null, null));
    Assert.assertEquals(JSONToken.LITERAL_INT, LongCodec.instance.getFastMatchToken());
}
项目:itmarry    文件:DefaultJSONParser.java   
public void parseObject(Object object) {
    Class<?> clazz = object.getClass();
    Map<String, FieldDeserializer> setters = config.getFieldDeserializers(clazz);

    if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
        throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
    }

    for (;;) {
        // lexer.scanSymbol
        String key = lexer.scanSymbol(symbolTable);

        if (key == null) {
            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken(JSONToken.COMMA);
                break;
            }
            if (lexer.token() == JSONToken.COMMA) {
                if (isEnabled(Feature.AllowArbitraryCommas)) {
                    continue;
                }
            }
        }

        FieldDeserializer fieldDeser = setters.get(key);
        if (fieldDeser == null) {
            if (!isEnabled(Feature.IgnoreNotMatch)) {
                throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key);
            }

            lexer.nextTokenWithColon();
            parse(); // skip

            if (lexer.token() == JSONToken.RBRACE) {
                lexer.nextToken();
                return;
            }

            continue;
        } else {
            Class<?> fieldClass = fieldDeser.getFieldClass();
            Type fieldType = fieldDeser.getFieldType();
            Object fieldValue;
            if (fieldClass == int.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
                fieldValue = IntegerCodec.instance.deserialze(this, fieldType, null);
            } else if (fieldClass == String.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
                fieldValue = StringCodec.deserialze(this);
            } else if (fieldClass == long.class) {
                lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
                fieldValue = LongCodec.instance.deserialze(this, fieldType, null);
            } else {
                ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType);

                lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken());
                fieldValue = fieldValueDeserializer.deserialze(this, fieldType, null);
            }

            fieldDeser.setValue(object, fieldValue);
        }

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

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