Java 类com.alibaba.fastjson.parser.JSONToken 实例源码

项目:GitHub    文件:ArrayListTypeFieldDeserializer.java   
@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);
    }
}
项目:GitHub    文件:JSONTokenTest.java   
public void test_0 () throws Exception {
    new JSONToken();

    Assert.assertEquals("int", JSONToken.name(JSONToken.LITERAL_INT));
    Assert.assertEquals("float", JSONToken.name(JSONToken.LITERAL_FLOAT));
    Assert.assertEquals("string", JSONToken.name(JSONToken.LITERAL_STRING));
    Assert.assertEquals("iso8601", JSONToken.name(JSONToken.LITERAL_ISO8601_DATE));
    Assert.assertEquals("true", JSONToken.name(JSONToken.TRUE));
    Assert.assertEquals("false", JSONToken.name(JSONToken.FALSE));
    Assert.assertEquals("null", JSONToken.name(JSONToken.NULL));
    Assert.assertEquals("new", JSONToken.name(JSONToken.NEW));
    Assert.assertEquals("(", JSONToken.name(JSONToken.LPAREN));
    Assert.assertEquals(")", JSONToken.name(JSONToken.RPAREN));
    Assert.assertEquals("{", JSONToken.name(JSONToken.LBRACE));
    Assert.assertEquals("}", JSONToken.name(JSONToken.RBRACE));
    Assert.assertEquals("[", JSONToken.name(JSONToken.LBRACKET));
    Assert.assertEquals("]", JSONToken.name(JSONToken.RBRACKET));
    Assert.assertEquals(",", JSONToken.name(JSONToken.COMMA));
    Assert.assertEquals(":", JSONToken.name(JSONToken.COLON));
    Assert.assertEquals("ident", JSONToken.name(JSONToken.IDENTIFIER));
    Assert.assertEquals("fieldName", JSONToken.name(JSONToken.FIELD_NAME));
    Assert.assertEquals("EOF", JSONToken.name(JSONToken.EOF));
    Assert.assertEquals("Unknown", JSONToken.name(Integer.MAX_VALUE));
    Assert.assertEquals("Set", JSONToken.name(JSONToken.SET));
    Assert.assertEquals("TreeSet", JSONToken.name(JSONToken.TREE_SET));
}
项目:uavstack    文件:BigIntegerCodec.java   
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_INT) {
        String val = lexer.numberString();
        lexer.nextToken(JSONToken.COMMA);
        return (T) new BigInteger(val);
    }

    Object value = parser.parse();

    if (value == null) {
        return null;
    }

    return (T) TypeUtils.castToBigInteger(value);
}
项目:GitHub    文件:JSONReader.java   
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);
    }
}
项目:GitHub    文件:JSONReader.java   
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);
}
项目:GitHub    文件:CollectionCodec.java   
@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;
}
项目:GitHub    文件:JSON.java   
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;
}
项目:GitHub    文件:JSON.java   
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;
}
项目:uavstack    文件:JSON.java   
public static final JSONArray parseArray(String text) {
    if (text == null) {
        return null;
    }

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());

    JSONArray array;

    JSONLexer lexer = parser.getLexer();
    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;
}
项目:GitHub    文件:BooleanDeserializerTest.java   
public void test_boolean() throws Exception {
    Assert.assertEquals(Boolean.TRUE, JSON.parseObject("true", Boolean.class));
    Assert.assertEquals(Boolean.FALSE, JSON.parseObject("false", Boolean.class));

    Assert.assertEquals(Boolean.TRUE, JSON.parseObject("'true'", Boolean.class));
    Assert.assertEquals(Boolean.FALSE, JSON.parseObject("'false'", Boolean.class));

    Assert.assertEquals(Boolean.TRUE, JSON.parseObject("1", Boolean.class));
    Assert.assertEquals(Boolean.FALSE, JSON.parseObject("0", Boolean.class));

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

    {
        DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(),
                                                         JSON.DEFAULT_PARSER_FEATURE);
        Assert.assertEquals(null, BooleanCodec.instance.deserialze(parser, null, null));
        parser.close();
    }
    Assert.assertEquals(JSONToken.TRUE, BooleanCodec.instance.getFastMatchToken());
}
项目:uavstack    文件:LongCodec.java   
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    final JSONLexer lexer = parser.getLexer();

    Long longObject;
    if (lexer.token() == JSONToken.LITERAL_INT) {
        long longValue = lexer.longValue();
        lexer.nextToken(JSONToken.COMMA);
        longObject = Long.valueOf(longValue);
    } else {

        Object value = parser.parse();

        if (value == null) {
            return null;
        }

        longObject = TypeUtils.castToLong(value);
    }

    if (clazz == AtomicLong.class) {
        return (T) new AtomicLong(longObject.longValue());
    }

    return (T) longObject;
}
项目:WeiMusicCommunity-server    文件:JodaTimeDeserializer.java   
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();
        if (type == LocalDateTime.class) {
            return (T) LocalDateTime.parse(text);
        } else if (type == LocalDate.class) {
            return (T) LocalDate.parse(text);
        } else if (type == LocalTime.class) {
            return (T) LocalTime.parse(text);
        } else if (type == Period.class) {
            return (T) Period.parse(text);
        } else if (type == Duration.class) {
            return (T) Duration.parse(text);
        } else if (type == Instant.class) {
            return (T) Instant.parse(text);
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return null;
}
项目:GitHub    文件:DefaultFieldDeserializer.java   
public int getFastMatchToken() {
    if (fieldValueDeserilizer != null) {
        return fieldValueDeserilizer.getFastMatchToken();
    }

    return JSONToken.LITERAL_INT;
}
项目:uavstack    文件:ArrayListTypeFieldDeserializer.java   
@SuppressWarnings({ "unchecked", "rawtypes" })
public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
    Type itemType = this.itemType;
    ObjectDeserializer itemTypeDeser = this.deserializer;

    if (itemType instanceof TypeVariable //
        && objectType instanceof ParameterizedType) {
        TypeVariable typeVar = (TypeVariable) itemType;
        ParameterizedType paramType = (ParameterizedType) objectType;

        Class<?> objectClass = null;
        if (paramType.getRawType() instanceof Class) {
            objectClass = (Class<?>) paramType.getRawType();
        }

        int paramIndex = -1;
        if (objectClass != null) {
            for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                TypeVariable item = objectClass.getTypeParameters()[i];
                if (item.getName().equals(typeVar.getName())) {
                    paramIndex = i;
                    break;
                }
            }
        }

        if (paramIndex != -1) {
            itemType = paramType.getActualTypeArguments()[paramIndex];
            if (!itemType.equals(this.itemType)) {
                itemTypeDeser = parser.getConfig().getDeserializer(itemType);
            }
        }
    }

    final JSONLexer lexer = parser.getLexer();

    if (lexer.token() != JSONToken.LBRACKET) {
        String errorMessage = "exepct '[', but " + JSONToken.name(lexer.token());
        if (objectType != null) {
            errorMessage += ", type : " + objectType;
        }
        throw new JSONException(errorMessage);
    }

    if (itemTypeDeser == null) {
        itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
        itemFastMatchToken = deserializer.getFastMatchToken();
    }

    lexer.nextToken(itemFastMatchToken);

    for (int i = 0;; ++i) {
        if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
            while (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken();
                continue;
            }
        }

        if (lexer.token() == JSONToken.RBRACKET) {
            break;
        }

        Object val = itemTypeDeser.deserialze(parser, itemType, i);
        array.add(val);

        parser.checkListResolve(array);

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

    lexer.nextToken(JSONToken.COMMA);
}
项目:GitHub    文件:JSONReader.java   
public void startArray() {
    if (context == null) {
        context = new JSONStreamContext(null, StartArray);
    } else {
        startStructure();

        context = new JSONStreamContext(context, StartArray);
    }
    this.parser.accept(JSONToken.LBRACKET);
}
项目:GitHub    文件:BigIntegerCodec.java   
@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);
}
项目:GitHub    文件:LongCodec.java   
@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.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;
}
项目:GitHub    文件:AwtCodec.java   
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;
}
项目:GitHub    文件:MediaContentDeserializer.java   
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexer lexer = parser.getLexer();

    parser.accept(JSONToken.LBRACKET, JSONToken.LBRACKET);
    Media media = mediaDeser.deserialze(parser, Media.class, "media");
    parser.accept(JSONToken.COMMA, JSONToken.LBRACKET);

    parser.accept(JSONToken.LBRACKET, JSONToken.LBRACKET);
    List<Image> images = new ArrayList<Image>();
    int index = 0;
    for (;;) {
        Image image = imageDesc.deserialze(parser, Image.class, index);
        images.add(image);
        index++;
        if (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken(JSONToken.LBRACKET);
        } else {
            break;
        }
    }
    parser.accept(JSONToken.RBRACKET, JSONToken.RBRACKET);

    parser.accept(JSONToken.RBRACKET, JSONToken.EOF);

    MediaContent content = new MediaContent();
    content.setMedia(media);
    content.setImages(images);
    return (T) content;
}
项目:GitHub    文件:JSONReaderTest_1.java   
public void test_read() throws Exception {
    String text = "{\"id\":1001}";
    JSONReader reader = new JSONReader(new StringReader(text));
    Assert.assertEquals(JSONToken.LBRACE, reader.peek());
    reader.startObject();
    Assert.assertEquals(JSONToken.LITERAL_STRING, reader.peek());
    Assert.assertEquals("id", reader.readString());
    Assert.assertEquals(JSONToken.COLON, reader.peek());
    Assert.assertEquals(Integer.valueOf(1001), reader.readInteger());
    reader.endObject();

    reader.close();
}
项目:GitHub    文件:JSONScannerTest_ISO8601.java   
public void test_3() throws Exception {
    JSONScanner lexer = new JSONScanner("2000-2");
    lexer.config(Feature.AllowISO8601DateFormat, true);
    lexer.nextToken();
    Assert.assertEquals(JSONToken.LITERAL_INT, lexer.token());
    lexer.nextToken();
    Assert.assertEquals(JSONToken.LITERAL_INT, lexer.token());
    lexer.nextToken();
    Assert.assertEquals(JSONToken.EOF, lexer.token());
}
项目:GitHub    文件:UUIDDeserializerTest.java   
public void test_url() throws Exception {
    UUID id = UUID.randomUUID();
    Assert.assertEquals(id, JSON.parseObject("'" + id.toString() + "'", UUID.class));

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

    DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);
    Assert.assertEquals(null, MiscCodec.instance.deserialze(parser, null, null));
    Assert.assertEquals(JSONToken.LITERAL_STRING, MiscCodec.instance.getFastMatchToken());

}
项目:GitHub    文件:URIDeserializerTest.java   
public void test_null() throws Exception {
    String input = "null";
    DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);

    MiscCodec deser = new MiscCodec();
    Assert.assertEquals(JSONToken.LITERAL_STRING, deser.getFastMatchToken());

    Assert.assertNull(deser.deserialze(parser, null, null));
}
项目: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    文件:DateParseTest9.java   
public void test_date() throws Exception {
    String text = "\"/Date(1242357713797+0800)/\"";
    Date date = JSON.parseObject(text, Date.class);
    Assert.assertEquals(date.getTime(), 1242357713797L);

    Assert.assertEquals(JSONToken.LITERAL_INT, CalendarCodec.instance.getFastMatchToken());
}
项目:GitHub    文件:FloatDeserializerTest.java   
public void test_bigdecimal() throws Exception {
    Assert.assertEquals(0, JSON.parseObject("0", Float.class).intValue());
    Assert.assertEquals(0, JSON.parseObject("0.0", Float.class).intValue());
    Assert.assertEquals(0, JSON.parseObject("'0'", Float.class).intValue());

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

    DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE);
    Assert.assertEquals(null, FloatCodec.instance.deserialze(parser, null, null));
    Assert.assertEquals(JSONToken.LITERAL_INT, FloatCodec.instance.getFastMatchToken());
}
项目:GitHub    文件:JSONScannerTest_ident.java   
public void test_Date() throws Exception {
    String text = "Date";
    JSONScanner lexer = new JSONScanner(text);
    lexer.scanIdent();
    Assert.assertEquals(JSONToken.IDENTIFIER, lexer.token());
    Assert.assertEquals(text, lexer.stringVal());
}
项目:GitHub    文件:JSONScannerTest_scanSymbol.java   
public void test_1() throws Exception {
    JSONScanner lexer = new JSONScanner("\"value\":\"aa\"},");
    long hashCode = lexer.scanFieldSymbol("\"value\":".toCharArray());
    Assert.assertEquals(fnv_hash("aa"), hashCode);
    Assert.assertEquals(JSONScanner.END, lexer.matchStat());
    Assert.assertEquals(JSONToken.COMMA, lexer.token());
}
项目:GitHub    文件:JSONScannerTest_scanSymbol.java   
public void test_2() throws Exception {
    JSONScanner lexer = new JSONScanner("\"value\":\"aa\"}]");
    long hashCode = lexer.scanFieldSymbol("\"value\":".toCharArray());
    Assert.assertEquals(fnv_hash("aa"), hashCode);
    Assert.assertEquals(JSONScanner.END, lexer.matchStat());
    Assert.assertEquals(JSONToken.RBRACKET, lexer.token());
}
项目:uavstack    文件:IntegerFieldDeserializer.java   
@Override
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) {
    Integer value;

    final JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.LITERAL_INT) {
        int val = lexer.intValue();
        lexer.nextToken(JSONToken.COMMA);
        if (object == null) {
            fieldValues.put(fieldInfo.getName(), val);
        } else {
            setValue(object, val);
        }
        return;
    } else if (lexer.token() == JSONToken.NULL) {
        value = null;
        lexer.nextToken(JSONToken.COMMA);
    } else {
        Object obj = parser.parse();

        value = TypeUtils.castToInt(obj);
    }

    if (value == null && getFieldClass() == int.class) {
        // skip
        return;
    }

    if (object == null) {
        fieldValues.put(fieldInfo.getName(), value);
    } else {
        setValue(object, value);
    }
}
项目:GitHub    文件:LexerTest.java   
public void test_empty() throws Exception {
    JSONScanner lexer = new JSONScanner("".toCharArray(), 0);
    lexer.nextToken();
    Assert.assertEquals(JSONToken.EOF, lexer.token());
}
项目:GitHub    文件:PropertyProcessableDeserializer.java   
public int getFastMatchToken() {
    return JSONToken.LBRACE;
}
项目:uavstack    文件:CharArrayDeserializer.java   
public int getFastMatchToken() {
    return JSONToken.LITERAL_STRING;
}
项目:GitHub    文件:JavaObjectDeserializer.java   
public int getFastMatchToken() {
    return JSONToken.LBRACE;
}
项目:uavstack    文件:BigDecimalCodec.java   
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
项目:uavstack    文件:ReferenceCodec.java   
public int getFastMatchToken() {
    return JSONToken.LBRACE;
}
项目:GitHub    文件:OptionalCodec.java   
public int getFastMatchToken() {
    return JSONToken.LBRACE;
}
项目:uavstack    文件:TimestampDeserializer.java   
public int getFastMatchToken() {
    return JSONToken.LITERAL_INT;
}
项目:GitHub    文件:JSONScannerTest__nextToken.java   
public void test_next_13() throws Exception {
    String text = "\n\r\t\f\b :\n\r\t\f\b ";
    JSONScanner lexer = new JSONScanner(text);
    lexer.nextTokenWithColon(JSONToken.LBRACKET);
    Assert.assertEquals(JSONToken.EOF, lexer.token());
}
项目:GitHub    文件:ArrayListTypeFieldDeserializer.java   
public int getFastMatchToken() {
    return JSONToken.LBRACKET;
}