Java 类com.alibaba.fastjson.JSONWriter 实例源码

项目:GitHub    文件:JSONWriterTest_3.java   
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());
}
项目:GitHub    文件:JSONWriterTest.java   
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());
}
项目:GitHub    文件:LargeTest.java   
public void test_0() throws Exception {
    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();
    reader.readObject();
    reader.endArray();
    reader.close();

}
项目:GitHub    文件:JSONWriterTest_1.java   
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());
}
项目:GitHub    文件:JSONWriterTest_2.java   
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());
}
项目:GitHub    文件:JSONWriterTest_error.java   
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();

}
项目:GitHub    文件:SerializeWriterTest_BrowserSecure_5_script_model.java   
public void test_3() throws Exception {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 500; i++) {
        buf.append("<script>");
    }

    StringBuilder buf1 = new StringBuilder();
    buf1.append("{\"value\":\"");
    for (int i = 0; i < 500; i++) {
        buf1.append("\\u003Cscript\\u003E");
    }
    buf1.append("\"}");

    Model object = new Model();
    object.value = buf.toString();

    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.BrowserSecure, true);
    writer.writeObject(object);
    writer.flush();

    assertEquals(buf1.toString(), out.toString());
}
项目:GitHub    文件:MultiFieldIntTest_writer.java   
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);
    }
}
项目:GitHub    文件:SerializeWriterTest_BrowserSecure_4_script.java   
public void test_3() throws Exception {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 500; i++) {
        buf.append("<script>");
    }

    StringBuilder buf1 = new StringBuilder();
    buf1.append('"');
    for (int i = 0; i < 500; i++) {
        buf1.append("\\u003Cscript\\u003E");
    }
    buf1.append('"');

    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.BrowserSecure, true);
    writer.writeObject(buf.toString());
    writer.flush();

    assertEquals(buf1.toString(), out.toString());
}
项目:clouddisk    文件:JSONBinary.java   
public <T> void writeObjToDisk(final T obj) {
    if (StringUtils.isNotBlank(binaryFilename)) {
        final File file = new File(getBianaryFilenamePath());
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }    
        try (JSONWriter jsonWriter=new JSONWriter(new FileWriter(file))){
            jsonWriter.startObject();
            jsonWriter.writeObject(obj);
            jsonWriter.endObject();
            jsonWriter.flush();
            LOGGER.info("文件 { " + file.getName() + " } 写入位置" + file.getAbsolutePath());
        } catch (IOException e) {
            LOGGER.error(ERROR,e);
        }
    }
}
项目:clouddisk    文件:JSONBinary.java   
public <T> void writeListToDisk(final List<T> lists) {
    if (StringUtils.isNotBlank(binaryFilename)) {
        final File file = new File(getBianaryFilenamePath());
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        try (JSONWriter jsonWriter=new JSONWriter(new FileWriter(file))){
            jsonWriter.startArray();
            lists.forEach(jsonWriter::writeObject);
            jsonWriter.endArray();
            jsonWriter.flush();
        } catch (IOException e) {
            LOGGER.error(ERROR,e);
        }
    }
}
项目:GitHub    文件:JSONWriterTest.java   
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());
}
项目:GitHub    文件:JSONWriterTest.java   
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());
}
项目:GitHub    文件:JSONWriterTest_0.java   
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());
}
项目:GitHub    文件:JSONWriterTest_4.java   
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());
}
项目:GitHub    文件:JSONWriterTest_5.java   
public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);

    writer.startObject();
    writer.writeKey("value");
    writer.writeObject((String) null);
    writer.endObject();

    writer.close();

    Assert.assertEquals("{\"value\":null}", out.toString());
}
项目:GitHub    文件:JSONWriterTest_6.java   
public void test_writer() throws Exception {
    StringWriter out = new StringWriter();
    JSONWriter writer = new JSONWriter(out);
    writer.config(SerializerFeature.WriteNullStringAsEmpty, true);

    writer.startObject();
    writer.writeKey("value");
    writer.writeObject((String) null);
    writer.endObject();

    writer.close();

    Assert.assertEquals("{\"value\":\"\"}", out.toString());
}
项目:GitHub    文件:MultiFieldIntTest_writer2.java   
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);
    }
}
项目:GitHub    文件:Issue169.java   
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();
}
项目:otter-G    文件:JsonUtilsTest.java   
@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);
}
项目:otter    文件:JsonUtilsTest.java   
@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);
}
项目:GitHub    文件:JSONWriterTest.java   
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());
}
项目:GitHub    文件:JSONWriterTest.java   
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());
}