use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.
the class JSONArrayTest method test_1.
public void test_1() throws Exception {
JSONArray array = new JSONArray(3);
Assert.assertEquals(true, array.isEmpty());
array.add(1);
Assert.assertEquals(false, array.isEmpty());
Assert.assertEquals(true, array.contains(1));
Assert.assertEquals(1, array.toArray()[0]);
{
Object[] items = new Object[1];
array.toArray(items);
Assert.assertEquals(1, items[0]);
}
Assert.assertEquals(true, array.containsAll(Collections.singletonList(1)));
Assert.assertEquals(true, array.remove(Integer.valueOf(1)));
Assert.assertEquals(true, array.isEmpty());
array.addAll(Collections.singletonList(1));
Assert.assertEquals(1, array.size());
array.removeAll(Collections.singletonList(1));
Assert.assertEquals(0, array.size());
array.addAll(0, Arrays.asList(1, 2, 3));
Assert.assertEquals(3, array.size());
array.clear();
array.addAll(0, Arrays.asList(1, 2, 3));
Assert.assertEquals(true, array.retainAll(Arrays.asList(1, 2)));
Assert.assertEquals(2, array.size());
Assert.assertEquals(true, array.retainAll(Arrays.asList(2, 4)));
Assert.assertEquals(1, array.size());
array.set(0, 4);
Assert.assertEquals(4, array.toArray()[0]);
array.add(0, 4);
Assert.assertEquals(4, array.toArray()[0]);
array.remove(0);
array.remove(0);
Assert.assertEquals(0, array.size());
array.addAll(Arrays.asList(1, 2, 3, 4, 5, 4, 3));
Assert.assertEquals(2, array.indexOf(3));
Assert.assertEquals(6, array.lastIndexOf(3));
{
AtomicInteger count = new AtomicInteger();
for (ListIterator<Object> iter = array.listIterator(); iter.hasNext(); iter.next()) {
count.incrementAndGet();
}
Assert.assertEquals(7, count.get());
}
{
AtomicInteger count = new AtomicInteger();
for (ListIterator<Object> iter = array.listIterator(2); iter.hasNext(); iter.next()) {
count.incrementAndGet();
}
Assert.assertEquals(5, count.get());
}
{
Assert.assertEquals(2, array.subList(2, 4).size());
}
}
use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.
the class CastTest2 method test_0.
public void test_0() throws Exception {
String text;
{
List<Object> list = new ArrayList<Object>();
list.add(new Header());
Body body = new Body("张三");
body.getItems().put("1", new Item());
list.add(body);
text = JSON.toJSONString(list);
System.out.println(text);
}
JSONArray array = JSON.parseArray(text);
Body body = array.getObject(1, Body.class);
Assert.assertEquals("张三", body.getName());
Assert.assertEquals(1, body.getItems().size());
}
use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.
the class JSONArrayTest3 method test_0.
public void test_0() throws Exception {
JSONArray array = new JSONArray();
array.set(1, "1001");
Assert.assertEquals(2, array.size());
Assert.assertNull(array.get(0));
Assert.assertEquals("1001", array.get(1));
array.clear();
Assert.assertEquals(0, array.size());
array.set(-1, "1001");
Assert.assertEquals(1, array.size());
Assert.assertEquals("1001", array.get(0));
array.fluentAdd("1002").fluentClear();
Assert.assertEquals(0, array.size());
array.fluentAdd("1002").fluentRemove("1002");
Assert.assertEquals(0, array.size());
array.fluentAdd("1002").fluentRemove(0);
Assert.assertEquals(0, array.size());
array.fluentSet(1, "1001");
Assert.assertEquals(2, array.size());
Assert.assertNull(array.get(0));
Assert.assertEquals("1001", array.get(1));
array.fluentRemoveAll(Arrays.asList(null, "1001"));
Assert.assertEquals(0, array.size());
array.fluentAddAll(Arrays.asList("1001", "1002", "1003"));
Assert.assertEquals(3, array.size());
array.retainAll(Arrays.asList("1002", "1004"));
Assert.assertEquals(1, array.size());
Assert.assertEquals("1002", array.get(0));
}
use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.
the class Issue1023 method test_for_issue.
public void test_for_issue() throws Exception {
Date now = new Date();
GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
gregorianCalendar.setTime(now);
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
String jsonString = JSON.toJSONString(calendar);
// success
calendar = JSON.parseObject(jsonString, XMLGregorianCalendar.class);
// debug看到是 Long 类型
Object toJSON1 = JSON.toJSON(calendar);
// 所以这里会报错:
// error: java.lang.ClassCastException: java.lang.Long cannot be cast to com.alibaba.fastjson.JSONObject
//JSONObject jsonObject = (JSONObject) JSON.toJSON(calendar);
// 所以 这里肯定会报错, 因为 jsonObject 不是JSONObject类型
//calendar = jsonObject.toJavaObject(XMLGregorianCalendar.class);
List<XMLGregorianCalendar> calendarList = new ArrayList<XMLGregorianCalendar>();
calendarList.add(calendar);
calendarList.add(calendar);
calendarList.add(calendar);
// debug 看到是 JSONArray 类型
Object toJSON2 = JSON.toJSON(calendarList);
// success: 通过 JSONArray.parseArray 方法可以正确转换
JSONArray jsonArray = (JSONArray) JSON.toJSON(calendarList);
jsonString = jsonArray.toJSONString();
List<XMLGregorianCalendar> calendarList1 = JSONArray.parseArray(jsonString, XMLGregorianCalendar.class);
// 通过 jsonArray.toJavaList 无法转换
// error: com.alibaba.fastjson.JSONException: can not cast to : javax.xml.datatype.XMLGregorianCalendar
List<XMLGregorianCalendar> calendarList2 = jsonArray.toJavaList(XMLGregorianCalendar.class);
assertNotNull(calendarList2);
assertEquals(3, calendarList2.size());
}
use of com.alibaba.fastjson.JSONArray in project fastjson by alibaba.
the class Bug_for_jiangwei1 method test_JSONArray_double.
public void test_JSONArray_double() throws Exception {
JSONArray json = JSON.parseArray("[12.3]");
Assert.assertTrue(12.3D == json.getDoubleValue(0));
}
Aggregations