Search in sources :

Example 76 with JSONArray

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

the class Alipay1213 method test_for_issue.

public void test_for_issue() throws Exception {
    String text = "{\"resultObj\":{\"assetSize\":{},\"chargeTypeList\":[\"A\"],\"fundManagerMapList\":[{},{}],\"fundRateConvertList\":[{},{},{}],\"fundRateOperate\":{},\"fundRatePurchaseList\":[{\"fixedRate\":{},\"maxAmount\":{},\"minAmount\":{}}],\"fundRateRedeemList\":[{\"fixedRate\":{}}],\"fundRateSubscribeList\":[{\"fixedRate\":{},\"maxAmount\":{},\"minAmount\":{}}],\"fundRatingList\":[{},{}],\"fundRuleConvertList\":[{},{}],\"fundRuleConvertVoList\":[{\"fundRateConvertList\":[{\"$ref\":\"$.resultObj.fundRateConvertList[0]\"},{\"$ref\":\"$.resultObj.fundRateConvertList[1]\"}],\"fundRuleConvert\":{\"$ref\":\"$.resultObj.fundRuleConvertList[0]\"}},{\"fundRateConvertList\":[{\"$ref\":\"$.resultObj.fundRateConvertList[2]\"}],\"fundRuleConvert\":{\"$ref\":\"$.resultObj.fundRuleConvertList[1]\"}}]}}";
    JSONObject root = JSON.parseObject(text);
    JSONObject resultObj = root.getJSONObject("resultObj");
    assertNotNull(resultObj);
    JSONArray fundRuleConvertVoList = resultObj.getJSONArray("fundRuleConvertVoList");
    assertNotNull(fundRuleConvertVoList);
    JSONArray fundRateConvertList = fundRuleConvertVoList.getJSONObject(0).getJSONArray("fundRateConvertList");
    assertNotNull(fundRateConvertList);
    assertNotNull(fundRateConvertList.get(0));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 77 with JSONArray

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

the class JSONReaderScannerTest__map_string method test_scanInt.

public void test_scanInt() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (int i = 0; i < 10; ++i) {
        if (i != 0) {
            buf.append(',');
        }
        // 1000000000000
        //
        buf.append("{\"id\":\"" + i + "\"}");
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    JSONArray array = (JSONArray) parser.parse();
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertEquals(Integer.toString(i), array.getJSONObject(i).get("id"));
    }
}
Also used : StringReader(java.io.StringReader) JSONArray(com.alibaba.fastjson.JSONArray) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 78 with JSONArray

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

the class JSONReaderScannerTest_long method test_scanInt.

public void test_scanInt() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (int i = 0; i < 1024; ++i) {
        if (i != 0) {
            buf.append(',');
        }
        long value = (long) Integer.MAX_VALUE + 1L + (long) i;
        buf.append(value);
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    JSONArray array = (JSONArray) parser.parse();
    for (int i = 0; i < array.size(); ++i) {
        long value = (long) Integer.MAX_VALUE + 1L + (long) i;
        Assert.assertEquals(value, ((Long) array.get(i)).longValue());
    }
}
Also used : StringReader(java.io.StringReader) JSONArray(com.alibaba.fastjson.JSONArray) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 79 with JSONArray

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

the class JSONReaderTest method test_read.

public void test_read() throws Exception {
    String resource = "2.json";
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    JSONReader reader = new JSONReader(new InputStreamReader(is, "UTF-8"));
    reader.startObject();
    Assert.assertEquals("company", reader.readString());
    Assert.assertTrue(reader.readObject() instanceof JSONObject);
    Assert.assertEquals("count", reader.readString());
    Assert.assertEquals(5, reader.readObject());
    Assert.assertEquals("pagecount", reader.readString());
    Assert.assertEquals(0, reader.readObject());
    Assert.assertEquals("pageindex", reader.readString());
    Assert.assertEquals(0, reader.readObject());
    Assert.assertEquals("resultList", reader.readString());
    Assert.assertTrue(reader.readObject() instanceof JSONArray);
    Assert.assertEquals("totalCount", reader.readString());
    Assert.assertEquals(0, reader.readObject());
    reader.endObject();
    reader.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(com.alibaba.fastjson.JSONObject) InputStream(java.io.InputStream) JSONArray(com.alibaba.fastjson.JSONArray) JSONReader(com.alibaba.fastjson.JSONReader)

Example 80 with JSONArray

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

the class StringTest_01 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("\\\"");
        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 + 32, item.length());
    for (int i = 0; i < 16; ++i) {
        Assert.assertTrue(item.charAt(i * 1026) == '\\');
        Assert.assertTrue(item.charAt(i * 1026 + 1) == '\"');
        for (int j = 0; j < 1024; ++j) {
            Assert.assertTrue(item.charAt(i * 1026 + j + 2) == '0');
        }
    }
}
Also used : 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