Package com.alibaba.fastjson

Examples of com.alibaba.fastjson.JSONWriter$Context


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.writeEndArray();

        writer.flush();

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

public class JSONWriterTest_2 extends TestCase {

    public void test_writer() throws Exception {
        StringWriter out = new StringWriter();
        JSONWriter writer = new JSONWriter(out);
        writer.config(SerializerFeature.UseSingleQuotes, true);
        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

public class JSONWriterTest_3 extends TestCase {

    public void test_writer() throws Exception {
        StringWriter out = new StringWriter();
        JSONWriter writer = new JSONWriter(out);
        writer.config(SerializerFeature.UseSingleQuotes, true);
        writer.startObject();
       
        writer.startObject();
        writer.endObject();

        writer.startObject();
        writer.endObject();
       
        writer.endObject();
        writer.close();

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

public class JSONWriterTest_4 extends TestCase {

    public void test_writer() throws Exception {
        StringWriter out = new StringWriter();
        JSONWriter writer = new JSONWriter(out);
        writer.config(SerializerFeature.UseSingleQuotes, true);
        writer.writeObject(Collections.emptyMap());
        writer.close();

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

public class JSONWriterTest_0 extends TestCase {

    public void test_writer() throws Exception {
        StringWriter out = new StringWriter();
        JSONWriter writer = new JSONWriter(out);
        writer.startArray();
        writer.writeObject("1");
        writer.writeObject("2");
        writer.writeObject("3");
        writer.endArray();
        writer.close();

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

    public void test_writer() throws Exception {
        Field field = JSONWriter.class.getDeclaredField("context");
        field.setAccessible(true);

        StringWriter out = new StringWriter();
        JSONWriter writer = new JSONWriter(out);
        writer.config(SerializerFeature.UseSingleQuotes, true);
        writer.startObject();

        Object context = field.get(writer);
        Field stateField = context.getClass().getDeclaredField("state");
        stateField.setAccessible(true);
        stateField.set(context, -1);

        Exception error = null;
        try {
            writer.startObject();
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
       
        writer.close();

    }
View Full Code Here

TOP

Related Classes of com.alibaba.fastjson.JSONWriter$Context

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.