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());
}
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());
}
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"));
}
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());
}
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());
}
Aggregations