Search in sources :

Example 16 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DateParserTest_sql_timestamp method test_date_2.

public void test_date_2() throws Exception {
    int features = JSON.DEFAULT_PARSER_FEATURE;
    DefaultJSONParser parser = new DefaultJSONParser("new Date(1294552193254)", ParserConfig.getGlobalInstance(), features);
    java.sql.Timestamp date = parser.parseObject(java.sql.Timestamp.class);
    Assert.assertEquals(new java.sql.Timestamp(1294552193254L), date);
    parser.close();
}
Also used : DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 17 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DefaultExtJSONParserTest method test_parseArrayWithType.

@SuppressWarnings("rawtypes")
public void test_parseArrayWithType() throws Exception {
    Method method = DefaultExtJSONParserTest.class.getMethod("f", Collection.class, Collection.class, Collection.class, Collection.class, Collection.class, Collection.class, Collection.class);
    Type[] types = method.getGenericParameterTypes();
    {
        String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[0])).get(0) instanceof Map);
    }
    {
        String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[1])).get(0) instanceof User);
    }
    {
        Exception error = null;
        try {
            String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
            DefaultJSONParser parser = new DefaultJSONParser(text);
            parser.parseArrayWithType(types[2]);
            ;
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[3])).get(0) instanceof User);
    }
    {
        Exception error = null;
        try {
            String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
            DefaultJSONParser parser = new DefaultJSONParser(text);
            parser.parseArrayWithType(types[4]);
            ;
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    {
        String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        Assert.assertEquals(true, ((List) parser.parseArrayWithType(types[5])).get(0) instanceof User);
    }
    {
        Exception error = null;
        try {
            String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
            DefaultJSONParser parser = new DefaultJSONParser(text);
            parser.parseArrayWithType(types[6]);
            ;
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) TypeUtils.castToString(com.alibaba.fastjson.util.TypeUtils.castToString) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) Map(java.util.Map)

Example 18 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DefaultExtJSONParserTest method test_not_match.

public void test_not_match() throws Exception {
    String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123, \"kxxx\":33}]";
    DefaultJSONParser parser = new DefaultJSONParser(text);
    Assert.assertEquals(true, (parser.parseArray(User.class).get(0) instanceof User));
}
Also used : TypeUtils.castToString(com.alibaba.fastjson.util.TypeUtils.castToString) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 19 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DefaultExtJSONParserTest method test_parseArrayWithType_error_1.

public void test_parseArrayWithType_error_1() throws Exception {
    Method method = DefaultExtJSONParserTest.class.getMethod("f", Collection.class, Collection.class, Collection.class, Collection.class, Collection.class, Collection.class, Collection.class);
    Type[] types = method.getGenericParameterTypes();
    Exception error = null;
    try {
        String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123}]";
        DefaultJSONParser parser = new DefaultJSONParser(text);
        parser.parseArrayWithType(types[6]);
        ;
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) TypeUtils.castToString(com.alibaba.fastjson.util.TypeUtils.castToString) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 20 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class InitStringFieldAsEmptyTest method test_1.

public void test_1() throws Exception {
    DefaultJSONParser parser = new DefaultJSONParser("{}");
    parser.config(Feature.InitStringFieldAsEmpty, false);
    Model model = parser.parseObject(Model.class);
    Assert.assertNull(null, model.value);
    parser.close();
}
Also used : DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Aggregations

DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)242 JSONObject (com.alibaba.fastjson.JSONObject)35 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)34 JSONException (com.alibaba.fastjson.JSONException)28 ArrayList (java.util.ArrayList)21 Reader (java.io.Reader)14 StringReader (java.io.StringReader)14 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)10 BigDecimal (java.math.BigDecimal)10 Date (java.util.Date)10 List (java.util.List)9 Feature (com.alibaba.fastjson.parser.Feature)6 JSONArray (com.alibaba.fastjson.JSONArray)5 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)5 TypeUtils.castToString (com.alibaba.fastjson.util.TypeUtils.castToString)5 MiscCodec (com.alibaba.fastjson.serializer.MiscCodec)4 LinkedHashMap (java.util.LinkedHashMap)4 TypeReference (com.alibaba.fastjson.TypeReference)3 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)3 NumberDeserializer (com.alibaba.fastjson.parser.deserializer.NumberDeserializer)3