Search in sources :

Example 71 with JSONArray

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

the class Issue771 method test_for_issue.

public void test_for_issue() throws Exception {
    SerializeWriter writer = new SerializeWriter(null, JSON.DEFAULT_GENERATE_FEATURE, new SerializerFeature[0]);
    int defaultBufferSize = writer.getBufferLength();
    String encoded = JSON.toJSONString(new FooBar(defaultBufferSize));
    JSONObject decoded = (JSONObject) JSON.parse(encoded);
    JSONArray dataToEncode = decoded.getJSONArray("dataToEncode");
    Assert.assertEquals(5, dataToEncode.size());
    writer.close();
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 72 with JSONArray

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

the class Issue943 method test_for_issue.

public void test_for_issue() throws Exception {
    String text = "{\n" + "\t\"symbols\":[\n" + "\t    {\"id\":1,\"type\":\"SCATTER\"},\n" + "\t    {\"id\":2,\"type\":\"BONUS\"}\n" + "\t]\n" + "}";
    JSONObject root = JSON.parseObject(text);
    JSONArray symbols = root.getJSONArray("symbols");
    assertNotNull(symbols);
    assertEquals(2, symbols.size());
    assertEquals(1, symbols.getJSONObject(0).get("id"));
    assertEquals("SCATTER", symbols.getJSONObject(0).get("type"));
    assertEquals(2, symbols.getJSONObject(1).get("id"));
    assertEquals("BONUS", symbols.getJSONObject(1).get("type"));
    SlotConfig slotConfig = JSON.parseObject(text, SlotConfig.class);
    assertNotNull(slotConfig);
    assertEquals(2, slotConfig.symbols.size());
    assertEquals(1, slotConfig.symbols.get(0).getId());
    assertEquals(SymbolType.SCATTER, slotConfig.symbols.get(0).getType());
    assertEquals(2, slotConfig.symbols.get(1).getId());
    assertEquals(SymbolType.BONUS, slotConfig.symbols.get(1).getType());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 73 with JSONArray

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

the class Issue955 method test_checkArray.

public void test_checkArray() throws Exception {
    Art origin = makeOrigin();
    JSONObject object = (JSONObject) JSON.toJSON(origin);
    JSONArray jsonArray = new JSONArray();
    jsonArray.add(object);
    Art other = JSON.parseObject(jsonArray.getString(0), Art.class);
    // test passed
    assertSame(origin, other);
    // return = null;
    other = jsonArray.getObject(0, Art.class);
    // test failed
    assertSame(origin, other);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 74 with JSONArray

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

the class Issue780 method test_for_issue_array.

public void test_for_issue_array() throws Exception {
    JSONObject json = new JSONObject();
    json.put("robj", "[123]");
    JSONArray robj = json.getJSONArray("robj");
    assertEquals(123, robj.get(0));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray)

Example 75 with JSONArray

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

the class JSONTest method testParseArray.

@Test
public void testParseArray() throws Exception {
    List list = new ArrayList();
    OuterEntry entry = new OuterEntry();
    list.add(entry);
    entry.setId(1000L);
    entry.setUrl("http://www.springframework.org/schema/aop");
    String jsonString = JSONObject.toJSONString(entry);
    String arrayString = JSONObject.toJSONString(list);
    System.out.println(jsonString);
    System.out.println(arrayString);
    list = JSONArray.parseArray(arrayString, OuterEntry.class);
    // 这一步出错
    JSONArray array = JSONArray.parseArray(arrayString);
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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