Search in sources :

Example 6 with JSONArray

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

the class JSONArrayTest method test_getArray.

public void test_getArray() throws Exception {
    JSONArray array = new JSONArray();
    array.add(new ArrayList());
    Assert.assertEquals(0, array.getJSONArray(0).size());
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList)

Example 7 with JSONArray

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

the class JSONArrayTest method test_getObject_map.

public void test_getObject_map() throws Exception {
    JSONArray array = new JSONArray();
    array.add(new HashMap());
    Assert.assertEquals(0, array.getJSONObject(0).size());
}
Also used : HashMap(java.util.HashMap) JSONArray(com.alibaba.fastjson.JSONArray)

Example 8 with JSONArray

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

the class JSONArrayTest method test_getObject.

public void test_getObject() throws Exception {
    JSONArray array = new JSONArray();
    array.add(new JSONObject());
    Assert.assertEquals(0, array.getJSONObject(0).size());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 9 with JSONArray

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

the class JSONArrayTest method test_getJavaBean.

public void test_getJavaBean() throws Exception {
    JSONArray array = JSON.parseArray("[{id:123, name:'aaa'}]");
    Assert.assertEquals(1, array.size());
    Assert.assertEquals(123, array.getObject(0, User.class).getId());
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 10 with JSONArray

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

the class JSONArrayTest method test_2.

public void test_2() throws Exception {
    JSONArray array = new JSONArray();
    array.add(123);
    array.add("222");
    array.add(3);
    array.add(true);
    array.add("true");
    array.add(null);
    Assert.assertEquals(123, array.getByte(0).byteValue());
    Assert.assertEquals(123, array.getByteValue(0));
    Assert.assertEquals(123, array.getShort(0).shortValue());
    Assert.assertEquals(123, array.getShortValue(0));
    Assert.assertTrue(123F == array.getFloat(0).floatValue());
    Assert.assertTrue(123F == array.getFloatValue(0));
    Assert.assertTrue(123D == array.getDouble(0).doubleValue());
    Assert.assertTrue(123D == array.getDoubleValue(0));
    Assert.assertEquals(123, array.getIntValue(0));
    Assert.assertEquals(123, array.getLongValue(0));
    Assert.assertEquals(new BigDecimal("123"), array.getBigDecimal(0));
    Assert.assertEquals(222, array.getIntValue(1));
    Assert.assertEquals(new Integer(222), array.getInteger(1));
    Assert.assertEquals(new Long(222), array.getLong(1));
    Assert.assertEquals(new BigDecimal("222"), array.getBigDecimal(1));
    Assert.assertEquals(true, array.getBooleanValue(4));
    Assert.assertEquals(Boolean.TRUE, array.getBoolean(4));
    Assert.assertEquals(0, array.getIntValue(5));
    Assert.assertEquals(0, array.getLongValue(5));
    Assert.assertEquals(null, array.getInteger(5));
    Assert.assertEquals(null, array.getLong(5));
    Assert.assertEquals(null, array.getBigDecimal(5));
    Assert.assertEquals(null, array.getBoolean(5));
    Assert.assertEquals(false, array.getBooleanValue(5));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONArray(com.alibaba.fastjson.JSONArray) BigDecimal(java.math.BigDecimal)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)93 JSONObject (com.alibaba.fastjson.JSONObject)37 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)8 List (java.util.List)7 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 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)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 Type (java.lang.reflect.Type)2