Search in sources :

Example 31 with JSONArray

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

the class JSONReaderScannerTest_decimal 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(',');
        }
        buf.append(i + ".0");
    }
    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) {
        BigDecimal value = new BigDecimal(i + ".0");
        Assert.assertEquals(value, array.get(i));
    }
}
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) BigDecimal(java.math.BigDecimal)

Example 32 with JSONArray

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

the class JSONReaderScannerTest_int 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(',');
        }
        buf.append(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(i, ((Integer) array.get(i)).intValue());
    }
}
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 33 with JSONArray

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

the class TypeUtilsTest3 method test_ex.

public void test_ex() throws Exception {
    RuntimeException ex = new RuntimeException();
    JSONObject object = (JSONObject) JSON.toJSON(ex);
    JSONArray array = object.getJSONArray("stackTrace");
    array.getJSONObject(0).put("lineNumber", null);
    JSON.parseObject(object.toJSONString(), Exception.class);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 34 with JSONArray

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

the class TypeUtilsToJSONTest method test_1.

public void test_1() throws Exception {
    List list = new ArrayList();
    JSONArray json = (JSONArray) JSON.toJSON(list);
    Assert.assertEquals(list.size(), json.size());
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) List(java.util.List) ArrayList(java.util.ArrayList)

Example 35 with JSONArray

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

the class JSONPath_array_remove_0 method test_remove.

public void test_remove() throws Exception {
    JSONObject jsonObject = new JSONObject();
    JSONArray array = new JSONArray();
    for (int i = 0; i < 10; ++i) {
        JSONObject item = new JSONObject();
        item.put("age", i);
        array.add(item);
    }
    jsonObject.put("aaa", array);
    //解析出错
    JSONPath.remove(jsonObject, "$.aaa[0:1].age");
    //解析出错
    JSONPath.remove(jsonObject, "$.aaa[0,1].age");
    //解析正确
    JSONPath.remove(jsonObject, "$.aaa[0].age");
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) 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