List of usage examples for org.json JSONWriter object
public JSONWriter object() throws JSONException
From source file:org.everit.osgi.webconsole.configuration.DisplayedAttribute.java
private void typeToJSON(final JSONWriter writer) { writer.object(); writer.key("baseType"); writer.value(type);/*ww w .j a va2s. com*/ writer.key("maxOccurences"); if (maxOccurences == Integer.MIN_VALUE || maxOccurences == Integer.MAX_VALUE) { writer.value("unbound"); } else { writer.value(maxOccurences); } optionsToJSON(writer); writer.endObject(); }
From source file:org.everit.osgi.webconsole.configuration.ServiceSuggestion.java
public void toJSON(final JSONWriter writer) { writer.object(); writer.key("serviceClass"); writer.value(serviceClass);/*from w w w . j a v a 2 s .com*/ writer.key("id"); writer.value(serviceId); writer.key("properties"); writer.array(); for (String key : serviceProperties.keySet()) { writer.object(); writer.key("key"); writer.value(key); writer.key("value"); writer.value(serviceProperties.get(key)); writer.endObject(); } writer.endArray(); writer.endObject(); }
From source file:org.mapfish.print.servlet.MapPrinterServlet.java
/** * Create the PDF and returns to the client (in JSON) the URL to get the PDF. *//*w ww .ja v a 2 s .c om*/ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String basePath) throws ServletException { TempFile tempFile = null; try { purgeOldTemporaryFiles(); String spec = getSpecFromPostBody(httpServletRequest); tempFile = doCreatePDFFile(spec, httpServletRequest); if (tempFile == null) { error(httpServletResponse, "Missing 'spec' parameter", 500); return; } } catch (Throwable e) { deleteFile(tempFile); error(httpServletResponse, e); return; } final String id = generateId(tempFile); httpServletResponse.setContentType("application/json; charset=utf-8"); PrintWriter writer = null; try { writer = httpServletResponse.getWriter(); JSONWriter json = new JSONWriter(writer); json.object(); { json.key("getURL").value(basePath + "/" + id + TEMP_FILE_SUFFIX); } json.endObject(); } catch (JSONException e) { deleteFile(tempFile); throw new ServletException(e); } catch (IOException e) { deleteFile(tempFile); throw new ServletException(e); } finally { if (writer != null) { writer.close(); } } addTempFile(tempFile, id); }
From source file:org.mapfish.print.servlet.MapPrinterServlet.java
/** * To get (in JSON) the information about the available formats and CO. *///from w ww. java 2 s . co m protected void getInfo(HttpServletRequest req, HttpServletResponse resp, String basePath) throws ServletException, IOException { app = req.getParameter("app"); //System.out.println("app = "+app); MapPrinter printer = getMapPrinter(app); resp.setContentType("application/json; charset=utf-8"); final PrintWriter writer = resp.getWriter(); try { final String var = req.getParameter("var"); if (var != null) { writer.print(var + "="); } JSONWriter json = new JSONWriter(writer); try { json.object(); { printer.printClientConfig(json); json.key("printURL").value(basePath + PRINT_URL); json.key("createURL").value(basePath + CREATE_URL); if (app != null) { json.key("app").value(app); } } json.endObject(); } catch (JSONException e) { throw new ServletException(e); } if (var != null) { writer.print(";"); } } finally { writer.close(); } }
From source file:com.composum.osgi.core.test.httpinterface.TestExecutorServlet.java
/** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) *//* w w w . j a v a 2s.c o m*/ @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final List<Result> runTests = this.testExecutorService.runTests(); final JSONWriter jsonWriter = new org.json.JSONWriter(resp.getWriter()); try { JSONWriter tests = jsonWriter.object().key("tests").array(); for (final Result result : runTests) { final String name = result.getDescription().getDisplayName(); final boolean success = result.isSuccess(); final Failure failure = result.getFailure(); final String message = success ? "ok" : failure.getMessage(); final String trace = success ? "" : failure.getTrace(); tests = tests.object().key("name").value(name).key("success").value(success).key("message") .value(message).key("trace").value(trace).endObject(); } tests.endArray().endObject(); } catch (final JSONException e) { throw new ServletException("error writing JSON response: " + e.getMessage(), e); } }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Save book to file.//from ww w . j a va2 s.co m * * @param file * @param book * @throws Exception */ public static void saveBook(BookFile bookFile) throws Exception { FileWriter writer = null; JSONWriter jsonWriter = null; try { Book book = bookFile.getBook(); writer = new FileWriter(bookFile.getFilePath()); jsonWriter = new JSONWriter(writer); jsonWriter.object().key("book"); jsonWriter.object(); jsonWriter.key("name").value(book.getName()); jsonWriter.key("currPeriodId").value(book.getCurrPeriodId()); savePeriodBudget(book.getPeriodBudget(), jsonWriter); savePeriodValue(book.getPeriodValue(), jsonWriter); jsonWriter.key("assets"); saveAccount(book.getAssetList(), jsonWriter); jsonWriter.key("liability"); saveAccount(book.getLiabilityList(), jsonWriter); jsonWriter.key("income"); saveAccount(book.getIncomeList(), jsonWriter); jsonWriter.key("expenses"); saveAccount(book.getExpenseList(), jsonWriter); jsonWriter.key("periodList"); savePeriodList(book.getPeriodList(), jsonWriter); jsonWriter.endObject(); jsonWriter.endObject(); } finally { if (writer != null) { writer.flush(); writer.close(); } } }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Save folder.//ww w. j a v a 2s . c om * * @param account * @param jsonWriter */ protected static void saveAccount(Account account, JSONWriter jsonWriter) { jsonWriter.object(); jsonWriter.key("name").value(account.getName()); jsonWriter.key("type").value(account.getType()); if (account instanceof Folder) { jsonWriter.key("folder").value("y"); } if (account.isHidden()) { jsonWriter.key("hidden").value("y"); if (account.getHiddenDate() != null) { jsonWriter.key("hiddenDate").value(Formatter.formatDate(account.getHiddenDate())); } } savePeriodBudget(account.getPeriodBudget(), jsonWriter); savePeriodValue(account.getPeriodValue(), jsonWriter); if (account instanceof Folder) { jsonWriter.key("list"); jsonWriter.array(); for (Account child : ((Folder) account).getList()) { saveAccount(child, jsonWriter); } jsonWriter.endArray(); } jsonWriter.endObject(); }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Save period list./*from w w w. j a v a 2s . co m*/ * * @param periodList * @param jsonWriter */ protected static void savePeriodList(List<Period> periodList, JSONWriter jsonWriter) { jsonWriter.array(); for (Period period : periodList) { jsonWriter.object(); jsonWriter.key("id").value(period.getId()); jsonWriter.key("date").value(Formatter.formatDate(period.getDate())); saveValue(period.getValue(), jsonWriter); saveTransactions(period.getTransactions(), jsonWriter); jsonWriter.endObject(); } jsonWriter.endArray(); }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Save transactions./*from ww w .j a v a2 s .c o m*/ * * @param trans * @param jsonWriter */ protected static void saveTransactions(List<Transaction> trans, JSONWriter jsonWriter) { if (trans.size() > 0) { jsonWriter.key("transactions"); jsonWriter.array(); for (Transaction tr : trans) { jsonWriter.object(); jsonWriter.key("from").value(tr.getFrom().getAbsName()); jsonWriter.key("to").value(tr.getTo().getAbsName()); jsonWriter.key("description").value(tr.getDescription()); jsonWriter.key("value").value(tr.getValue()); jsonWriter.endObject(); } jsonWriter.endArray(); } }
From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java
/** * Save period budget.//from www.j ava 2 s . com * * @param periodBudget * @param jsonWriter */ protected static void savePeriodBudget(Map<String, Double> periodBudget, JSONWriter jsonWriter) { if (!isEmptyBudget(periodBudget)) { jsonWriter.key("periodBudget"); jsonWriter.array(); Iterator<String> it = periodBudget.keySet().iterator(); while (it.hasNext()) { String id = it.next(); Double value = periodBudget.get(id); if (value.doubleValue() != 0.0) { jsonWriter.object(); jsonWriter.key("periodId").value(id); jsonWriter.key("budget").value(value); jsonWriter.endObject(); } } jsonWriter.endArray(); } }