Package com.alibaba.fastjson

Examples of com.alibaba.fastjson.JSONWriter


public class JSONWriterTest extends TestCase {

    public void test_0() throws Exception {
        StringWriter out = new StringWriter();

        JSONWriter writer = new JSONWriter(out);
        writer.writeStartObject();
        writer.writeEndObject();
        writer.flush();

        Assert.assertEquals("{}", out.toString());
    }
View Full Code Here


    }

    public void test_1() throws Exception {
        StringWriter out = new StringWriter();

        JSONWriter writer = new JSONWriter(out);
        writer.writeStartObject();
        writer.writeKey("id");
        writer.writeValue(33);
        writer.writeEndObject();
        writer.flush();

        Assert.assertEquals("{\"id\":33}", out.toString());
    }
View Full Code Here

    }

    public void test_2() 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.writeEndObject();
        writer.flush();

        Assert.assertEquals("{\"id\":33,\"name\":\"jobs\"}", out.toString());
    }
View Full Code Here

    }

    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());
    }
View Full Code Here

    }

    public void test_4() throws Exception {
        StringWriter out = new StringWriter();

        JSONWriter writer = new JSONWriter(out);

        writer.writeStartArray();

        writer.writeStartObject();
        writer.writeEndObject();

        writer.writeStartObject();
        writer.writeEndObject();

        writer.writeStartArray();
        writer.writeEndArray();
        {
            writer.writeStartArray();

            writer.writeStartArray();
            writer.writeEndArray();

            writer.writeStartArray();
            writer.writeEndArray();

            writer.writeEndArray();
           
            writer.writeValue(1);
        }

        writer.writeEndArray();

        writer.flush();

        Assert.assertEquals("[{},{},[],[[],[]],1]", out.toString());
    }
View Full Code Here

       
        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);
View Full Code Here

        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;
View Full Code Here

        List<String> list = new ArrayList<String>(1000 * 1);
        for (int i = 0; i < 100 * 1; ++i) {
            list.add(Integer.toString(i));
        }
        File file = File.createTempFile("fastjson-stream-large", "json");
        JSONWriter writer = new JSONWriter(new FileWriter(file));
        writer.startArray();
        writer.writeObject(list);
        writer.endArray();
        writer.close();

        System.out.println(FileUtils.readFileToString(file));

        JSONReader reader = new JSONReader(new FileReader(file));
        reader.startArray();
View Full Code Here

public class JSONWriterTest_1 extends TestCase {

    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());
    }
View Full Code Here

TOP

Related Classes of com.alibaba.fastjson.JSONWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.