Search in sources :

Example 16 with JSONReader

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

the class Issue143 method test_for_issue.

public void test_for_issue() throws Exception {
    String text = "{\"rec\":[{},{}]}";
    JsonStroe store = new JsonStroe();
    JSONReader reader = new JSONReader(new StringReader(text));
    reader.startObject();
    String key = reader.readString();
    Assert.assertEquals("rec", key);
    reader.startArray();
    List<KeyValue> list = new ArrayList<KeyValue>();
    while (reader.hasNext()) {
        KeyValue keyValue = reader.readObject(KeyValue.class);
        list.add(keyValue);
    }
    store.setRec(list);
    reader.endArray();
    reader.endObject();
    reader.close();
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) JSONReader(com.alibaba.fastjson.JSONReader)

Example 17 with JSONReader

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

the class Issue169 method test_for_issue.

public void test_for_issue() throws Exception {
    StringWriter strWriter = new StringWriter();
    SectionRequest req = new SectionRequest();
    // 父类中的属性
    req.setScreenHeight(100);
    // 父类中的属性
    req.setScreenWidth(12);
    req.setTag("11");
    JSONWriter writer = new JSONWriter(strWriter);
    writer.startArray();
    writer.writeObject(req);
    writer.endArray();
    writer.close();
    String text = strWriter.toString();
    StringReader strReader = new StringReader(text);
    JSONReader reader = new JSONReader(strReader);
    reader.startArray();
    ;
    while (reader.hasNext()) {
        SectionRequest vo = reader.readObject(SectionRequest.class);
        System.out.println("tag:" + vo.getTag() + "screenHeight:" + vo.getScreenHeight() + "ScreenWidth:" + vo.getScreenWidth());
        Assert.assertEquals(100, vo.getScreenHeight());
        Assert.assertEquals(12, vo.getScreenWidth());
        Assert.assertEquals("11", vo.getTag());
    }
    reader.endArray();
    reader.close();
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JSONReader(com.alibaba.fastjson.JSONReader)

Example 18 with JSONReader

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

the class Bug_for_issue_448 method test_for_issue.

// skip
public void test_for_issue() throws Exception {
    final int value_size = 1024 * 16;
    List<Model> list = new ArrayList<Model>();
    for (int i = 0; i < 10; ++i) {
        Model model = new Model();
        char[] buf = new char[value_size];
        for (int j = 0; j < buf.length; ++j) {
            buf[j] = (char) ('a' + j);
        }
        model.value = new String(buf);
        list.add(model);
    }
    String text = JSON.toJSONString(list);
    JSONReader reader = new JSONReader(new StringReader(text));
    reader.startArray();
    while (reader.hasNext()) {
        Model model = reader.readObject(Model.class);
        String value = model.value;
        Assert.assertEquals(value_size, value.length());
        for (int i = 0; i < value.length(); ++i) {
            char ch = value.charAt(i);
            Assert.assertEquals("error : index_" + i, (char) ('a' + i), ch);
        }
    }
    reader.endArray();
    reader.close();
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JSONReader(com.alibaba.fastjson.JSONReader)

Example 19 with JSONReader

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

the class DateTest_tz method test_codec.

//    protected void setUp() throws Exception {
//        JSON.defaultTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
//        JSON.defaultLocale = Locale.CHINA;
//    }
public void test_codec() throws Exception {
    JSONReader reader = new JSONReader(new StringReader("{\"value\":\"2016-04-29\"}"));
    reader.setLocale(Locale.CHINA);
    reader.setTimzeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    Model model = reader.readObject(Model.class);
    Assert.assertNotNull(model.value);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    Date date = format.parse("2016-04-29");
    Assert.assertEquals(date.getTime(), model.value.getTime());
    Assert.assertEquals(TimeZone.getTimeZone("Asia/Shanghai"), reader.getTimzeZone());
    Assert.assertEquals(Locale.CHINA, reader.getLocal());
    reader.close();
}
Also used : StringReader(java.io.StringReader) JSONReader(com.alibaba.fastjson.JSONReader) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 20 with JSONReader

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

the class Issue72 method test_for_issue.

public void test_for_issue() throws Exception {
    InputStream is = Issue72.class.getClassLoader().getResourceAsStream("issue72.json");
    JSONReader reader = null;
    try {
        byte[] rowBatchBytes = null;
        byte[] fileBatchBytes = null;
        reader = new JSONReader(new InputStreamReader(is));
        reader.startArray();
        while (reader.hasNext()) {
            if (rowBatchBytes == null) {
                rowBatchBytes = reader.readObject(byte[].class);
            } else if (fileBatchBytes == null) {
                fileBatchBytes = reader.readObject(byte[].class);
            } else {
                throw new Exception("archive data json parse failed!");
            }
        }
        reader.endArray();
    } finally {
        IOUtils.close(reader);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JSONReader(com.alibaba.fastjson.JSONReader)

Aggregations

JSONReader (com.alibaba.fastjson.JSONReader)215 StringReader (java.io.StringReader)195 JSONException (com.alibaba.fastjson.JSONException)83 Map (java.util.Map)30 JSONScanner (com.alibaba.fastjson.parser.JSONScanner)11 IOException (java.io.IOException)5 JSONObject (com.alibaba.fastjson.JSONObject)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JSONWriter (com.alibaba.fastjson.JSONWriter)3 File (java.io.File)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 JSONArray (com.alibaba.fastjson.JSONArray)2 TypeReference (com.alibaba.fastjson.TypeReference)2 VO (com.alibaba.json.bvt.writeAsArray.WriteAsArray_int_public.VO)2 StringWriter (java.io.StringWriter)2 Field (java.lang.reflect.Field)2 Random (java.util.Random)2 Model (com.alibaba.json.bvt.LongFieldTest_3_stream.Model)1