本文整理汇总了Java中org.json.simple.JSONObject.writeJSONString方法的典型用法代码示例。如果您正苦于以下问题:Java JSONObject.writeJSONString方法的具体用法?Java JSONObject.writeJSONString怎么用?Java JSONObject.writeJSONString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.json.simple.JSONObject
的用法示例。
在下文中一共展示了JSONObject.writeJSONString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processPendingResponses
import org.json.simple.JSONObject; //导入方法依赖的package包/类
synchronized void processPendingResponses(HttpServletRequest req, HttpServletResponse resp) throws IOException {
JSONArray responses = new JSONArray();
JSONStreamAware pendingResponse;
while ((pendingResponse = pendingResponses.poll()) != null) {
responses.add(pendingResponse);
}
if (responses.size() > 0) {
JSONObject combinedResponse = new JSONObject();
combinedResponse.put("responses", responses);
if (asyncContext != null) {
asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
try (Writer writer = asyncContext.getResponse().getWriter()) {
combinedResponse.writeJSONString(writer);
}
asyncContext.complete();
asyncContext = req.startAsync();
asyncContext.addListener(new UserAsyncListener());
asyncContext.setTimeout(5000);
} else {
resp.setContentType("text/plain; charset=UTF-8");
try (Writer writer = resp.getWriter()) {
combinedResponse.writeJSONString(writer);
}
}
} else {
if (asyncContext != null) {
asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
try (Writer writer = asyncContext.getResponse().getWriter()) {
JSON.emptyJSON.writeJSONString(writer);
}
asyncContext.complete();
}
asyncContext = req.startAsync();
asyncContext.addListener(new UserAsyncListener());
asyncContext.setTimeout(5000);
}
}
示例2: process
import org.json.simple.JSONObject; //导入方法依赖的package包/类
@Override
public void process(ProcessingContext<Corpus> ctx, Corpus corpus) throws ModuleException {
try {
AlvisAEExportResolvedObjects resObj = getResolvedObjects();
CadixeExportContext exportContext = new CadixeExportContext(ctx);
if (schemaFile != null) {
Reader r = new FileReader(schemaFile);
exportContext.schema = JSONValue.parse(r);
if (exportContext.schema == null)
processingException("could not parse " + schemaFile + " as JSON");
r.close();
}
for (Document doc : Iterators.loop(documentIterator(exportContext.evalCtx, corpus))) {
JSONObject json = documentToJSON(doc, exportContext);
OutputFile outFile = new OutputFile(outDir, resObj.fileName.evaluateString(exportContext.evalCtx, doc));
TargetStream target = new FileTargetStream("UTF-8", outFile);
Writer w = target.getWriter();
json.writeJSONString(w);
w.close();
}
}
catch (UnsupportedEncodingException uee) {
rethrow(uee);
}
catch (FileNotFoundException fnfe) {
rethrow(fnfe);
}
catch (IOException ioe) {
rethrow(ioe);
}
}
示例3: writeTo
import org.json.simple.JSONObject; //导入方法依赖的package包/类
@Override
public void writeTo(Map map, Class<?> aClass, Type type, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap,
OutputStream outputStream) throws IOException, WebApplicationException {
Writer writer = new OutputStreamWriter(outputStream, Charsets.UTF_8);
JSONObject.writeJSONString(map, writer);
writer.write(ENTER);
writer.flush();
}
示例4: send
import org.json.simple.JSONObject; //导入方法依赖的package包/类
synchronized void send(JSONStreamAware response) {
if (asyncContext == null) {
if (isInactive) {
// user not seen recently, no responses should be collected
return;
}
if (pendingResponses.size() > 1000) {
pendingResponses.clear();
// stop collecting responses for this user
isInactive = true;
if (secretPhrase == null) {
// but only completely remove users that don't have unlocked accounts
Users.remove(this);
}
return;
}
pendingResponses.offer(response);
} else {
JSONArray responses = new JSONArray();
JSONStreamAware pendingResponse;
while ((pendingResponse = pendingResponses.poll()) != null) {
responses.add(pendingResponse);
}
responses.add(response);
JSONObject combinedResponse = new JSONObject();
combinedResponse.put("responses", responses);
asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
try (Writer writer = asyncContext.getResponse().getWriter()) {
combinedResponse.writeJSONString(writer);
} catch (IOException e) {
Logger.logMessage("Error sending response to user", e);
}
asyncContext.complete();
asyncContext = null;
}
}
示例5: service
import org.json.simple.JSONObject; //导入方法依赖的package包/类
@Test
@TestDir
@SuppressWarnings("unchecked")
public void service() throws Exception {
String dir = TestDirHelper.getTestDir().getAbsolutePath();
String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));
Configuration conf = new Configuration(false);
conf.set("server.services", services);
Server server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Instrumentation instrumentation = server.get(Instrumentation.class);
assertNotNull(instrumentation);
instrumentation.incr("g", "c", 1);
instrumentation.incr("g", "c", 2);
instrumentation.incr("g", "c1", 2);
Instrumentation.Cron cron = instrumentation.createCron();
cron.start();
sleep(100);
cron.stop();
instrumentation.addCron("g", "t", cron);
cron = instrumentation.createCron();
cron.start();
sleep(200);
cron.stop();
instrumentation.addCron("g", "t", cron);
Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
@Override
public String getValue() {
return "foo";
}
};
instrumentation.addVariable("g", "v", var);
Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
@Override
public Long getValue() {
return 1L;
}
};
instrumentation.addSampler("g", "s", 10, varToSample);
Map<String, ?> snapshot = instrumentation.getSnapshot();
assertNotNull(snapshot.get("os-env"));
assertNotNull(snapshot.get("sys-props"));
assertNotNull(snapshot.get("jvm"));
assertNotNull(snapshot.get("counters"));
assertNotNull(snapshot.get("timers"));
assertNotNull(snapshot.get("variables"));
assertNotNull(snapshot.get("samplers"));
assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));
StringWriter writer = new StringWriter();
JSONObject.writeJSONString(snapshot, writer);
writer.close();
server.destroy();
}