Example usage for org.json.simple JSONObject writeJSONString

List of usage examples for org.json.simple JSONObject writeJSONString

Introduction

In this page you can find the example usage for org.json.simple JSONObject writeJSONString.

Prototype

public void writeJSONString(Writer out) throws IOException 

Source Link

Usage

From source file:us.mn.state.health.lims.common.provider.query.PendingAnalysisForTestProvider.java

@Override
public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String testId = request.getParameter("testId");

    String jResult;/*from w ww  .  j  a va2 s .c  o m*/
    JSONObject jsonResult = new JSONObject();
    String jString;

    if (GenericValidator.isBlankOrNull(testId)) {
        jResult = INVALID;
        jString = "Internal error, please contact Admin and file bug report";
    } else {
        jResult = createJsonGroupedAnalysis(testId, jsonResult);
        StringWriter out = new StringWriter();
        try {
            jsonResult.writeJSONString(out);
            jString = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
            jResult = INVALID;
            jString = "Internal error, please contact Admin and file bug report";
        } catch (IllegalStateException e) {
            e.printStackTrace();
            jResult = INVALID;
            jString = "Internal error, please contact Admin and file bug report";
        }
    }
    ajaxServlet.sendData(jString, jResult, request, response);

}

From source file:us.mn.state.health.lims.common.provider.query.TestNamesProvider.java

@Override
public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String testId = request.getParameter("testId");

    String jResult;//from  w w  w.  j  a  v a  2  s .  c om
    JSONObject jsonResult = new JSONObject();
    String jString;

    if (GenericValidator.isBlankOrNull(testId)) {
        jResult = INVALID;
        jString = "Internal error, please contact Admin and file bug report";
    } else {
        jResult = createJsonTestNames(testId, jsonResult);
        StringWriter out = new StringWriter();
        try {
            jsonResult.writeJSONString(out);
            jString = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
            jResult = INVALID;
            jString = "Internal error, please contact Admin and file bug report";
        } catch (IllegalStateException e) {
            e.printStackTrace();
            jResult = INVALID;
            jString = "Internal error, please contact Admin and file bug report";
        }
    }
    ajaxServlet.sendData(jString, jResult, request, response);

}

From source file:us.mn.state.health.lims.common.provider.query.TestReflexUserChoiceProvider.java

@Override
public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String resultIds = request.getParameter("resultIds");
    String analysisIds = request.getParameter("analysisIds");
    String testIds = request.getParameter("testIds");
    String rowIndex = request.getParameter("rowIndex");
    String accessionNumber = request.getParameter("accessionNumber");

    String jResult = "";
    JSONObject jsonResult = new JSONObject();
    String jString = "";
    if (GenericValidator.isBlankOrNull(resultIds) || GenericValidator.isBlankOrNull(testIds)
            || GenericValidator.isBlankOrNull(rowIndex) || (GenericValidator.isBlankOrNull(analysisIds)
                    && GenericValidator.isBlankOrNull(accessionNumber))) {
        jResult = INVALID;//from   ww w. j  a  v  a2s. c  o  m
        jString = "Internal error, please contact Admin and file bug report";
    } else {
        jResult = createJsonTestReflex(resultIds, analysisIds, testIds, accessionNumber, rowIndex, jsonResult);
        StringWriter out = new StringWriter();
        try {
            jsonResult.writeJSONString(out);
            jString = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
            jResult = INVALID;
            jString = "Internal error, please contact Admin and file bug report";
        }
    }
    ajaxServlet.sendData(jString, jResult, request, response);

}

From source file:us.mn.state.health.lims.dataexchange.aggregatereporting.TestUsageUpdate.java

@SuppressWarnings("unchecked")
private void updateExport(ReportExternalExport export, Map<String, Integer> testCountMap) {
    JSONParser parser = new JSONParser();
    Map<String, Long> databaseTestCountList = null;

    if (export.getData() == null) {
        export.setData("{}");
    }//from www .  j av a  2 s  .  c  o m

    export.setSend(true);

    try {
        databaseTestCountList = (Map<String, Long>) parser.parse(export.getData().replace("\n", ""),
                CONTAINER_FACTORY);

        for (String test : testCountMap.keySet()) {
            Long count = databaseTestCountList.get(test);
            databaseTestCountList.put(test, count == null ? 1 : count + testCountMap.get(test));
        }
    } catch (ParseException pe) {
        System.out.println(pe);
    }

    if (databaseTestCountList != null) {
        JSONObject json = new JSONObject();
        for (String name : databaseTestCountList.keySet()) {
            json.put(name, databaseTestCountList.get(name));
        }

        StringWriter buffer = new StringWriter();
        try {
            json.writeJSONString(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String data = buffer.toString().replace("\n", "");
        export.setData(data);
    }
}

From source file:Utility.JSONUtility.java

/**
 * Realizza il JSON partendo da una lista di pazienti
 *
 * @param lp lista di pazienti//from   w  w w.ja  va  2s.co m
 * @return JSON della lista di pazienti
 */
public static String listaPazientiToJSON(List<Paziente> lp) {
    //ESEMPIO {
    //            "mieiPazienti": [
    //                              {"idPaziente": "11", "nome": "Marco", "cognome": "Busso"},
    //                              {"idPaziente": "12", "nome": "Marco", "cognome": "Varesano"},
    //                              {"idPaziente": "13", "nome": "Tobia", "cognome": "Giani"}
    //                            ]
    //        }

    JSONObject obj = new JSONObject();

    List l = new LinkedList();

    for (Paziente p : lp) {
        Map m = new HashMap();
        m.put("idPaziente", p.getId().toString());
        m.put("nome", p.getNome());
        m.put("cognome", p.getCognome());
        l.add(m);
    }
    obj.put("mieiPazienti", l);

    StringWriter out = new StringWriter();
    String jsonText = "";
    try {
        obj.writeJSONString(out);
        jsonText = out.toString();
    } catch (IOException e) {
        return JSONFail;
    }
    return jsonText;
}

From source file:Utility.JSONUtility.java

/**
 * Realizza il JSON partendo da due input
 *
 * @param param1/*  w w w  . j ava  2s. c  o  m*/
 * @param param2
 * @return JSON contenente i due input
 */
public static String creaGenericoJSON(String param1, String param2) {
    //ESEMPIO {
    //            "param1": "param2"
    //        }

    JSONObject obj = new JSONObject();
    obj.put(param1, param2);

    StringWriter out = new StringWriter();
    String jsonText = "";
    try {
        obj.writeJSONString(out);
        jsonText = out.toString();
    } catch (IOException e) {
        return JSONFail;
    }
    return jsonText;
}

From source file:Utility.JSONUtility.java

/**
 * Realizza il JSON partendo da una cartella clinica
 *
 * @param cc cartella clinica del paziente
 * @return JSON della cartella clinica// w w  w  . j  a  va  2  s .  c  o m
 */
public static String cartellaClinicaToJSON(CartellaClinica cc) {
    //ESEMPIO {
    //            "idCC": "19",
    //            "anamnesi": "Mal di gola. Distorsione caviglia dx",
    //            "referti": [
    //                         {"idRM": "11", "tipoVisita": "Visita cardiologica", "data": "15/04/2013", "medico": "Bianchi", "diagnosi": "Mal di gola acuto"},
    //                         {"idRM": "14", "tipoVisita": "Visita ortopedica", "data": "22/03/2014", "medico": "Rossi", "diagnosi": "Distorsione caviglia dx"}
    //                       ]
    //        }

    List<RefertoMedico> lrm = cc.getLista_referti();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    JSONObject obj = new JSONObject();

    obj.put("idCC", cc.getId().toString());
    obj.put("anamnesi", cc.getAnamnesi());

    List l = new LinkedList();

    for (RefertoMedico rm : lrm) {
        Map m = new HashMap();
        m.put("idRM", rm.getId().toString());
        m.put("tipoVisita", rm.getTipoVisita().getNome());
        m.put("data", sdf.format(rm.getDataVisita()));
        m.put("medico", rm.getMedico().getCognome());
        m.put("diagnosi", rm.getDiagnosi());
        l.add(m);
    }
    obj.put("referti", l);

    StringWriter out = new StringWriter();
    String jsonText = "";
    try {
        obj.writeJSONString(out);
        jsonText = out.toString();
    } catch (IOException e) {
        return JSONFail;
    }
    return jsonText;
}

From source file:Utility.JSONUtility.java

/**
 * Realizza il JSON partendo da una lista di path di file
 *
 * @param multimedia lista di path di file multimediali
 * @return JSON delle immagini dei referti medici (codificate in base64)
 *///from w ww .j ava  2s .  c  om
public static String encodeImageToJSON(List<String> multimedia) {
    //ESEMPIO {
    //            "multimedia": [
    //                            {"image": "image_1"},
    //                            {"image": "image_2"},
    //                            {"image": "image_3"}
    //                          ]
    //        }              

    JSONObject obj = new JSONObject();
    List l = new LinkedList();

    String path = JSONUtility.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    String path2 = path.substring(0, path.indexOf("dist")) + "Ippocrate-war/web/";

    for (String multim : multimedia) {
        String path3 = path2 + multim;

        File imgFile = new File(path3);
        if (imgFile.exists()) {
            byte[] bytes;
            try {
                bytes = loadFile(imgFile);
            } catch (IOException ex) {
                return JSONFail;
            }
            byte[] encoded = Base64.encodeBase64(bytes);
            String encodedString = new String(encoded);
            Map m = new HashMap();
            m.put("image", encodedString);
            l.add(m);
        }
    }
    obj.put("multimedia", l);

    StringWriter out = new StringWriter();
    String jsonText = "";
    try {
        obj.writeJSONString(out);
        jsonText = out.toString();
    } catch (IOException e) {
        return JSONFail;
    }
    return jsonText;
}

From source file:Utility.JSONUtility.java

/**
 * Realizza il JSON partendo da una lista di prescrizioni mediche
 *
 * @param lpm lista di prescrizioni mediche
 * @return JSON delle prescrizioni mediche
 */// w  w w.j  av  a 2s.c  o m
public static String listaPMToJSON(List<PrescrizioneMedica> lpm) {
    //ESEMPIO {
    //            "prescrizioni": [
    //                              {"idPM": "34", "dataPrescrizione": "15/04/2013", "dataScadenza": "01/05/2013", "medicinale": "Analgesico", "quantita": "3"},
    //                              {"idPM": "33", "dataPrescrizione": "18/07/2013", "dataScadenza": "18/02/2014", "medicinale": "Antipiretico", "quantita": "1"},
    //                              {"idPM": "21", "dataPrescrizione": "22/02/2013", "dataScadenza": "15/04/2014", "medicinale": "Cerotti", "quantita": "5"}
    //                            ]
    //        }

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    JSONObject obj = new JSONObject();

    List l = new LinkedList();

    for (PrescrizioneMedica pm : lpm) {
        Map m = new HashMap();
        m.put("idPM", pm.getId().toString());
        m.put("dataPrescrizione", sdf.format(pm.getData_prescrizione()));
        m.put("dataScadenza", sdf.format(pm.getData_scadenza()));
        m.put("medicinale", pm.getMedicinale());
        m.put("quantita", pm.getNumero_confezioni());
        l.add(m);
    }
    obj.put("prescrizioni", l);

    StringWriter out = new StringWriter();
    String jsonText = "";
    try {
        obj.writeJSONString(out);
        jsonText = out.toString();
    } catch (IOException e) {
        return JSONFail;
    }
    return jsonText;
}