Search in sources :

Example 26 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 27 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)

Example 28 with JSONArray

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

the class StringTest_00 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 29 with JSONArray

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

the class JSONPath_set_test2 method test_jsonpath.

public void test_jsonpath() throws Exception {
    JSONObject rootObject = JSON.parseObject("{\"array\":[{},{},{},{}]}");
    JSONPath.set(rootObject, "$.array[0:].key", "123");
    JSONArray array = rootObject.getJSONArray("array");
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertEquals("123", array.getJSONObject(i).get("key"));
    }
    System.out.println(rootObject);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 30 with JSONArray

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

the class RefTest5 method test_parse.

public void test_parse() throws Exception {
    Object[] array2 = JSON.parseObject("[[{\"$ref\":\"..\"}]]", Object[].class);
    JSONArray item = (JSONArray) array2[0];
    Assert.assertSame(item, item.get(0));
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)93 JSONObject (com.alibaba.fastjson.JSONObject)37 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)8 List (java.util.List)7 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 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)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 Type (java.lang.reflect.Type)2