Search in sources :

Example 6 with JSONWriter

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

the class JSONWriterTest method test_3.

public void test_3() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.writeStartObject();
    writer.writeKey("id");
    writer.writeValue(33);
    writer.writeKey("name");
    writer.writeValue("jobs");
    writer.writeKey("children");
    writer.writeStartArray();
    writer.writeStartObject();
    writer.writeEndObject();
    writer.writeStartObject();
    writer.writeEndObject();
    writer.writeEndArray();
    writer.writeEndObject();
    writer.flush();
    Assert.assertEquals("{\"id\":33,\"name\":\"jobs\",\"children\":[{},{}]}", out.toString());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 7 with JSONWriter

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

the class JSONWriterTest_1 method test_writer.

public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.startObject();
    writer.writeObject("a");
    writer.writeObject("1");
    writer.writeObject("b");
    writer.writeObject("2");
    writer.writeObject("c");
    writer.writeObject("3");
    writer.endObject();
    writer.close();
    Assert.assertEquals("{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}", out.toString());
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter)

Example 8 with JSONWriter

use of com.alibaba.fastjson.JSONWriter in project otter by alibaba.

the class JsonUtilsTest method test_bytes.

@Test
public void test_bytes() {
    Map data = new HashMap<String, byte[]>();
    byte[] one = new byte[] { 1, 2, 3 };
    byte[] two = new byte[] { 4, 5, 6 };
    data.put("one", one);
    data.put("two", two);
    StringWriter jsonStr = new StringWriter();
    //超大文本写入
    JSONWriter writer = new JSONWriter(jsonStr);
    writer.startArray();
    writer.writeValue(one);
    writer.writeValue(two);
    writer.endArray();
    try {
        writer.close();
    } catch (IOException e) {
    }
    JSONReader reader = new JSONReader(new StringReader(jsonStr.getBuffer().toString()));
    byte[] oneDates = null;
    byte[] twoDates = null;
    reader.startArray();
    while (reader.hasNext()) {
        if (oneDates == null) {
            oneDates = reader.readObject(byte[].class);
        } else if (twoDates == null) {
            twoDates = reader.readObject(byte[].class);
        } else {
            want.fail("not possible");
        }
    }
    reader.endArray();
    reader.close();
    check(oneDates, one);
    check(twoDates, two);
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) StringReader(java.io.StringReader) IOException(java.io.IOException) JSONReader(com.alibaba.fastjson.JSONReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 9 with JSONWriter

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

the class MultiFieldIntTest_writer method test_for_big_writer.

public void test_for_big_writer() throws Exception {
    List<Model> list = new ArrayList<Model>();
    for (int i = 0; i < 1024 * 10; ++i) {
        Model model = new Model();
        model.id = 10000000 + i;
        list.add(model);
    }
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.writeObject(list);
    writer.close();
    String text = out.toString();
    System.out.println(text);
    List<Model> results = JSON.parseObject(text, new TypeReference<List<Model>>() {
    });
    Assert.assertEquals(list.size(), results.size());
    for (int i = 0; i < results.size(); ++i) {
        Assert.assertEquals(list.get(i).id, results.get(i).id);
    }
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 10 with JSONWriter

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

the class MultiFieldIntTest_writer2 method test_for_big_writer.

public void test_for_big_writer() throws Exception {
    List<Model> list = new ArrayList<Model>();
    for (int i = 0; i < 1024 * 10; ++i) {
        Model model = new Model();
        model.i = 0;
        model.j = 1;
        model.k = 2;
        model.v = 3;
        model.l = 4;
        model.m = 5;
        model.n = 6;
        list.add(model);
    }
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.writeObject(list);
    writer.close();
    String text = out.toString();
    System.out.println(text);
    List<Model> results = JSON.parseObject(text, new TypeReference<List<Model>>() {
    });
    Assert.assertEquals(list.size(), results.size());
    for (int i = 0; i < results.size(); ++i) {
        Assert.assertEquals(list.get(i).i, results.get(i).i);
        Assert.assertEquals(list.get(i).j, results.get(i).j);
        Assert.assertEquals(list.get(i).k, results.get(i).k);
        Assert.assertEquals(list.get(i).v, results.get(i).v);
        Assert.assertEquals(list.get(i).l, results.get(i).l);
        Assert.assertEquals(list.get(i).m, results.get(i).m);
        Assert.assertEquals(list.get(i).n, results.get(i).n);
    }
}
Also used : JSONWriter(com.alibaba.fastjson.JSONWriter) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

JSONWriter (com.alibaba.fastjson.JSONWriter)18 StringWriter (java.io.StringWriter)17 JSONReader (com.alibaba.fastjson.JSONReader)3 ArrayList (java.util.ArrayList)3 StringReader (java.io.StringReader)2 List (java.util.List)2 File (java.io.File)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1