Search in sources :

Example 81 with JSONArray

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

the class StringTest_02 method test_string.

public void test_string() throws Exception {
    char[] chars = new char[1024];
    Arrays.fill(chars, '0');
    StringBuilder buf = new StringBuilder();
    buf.append("[\"");
    for (int i = 0; i < 16; ++i) {
        buf.append("\\\"");
        buf.append(new String(chars));
    }
    buf.append("\"]");
    String text = buf.toString();
    JSONArray array = (JSONArray) JSON.parse(text);
    Assert.assertEquals(1, array.size());
    String item = (String) array.get(0);
    Assert.assertEquals(16 * 1024 + 16, item.length());
    for (int i = 0; i < 16; ++i) {
        Assert.assertTrue(item.charAt(i * 1025) == '\"');
        for (int j = 0; j < 1024; ++j) {
            Assert.assertTrue(item.charAt(i * 1025 + j + 1) == '0');
        }
    }
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 82 with JSONArray

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

the class JSONReader_array method test_array.

public void test_array() throws Exception {
    JSONReader reader = new JSONReader(new StringReader("[[],[],3,null,{\"name\":\"jobs\"},{\"id\":123},{\"id\":1},{\"id\":2}]"));
    reader.startArray();
    JSONArray first = (JSONArray) reader.readObject();
    JSONArray second = (JSONArray) reader.readObject();
    Assert.assertNotNull(first);
    Assert.assertNotNull(second);
    Assert.assertEquals(new Integer(3), reader.readInteger());
    Assert.assertNull(reader.readString());
    {
        Map<String, Object> map = new HashMap<String, Object>();
        reader.readObject(map);
        Assert.assertEquals("jobs", map.get("name"));
    }
    {
        VO vo = new VO();
        reader.readObject(vo);
        Assert.assertEquals(123, vo.getId());
    }
    while (reader.hasNext()) {
        VO vo = reader.readObject(VO.class);
        Assert.assertNotNull(vo);
    }
    reader.endArray();
    reader.close();
}
Also used : StringReader(java.io.StringReader) JSONArray(com.alibaba.fastjson.JSONArray) JSONReader(com.alibaba.fastjson.JSONReader) Map(java.util.Map) HashMap(java.util.HashMap)

Example 83 with JSONArray

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

the class RefTest17 method test_0.

public void test_0() throws Exception {
    Person pA = new Person("a");
    Person pB = new Person("b");
    Family fA = new Family();
    fA.setMembers(new Person[] { pA, pB });
    fA.setMaster(pA);
    Person pC = new Person("c");
    Person pD = new Person("d");
    Family fB = new Family();
    fB.setMembers(new Person[] { pC, pD });
    fB.setMaster(pC);
    Family[] familyArray = new Family[] { fA, fB };
    String text = JSON.toJSONString(familyArray, true);
    System.out.println(text);
    JSONArray array = JSON.parseArray(text);
    Assert.assertSame(array.getJSONObject(0).get("master"), array.getJSONObject(0).getJSONArray("members").get(0));
    Family family = array.getObject(0, Family.class);
    Assert.assertNotNull(family.getMembers()[0]);
    Assert.assertNotNull(family.getMembers()[1]);
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Example 84 with JSONArray

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

the class RefTest18 method test_array.

public void test_array() throws Exception {
    String text = "{\"b\":{},\"a\":[{\"$ref\":\"$.b\"}]}";
    JSONObject obj = JSON.parseObject(text);
    JSONArray array = obj.getJSONArray("a");
    Assert.assertEquals(1, array.size());
    Assert.assertNotNull(array.get(0));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 85 with JSONArray

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

the class Alipay1206 method test_for_alipay.

public void test_for_alipay() throws Exception {
    File file = new File("/Users/wenshao/Downloads/x.txt");
    String text = FileUtils.readFileToString(file);
    JSONObject root = JSON.parseObject(text);
    JSONObject resultObj = root.getJSONObject("resultObj");
    assertNotNull(resultObj);
    JSONArray conditionGroupItemList = resultObj.getJSONArray("conditionGroupItemList");
    assertNotNull(conditionGroupItemList);
    JSONObject conditionGroup = conditionGroupItemList.getJSONObject(0).getJSONObject("conditionGroup");
    assertNotNull(conditionGroup);
    JSONArray recordList = conditionGroup.getJSONArray("recordList");
    assertNotNull(recordList);
    JSONArray conditionItemList = recordList.getJSONObject(0).getJSONArray("conditionItemList");
    assertNotNull(conditionItemList);
    JSONObject condition = conditionItemList.getJSONObject(18).getJSONObject("condition");
    assertNotNull(condition);
    JSONArray conditionConstraint = condition.getJSONArray("conditionConstraint");
    assertNotNull(conditionConstraint);
    JSONObject constraintOptionalRecordMap = conditionConstraint.getJSONObject(0).getJSONObject("constraintOptionalRecordMap");
    assertNotNull(constraintOptionalRecordMap);
    System.out.println(constraintOptionalRecordMap);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) File(java.io.File)

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