This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Stream api
Qiao edited this page Jan 16, 2017
·
10 revisions
当需要处理超大JSON文本时,需要Stream API,在fastjson-1.1.32版本中开始提供Stream API。
如果你的JSON格式是一个巨大的JSON数组,有很多元素,则先调用startArray,然后挨个写入对象,然后调用endArray。
JSONWriter writer = new JSONWriter(new FileWriter("/tmp/huge.json"));
writer.startArray();
for (int i = 0; i < 1000 * 1000; ++i) {
writer.writeValue(new VO());
}
writer.endArray();
writer.close();
如果你的JSON格式是一个巨大的JSONObject,有很多Key/Value对,则先调用startObject,然后挨个写入Key和Value,然后调用endObject。
JSONWriter writer = new JSONWriter(new FileWriter("/tmp/huge.json"));
writer.startObject();
for (int i = 0; i < 1000 * 1000; ++i) {
writer.writeKey("x" + i);
writer.writeValue(new VO());
}
writer.endObject();
writer.close();
JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json"));
reader.startArray();
while(reader.hasNext()) {
VO vo = reader.readObject(VO.class);
// handle vo ...
}
reader.endArray();
reader.close();
JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json"));
reader.startObject();
while(reader.hasNext()) {
String key = reader.readString();
VO vo = reader.readObject(VO.class);
// handle vo ...
}
reader.endObject();
reader.close();
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner