Search in sources :

Example 61 with JSONArray

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

the class JSONObjectTest2 method test_0.

public void test_0() throws Exception {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    JSONObject obj = new JSONObject(map);
    Assert.assertEquals(obj.size(), map.size());
    map.put("a", 1);
    Assert.assertEquals(obj.size(), map.size());
    Assert.assertEquals(obj.get("a"), map.get("a"));
    map.put("b", new int[] { 1 });
    JSONArray array = obj.getJSONArray("b");
    Assert.assertEquals(array.size(), 1);
    map.put("c", new JSONArray());
    JSONArray array2 = obj.getJSONArray("b");
    Assert.assertEquals(array2.size(), 1);
    Assert.assertEquals(obj.getByteValue("d"), 0);
    Assert.assertEquals(obj.getShortValue("d"), 0);
    Assert.assertTrue(obj.getFloatValue("d") == 0F);
    Assert.assertTrue(obj.getDoubleValue("d") == 0D);
    Assert.assertEquals(obj.getBigInteger("d"), null);
    Assert.assertEquals(obj.getSqlDate("d"), null);
    Assert.assertEquals(obj.getTimestamp("d"), null);
    JSONObject obj2 = (JSONObject) obj.clone();
    Assert.assertEquals(obj.size(), obj2.size());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) LinkedHashMap(java.util.LinkedHashMap)

Example 62 with JSONArray

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

the class JSONArrayTest2 method test_0.

public void test_0() throws Exception {
    long time = System.currentTimeMillis();
    JSONArray array = new JSONArray();
    array.add(null);
    array.add(1);
    array.add(time);
    Assert.assertEquals(0, array.getByteValue(0));
    Assert.assertEquals(0, array.getShortValue(0));
    Assert.assertTrue(0F == array.getFloatValue(0));
    Assert.assertTrue(0D == array.getDoubleValue(0));
    Assert.assertEquals(new BigInteger("1"), array.getBigInteger(1));
    Assert.assertEquals("1", array.getString(1));
    Assert.assertEquals(new java.util.Date(time), array.getDate(2));
    Assert.assertEquals(new java.sql.Date(time), array.getSqlDate(2));
    Assert.assertEquals(new java.sql.Timestamp(time), array.getTimestamp(2));
    JSONArray array2 = (JSONArray) array.clone();
    Assert.assertEquals(0, array2.getByteValue(0));
    Assert.assertEquals(0, array2.getShortValue(0));
    Assert.assertTrue(0F == array2.getFloatValue(0));
    Assert.assertTrue(0D == array2.getDoubleValue(0));
    Assert.assertEquals(new BigInteger("1"), array2.getBigInteger(1));
    Assert.assertEquals("1", array2.getString(1));
    Assert.assertEquals(new java.util.Date(time), array2.getDate(2));
    Assert.assertEquals(new java.sql.Date(time), array2.getSqlDate(2));
    Assert.assertEquals(new java.sql.Timestamp(time), array2.getTimestamp(2));
    Assert.assertEquals(array2.size(), array2.size());
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) BigInteger(java.math.BigInteger)

Example 63 with JSONArray

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

the class JSONFromObjectTest method test_3.

public void test_3() throws Exception {
    List users = new ArrayList();
    HashMap user = new HashMap();
    user.put("id", 3);
    user.put("name", "周访");
    users.add(user);
    JSONArray array = (JSONArray) JSON.toJSON(users);
    JSONObject json = array.getJSONObject(0);
    Assert.assertEquals(new Long(3), json.getLong("id"));
    Assert.assertEquals("周访", json.getString("name"));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) List(java.util.List) ArrayList(java.util.ArrayList)

Example 64 with JSONArray

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

the class JSONArrayTest method test_getArray_1.

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

Example 65 with JSONArray

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

the class JSONArrayTest method test_toString.

public void test_toString() throws Exception {
    StringWriter out = new StringWriter();
    new JSONArray().writeJSONString(out);
    Assert.assertEquals("[]", out.toString());
    Assert.assertEquals("[]", new JSONArray().toString());
}
Also used : StringWriter(java.io.StringWriter) 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