List of usage examples for org.json.simple JSONValue toJSONString
public static String toJSONString(Object value)
From source file:org.apache.zookeeper.graph.JsonGenerator.java
public String toString() { return JSONValue.toJSONString(root); }
From source file:org.apache.zookeeper.graph.servlets.FileLoader.java
String handleRequest(JsonRequest request) throws Exception { String output = ""; String file = request.getString("path", "/"); JSONObject o = new JSONObject(); try {//from w ww . ja va 2 s . c om this.source.addSource(file); o.put("status", "OK"); } catch (Exception e) { o.put("status", "ERR"); o.put("error", e.toString()); } return JSONValue.toJSONString(o); }
From source file:org.apache.zookeeper.graph.servlets.Fs.java
String handleRequest(JsonRequest request) throws Exception { String output = ""; JSONArray filelist = new JSONArray(); File base = new File(request.getString("path", "/")); if (!base.exists() || !base.isDirectory()) { throw new FileNotFoundException("Couldn't find [" + request + "]"); }//from w w w . j av a 2 s . c o m File[] files = base.listFiles(); Arrays.sort(files, new Comparator<File>() { public int compare(File o1, File o2) { if (o1.isDirectory() != o2.isDirectory()) { if (o1.isDirectory()) { return -1; } else { return 1; } } return o1.getName().compareToIgnoreCase(o2.getName()); } }); for (File f : files) { JSONObject o = new JSONObject(); o.put("file", f.getName()); o.put("type", f.isDirectory() ? "D" : "F"); o.put("path", f.getCanonicalPath()); filelist.add(o); } return JSONValue.toJSONString(filelist); }
From source file:org.apache.zookeeper.graph.servlets.JsonServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); try {/*from w ww . j a v a 2s . c om*/ String req = request.getRequestURI().substring(request.getServletPath().length()); response.getWriter().println(handleRequest(new JsonRequest(request))); } catch (Exception e) { JSONObject o = new JSONObject(); o.put("error", e.toString()); response.getWriter().println(JSONValue.toJSONString(o)); } catch (java.lang.OutOfMemoryError oom) { JSONObject o = new JSONObject(); o.put("error", "Out of memory. Perhaps you've requested too many logs. Try narrowing you're filter criteria."); response.getWriter().println(JSONValue.toJSONString(o)); } }
From source file:org.apache.zookeeper.graph.servlets.NumEvents.java
String handleRequest(JsonRequest request) throws Exception { String output = ""; long starttime = 0; long endtime = 0; long period = 0; starttime = request.getNumber("start", 0); endtime = request.getNumber("end", 0); period = request.getNumber("period", 0); if (starttime == 0) { starttime = source.getStartTime(); }// w ww. j av a 2s. co m if (endtime == 0) { if (period > 0) { endtime = starttime + period; } else { endtime = source.getEndTime(); } } LogIterator iter = source.iterator(starttime, endtime); JSONObject data = new JSONObject(); data.put("startTime", starttime); data.put("endTime", endtime); long size = 0; size = iter.size(); data.put("numEntries", size); if (LOG.isDebugEnabled()) { LOG.debug("handle(start= " + starttime + ", end=" + endtime + ", numEntries=" + size + ")"); } return JSONValue.toJSONString(data); }
From source file:org.apache.zookeeper.graph.servlets.Throughput.java
public String handleRequest(JsonRequest request) throws Exception { long starttime = 0; long endtime = 0; long period = 0; long scale = 0; starttime = request.getNumber("start", 0); endtime = request.getNumber("end", 0); period = request.getNumber("period", 0); if (starttime == 0) { starttime = source.getStartTime(); }//www. j a v a 2 s . co m if (endtime == 0) { if (period > 0) { endtime = starttime + period; } else { endtime = source.getEndTime(); } } String scalestr = request.getString("scale", "minutes"); if (scalestr.equals("seconds")) { scale = MS_PER_SEC; } else if (scalestr.equals("hours")) { scale = MS_PER_HOUR; } else { scale = MS_PER_MIN; } LogIterator iter = source.iterator(starttime, endtime); long current = 0; long currentms = 0; HashSet<Long> zxids_ms = new HashSet<Long>(); long zxidcount = 0; JSONArray events = new JSONArray(); while (iter.hasNext()) { LogEntry e = iter.next(); if (e.getType() != LogEntry.Type.TXN) { continue; } TransactionEntry cxn = (TransactionEntry) e; long ms = cxn.getTimestamp(); long inscale = ms / scale; if (currentms != ms && currentms != 0) { zxidcount += zxids_ms.size(); zxids_ms.clear(); } if (inscale != current && current != 0) { JSONObject o = new JSONObject(); o.put("time", current * scale); o.put("count", zxidcount); events.add(o); zxidcount = 0; } current = inscale; currentms = ms; zxids_ms.add(cxn.getZxid()); } JSONObject o = new JSONObject(); o.put("time", current * scale); o.put("count", zxidcount); events.add(o); iter.close(); return JSONValue.toJSONString(events); }
From source file:org.arkhamnetwork.playersync.utils.SerializationUtils.java
public static byte[] serializePotionEffects(PotionEffect[] ef) throws SerializationException { List<Map<String, Object>> effects = new ArrayList<>(ef.length); for (PotionEffect effect : ef) { effects.add(effect.serialize()); }//www .j a va 2 s. c om return JSONValue.toJSONString(effects).getBytes(); }
From source file:org.biokoframework.http.exception.impl.ExceptionResponseBuilderImpl.java
@Override public HttpServletResponse build(HttpServletResponse response, Exception exception, Fields input, Fields output) throws IOException { response.setContentType(ContentType.APPLICATION_JSON.toString()); LOGGER.info("Before choosing"); response.setStatus(chooseStatusCode(exception)); LOGGER.info("After choosing"); List<ErrorEntity> errors = null; if (exception instanceof BiokoException) { errors = ((BiokoException) exception).getErrors(); }// ww w . j a va 2s .c om if (errors == null || errors.isEmpty()) { errors = new ArrayList<>(); StringBuilder message = new StringBuilder().append("An unexpected exception as been captured ") .append(descriptionOf(exception)); ErrorEntity entity = new ErrorEntity(); entity.setAll(new Fields(ErrorEntity.ERROR_CODE, FieldNames.CONTAINER_EXCEPTION_CODE, ErrorEntity.ERROR_MESSAGE, message.toString())); errors.add(entity); } IOUtils.copy(new StringReader(JSONValue.toJSONString(errors)), response.getWriter()); return response; }
From source file:org.biokoframework.http.exception.impl.ExceptionResponseBuilderTest.java
@Test public void testBiokoExceptionWithErrorEntity() throws Exception { ExceptionResponseBuilderImpl builder = new ExceptionResponseBuilderImpl(fCodesMap); MockResponse mockResponse = new MockResponse(); ErrorEntity error = new ErrorEntity(); error.setAll(new Fields(ErrorEntity.ERROR_CODE, 12345, ErrorEntity.ERROR_MESSAGE, "Failed at life")); MockException mockException = new MockException(error); mockResponse = (MockResponse) builder.build(mockResponse, mockException, null, null); assertThat(mockResponse.getContentType(), is(equalTo(ContentType.APPLICATION_JSON.toString()))); assertThat(mockResponse.getStatus(), is(equalTo(626))); assertThat(mockResponse, hasToString(equalTo(JSONValue.toJSONString(mockException.getErrors())))); }
From source file:org.biokoframework.http.response.impl.JsonResponseBuilderImpl.java
@Override protected void safelyBuild(HttpServletRequest request, HttpServletResponse response, Fields input, Fields output) throws IOException, RequestNotSupportedException { response.setContentType(APPLICATION_JSON + withCharsetFrom(request)); Object responseContent = output.get(GenericFieldNames.RESPONSE); for (Map.Entry<String, String> entry : fHeadersMapping.entrySet()) { if (output.containsKey(entry.getKey())) { response.setHeader(entry.getValue(), output.get(entry.getKey()).toString()); }/*from w w w.j av a 2 s . co m*/ } Writer writer = response.getWriter(); IOUtils.write(JSONValue.toJSONString(responseContent), writer); }