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());
}
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());
}
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);
}
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);
}
}
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);
}
}
Aggregations