Search in sources :

Example 66 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class JSONArrayTest method test_constructor.

public void test_constructor() throws Exception {
    List<Object> list = new ArrayList();
    JSONArray array = new JSONArray(list);
    array.add(3);
    Assert.assertEquals(1, list.size());
    Assert.assertEquals(3, list.get(0));
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject)

Example 67 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class JSONArrayTest method test_getObject_null.

public void test_getObject_null() throws Exception {
    JSONArray array = new JSONArray();
    array.add(null);
    Assert.assertTrue(array.getJSONObject(0) == null);
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 68 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class LexerTest method test_number.

public void test_number() throws Exception {
    String text = "[0,1,-1,2E3,2E+3,2E-3,2e3,2e+3,2e-3]";
    JSONArray array = JSON.parseArray(text);
    Assert.assertEquals(0, array.get(0));
    Assert.assertEquals(1, array.get(1));
    Assert.assertEquals(-1, array.get(2));
    Assert.assertEquals(new BigDecimal("2E3"), array.get(3));
    Assert.assertEquals(new BigDecimal("2E3"), array.get(4));
    Assert.assertEquals(new BigDecimal("2E-3"), array.get(5));
    Assert.assertEquals(new BigDecimal("2E3"), array.get(6));
    Assert.assertEquals(new BigDecimal("2E3"), array.get(7));
    Assert.assertEquals(new BigDecimal("2E-3"), array.get(8));
    for (long i = Long.MIN_VALUE; i <= Long.MIN_VALUE + 1000 * 10; ++i) {
        Assert.assertEquals(i, JSON.parse(Long.toString(i)));
    }
    for (long i = Long.MAX_VALUE - 1000 * 10; i <= Long.MAX_VALUE && i > 0; ++i) {
        Assert.assertEquals(i, JSON.parse(Long.toString(i)));
    }
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) BigDecimal(java.math.BigDecimal)

Example 69 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class Bug_for_issue_434 method test_for_issue.

public void test_for_issue() throws Exception {
    String json = "{value:[\"null\"]}";
    JSONObject parse = JSONObject.parseObject(json);
    JSONArray jsonArray = parse.getJSONArray("value");
    Assert.assertEquals(1, jsonArray.size());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 70 with JSONArray

use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.

the class Bug_for_issue_430 method test_for_issue_1.

public void test_for_issue_1() throws Exception {
    String text = "[{\"@type\": \"com.alibaba.json.bvt.bug.Bug_for_issue_430$FooModel\", \"fooCollection\": null}, {\"@type\": \"com.alibaba.json.bvt.bug.Bug_for_issue_430$FooModel\", \"fooCollection\": null}]";
    JSONArray array = (JSONArray) JSON.parse(text);
    Assert.assertEquals(FooModel.class, array.get(0).getClass());
    Assert.assertEquals(FooModel.class, array.get(1).getClass());
    Assert.assertNull(((FooModel) array.get(0)).fooCollection);
    Assert.assertNull(((FooModel) array.get(1)).fooCollection);
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)95 JSONObject (com.alibaba.fastjson.JSONObject)39 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 StringReader (java.io.StringReader)6 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)5 Reader (java.io.Reader)5 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)3 Map (java.util.Map)3 Setting (org.aisen.android.common.setting.Setting)3 JSONReader (com.alibaba.fastjson.JSONReader)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2