Example usage for org.json JSONWriter endObject

List of usage examples for org.json JSONWriter endObject

Introduction

In this page you can find the example usage for org.json JSONWriter endObject.

Prototype

public JSONWriter endObject() throws JSONException 

Source Link

Document

End an object.

Usage

From source file:org.everit.osgi.webconsole.configuration.ServiceSuggestion.java

public void toJSON(final JSONWriter writer) {
    writer.object();//from  w w  w  .jav  a  2s  .c  om
    writer.key("serviceClass");
    writer.value(serviceClass);
    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.
 *///from  w w  w.ja  va 2s. co m
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.
 */// w  w  w  .  j av a  2  s .  c om
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:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save book to file./*ww  w  . j a  va  2 s  .c o  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.//from  w  w  w . ja v  a 2s.c o m
 * 
 * @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  ww  w  . ja v  a 2s.c  o  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./*  w ww.j  a  v  a  2  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./*  w  ww .j  a v  a2s.  c  o  m*/
 * 
 * @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();
    }
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save period value./*w  w w .ja va  2s .  c  o  m*/
 * 
 * @param periodBudget
 * @param jsonWriter
 */
protected static void savePeriodValue(Map<String, Value> periodValue, JSONWriter jsonWriter) {
    if (!isEmpty(periodValue)) {
        jsonWriter.key("periodValue");
        jsonWriter.array();
        Iterator<String> it = periodValue.keySet().iterator();
        while (it.hasNext()) {
            String id = it.next();
            Value value = periodValue.get(id);
            if (!value.isEmpty()) {
                jsonWriter.object();
                jsonWriter.key("periodId").value(id);
                saveValue(value, jsonWriter);
                jsonWriter.endObject();
            }
        }
        jsonWriter.endArray();
    }
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Value object./*from  w w w.j a v a2  s.c  om*/
 * 
 * @param value
 * @param jsonWriter
 */
protected static void saveValue(Value value, JSONWriter jsonWriter) {
    if (value != null && !value.isEmpty()) {
        jsonWriter.key("value");
        jsonWriter.object();
        if (value.getStartOfPeriod() != 0.0) {
            jsonWriter.key("startOfPeriod").value(value.getStartOfPeriod());
        }
        if (value.getDebit() != 0.0) {
            jsonWriter.key("debit").value(value.getDebit());
        }
        if (value.getCredit() != 0.0) {
            jsonWriter.key("credit").value(value.getCredit());
        }
        if (value.getEndOfPeriod() != 0.0) {
            jsonWriter.key("endOfPeriod").value(value.getEndOfPeriod());
        }
        if (value.getControl() != 0.0) {
            jsonWriter.key("control").value(value.getControl());
        }
        if (value.isManual()) {
            jsonWriter.key("manual").value("y");
        }
        jsonWriter.endObject();
    }
}