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

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

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

    DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);
    Assert.assertEquals(null, IntegerCodec.instance.deserialze(parser, null, null));
    Assert.assertEquals(JSONToken.LITERAL_INT, IntegerCodec.instance.getFastMatchToken());
}
项目:GitHub    文件:JSONSerializerMapTest.java   
public void test_0() throws Exception {
    SerializeConfig map = new SerializeConfig();

    Assert.assertFalse(0 == size(map));
    Assert.assertEquals(true, map.get(Integer.class) == IntegerCodec.instance);

    Assert.assertEquals(true, map.put(Integer.class, IntegerCodec.instance));
    Assert.assertEquals(true, map.put(Integer.class, IntegerCodec.instance));
    Assert.assertEquals(true, map.put(Integer.class, IntegerCodec.instance));

    Assert.assertEquals(true, map.get(Integer.class) == IntegerCodec.instance);

    Assert.assertFalse(0 == size(map));
}
项目:boohee_v5.6    文件:DefaultJSONParser.java   
public void parseArray(Type type, Collection array, Object fieldName) {
    if (this.lexer.token() == 21 || this.lexer.token() == 22) {
        this.lexer.nextToken();
    }
    if (this.lexer.token() != 14) {
        throw new JSONException("exepct '[', but " + JSONToken.name(this.lexer.token()));
    }
    ObjectDeserializer deserializer;
    if (Integer.TYPE == type) {
        deserializer = IntegerCodec.instance;
        this.lexer.nextToken(2);
    } else if (String.class == type) {
        deserializer = StringCodec.instance;
        this.lexer.nextToken(4);
    } else {
        deserializer = this.config.getDeserializer(type);
        this.lexer.nextToken(deserializer.getFastMatchToken());
    }
    ParseContext context = getContext();
    setContext(array, fieldName);
    int i = 0;
    while (true) {
        if (isEnabled(Feature.AllowArbitraryCommas)) {
            while (this.lexer.token() == 16) {
                this.lexer.nextToken();
            }
        }
        try {
            if (this.lexer.token() == 15) {
                break;
            }
            if (Integer.TYPE == type) {
                array.add(IntegerCodec.instance.deserialze(this, null, null));
            } else if (String.class == type) {
                String value;
                if (this.lexer.token() == 4) {
                    value = this.lexer.stringVal();
                    this.lexer.nextToken(16);
                } else {
                    Object obj = parse();
                    if (obj == null) {
                        value = null;
                    } else {
                        value = obj.toString();
                    }
                }
                array.add(value);
            } else {
                Object obj2;
                if (this.lexer.token() == 8) {
                    this.lexer.nextToken();
                    obj2 = null;
                } else {
                    obj2 = deserializer.deserialze(this, type, Integer.valueOf(i));
                }
                array.add(obj2);
                checkListResolve(array);
            }
            if (this.lexer.token() == 16) {
                this.lexer.nextToken(deserializer.getFastMatchToken());
            }
            i++;
        } finally {
            setContext(context);
        }
    }
    this.lexer.nextToken(16);
}
项目: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;
        }
    }
}