Search in sources :

Example 61 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 62 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 63 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 64 with DefaultJSONParser

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

the class DefaultExtJSONParserTest_1 method f_test_2.

public void f_test_2() throws Exception {
    TestEntity a = new TestEntity();
    a.setF1(true);
    a.setF2(Boolean.TRUE);
    a.setF3((byte) 123);
    a.setF4((byte) 123);
    a.setF5((short) 123);
    a.setF6((short) 123);
    a.setF7((int) 123);
    a.setF8((int) 123);
    a.setF9((long) 123);
    a.setF10((long) 123);
    a.setF11(new BigInteger("123"));
    a.setF12(new BigDecimal("123"));
    a.setF13("abc");
    a.setF14(null);
    a.setF15(12.34F);
    a.setF16(12.35F);
    a.setF17(12.345D);
    a.setF18(12.345D);
    String text = JSON.toJSONString(a);
    System.out.println(text);
    TestEntity b = new TestEntity();
    {
        DefaultJSONParser parser = new DefaultJSONParser(text);
        parser.parseObject(b);
    }
    Assert.assertEquals("f1", a.isF1(), b.isF1());
    Assert.assertEquals("f2", a.getF2(), b.getF2());
    Assert.assertEquals("f3", a.getF3(), b.getF3());
    Assert.assertEquals("f4", a.getF4(), b.getF4());
    Assert.assertEquals("f5", a.getF5(), b.getF5());
    Assert.assertEquals("f6", a.getF6(), b.getF6());
    Assert.assertEquals("f7", a.getF7(), b.getF7());
    Assert.assertEquals("f8", a.getF8(), b.getF8());
    Assert.assertEquals("f9", a.getF9(), b.getF9());
    Assert.assertEquals(a.getF10(), b.getF10());
    Assert.assertEquals(a.getF11(), b.getF11());
    Assert.assertEquals(a.getF12(), b.getF12());
    Assert.assertEquals(a.getF13(), b.getF13());
    Assert.assertEquals(a.getF14(), b.getF14());
    Assert.assertEquals(a.getF15(), b.getF15());
    Assert.assertEquals(a.getF16(), b.getF16());
    Assert.assertEquals(a.getF17(), b.getF17());
    Assert.assertEquals(a.getF18(), b.getF18());
}
Also used : TestEntity(com.alibaba.json.test.entity.TestEntity) BigInteger(java.math.BigInteger) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) BigDecimal(java.math.BigDecimal)

Example 65 with DefaultJSONParser

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

the class DefaultExtJSONParserTest_3 method test_4.

public void test_4() throws Exception {
    DefaultJSONParser parser = new DefaultJSONParser("{v5:'3'}");
    parser.config(Feature.AllowUnQuotedFieldNames, true);
    parser.config(Feature.AllowSingleQuotes, true);
    A a = parser.parseObject(A.class);
    Assert.assertEquals(3L, a.getV5().longValue());
}
Also used : DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Aggregations

DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)258 JSONObject (com.alibaba.fastjson.JSONObject)42 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)35 JSONException (com.alibaba.fastjson.JSONException)29 ArrayList (java.util.ArrayList)21 Reader (java.io.Reader)14 StringReader (java.io.StringReader)14 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)11 BigDecimal (java.math.BigDecimal)10 Date (java.util.Date)10 Feature (com.alibaba.fastjson.parser.Feature)9 List (java.util.List)9 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)8 Type (java.lang.reflect.Type)7 JSONArray (com.alibaba.fastjson.JSONArray)6 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)5 ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)5 TypeUtils.castToString (com.alibaba.fastjson.util.TypeUtils.castToString)5 LinkedHashMap (java.util.LinkedHashMap)5 MiscCodec (com.alibaba.fastjson.serializer.MiscCodec)4