/** * Transforms a <code>List<GenesDAO></code> to a JSON string. * * @param genesDAOList the list to be transformed * @return the transformed JSON string */ public static String toJSON(List<GenesDAO> genesDAOList) { JSONArray jsonList = new JSONArray(); for (GenesDAO gene : genesDAOList) { JSONObject jsonGeneDAO = new JSONObject(); jsonGeneDAO.put("centimorgan", gene.getCentimorgan() == null ? "" : gene.getCentimorgan()); jsonGeneDAO.put("chromosome", gene.getChromosome() == null ? "" : gene.getChromosome()); jsonGeneDAO.put("cytoband", gene.getCytoband() == null ? "" : gene.getCytoband()); jsonGeneDAO.put("ensembl_ref", gene.getEnsembl_ref() == null ? "" : gene.getEnsembl_ref()); jsonGeneDAO.put( "founder_line_number", gene.getFounder_line_number() == null ? "" : gene.getFounder_line_number()); jsonGeneDAO.put("id_gene", Integer.toString(gene.getId_gene())); jsonGeneDAO.put("mgi_ref", gene.getMgi_ref() == null ? "" : gene.getMgi_ref()); jsonGeneDAO.put("name", gene.getName() == null ? "" : gene.getName()); jsonGeneDAO.put( "plasmid_construct", gene.getPlasmid_construct() == null ? "" : gene.getPlasmid_construct()); jsonGeneDAO.put("promoter", gene.getPromoter() == null ? "" : gene.getPromoter()); jsonGeneDAO.put("species", gene.getSpecies() == null ? "" : gene.getSpecies()); jsonGeneDAO.put("symbol", gene.getSymbol() == null ? "" : gene.getSymbol()); if ((gene.getSynonyms() != null) && (gene.getSynonyms().size() > 0)) { JSONArray synonyms = new JSONArray(); Iterator<Syn_GenesDAO> iterator = gene.getSynonyms().iterator(); while (iterator.hasNext()) { Syn_GenesDAO syn_genesDAO = iterator.next(); JSONObject synonym = new JSONObject(); synonym.put("id_syn", Integer.toString(syn_genesDAO.getId_syn())); synonym.put("name", syn_genesDAO.getName()); synonym.put("symbol", syn_genesDAO.getSymbol()); synonyms.add(synonym); } jsonGeneDAO.put("synonyms", synonyms); } jsonList.add(jsonGeneDAO); } // Gson dosn't reserve space for fields with null values!!!! //////// Gson gson = new Gson(); //////// String s = gson.toJson(genesDAOList); //////// System.out.println(s); //////// return s; return jsonList.toString(); }
/** * Remaps null fields to empty strings suitable for use in the client. * * @param genesDAO the instance to remap * @return the same instance, with nulls remapped to empty strings. */ private GenesDAO remapNulls(GenesDAO genesDAO) { // Re-map null fields to empty strings. if (genesDAO != null) { if (genesDAO.getCentimorgan() == null) genesDAO.setCentimorgan(""); if (genesDAO.getChromosome() == null) genesDAO.setChromosome(""); if (genesDAO.getCytoband() == null) genesDAO.setCytoband(""); if (genesDAO.getEnsembl_ref() == null) genesDAO.setEnsembl_ref(""); if (genesDAO.getFounder_line_number() == null) genesDAO.setFounder_line_number(""); if (genesDAO.getLast_change() == null) genesDAO.setLast_change(""); if (genesDAO.getMgi_ref() == null) genesDAO.setMgi_ref(""); if (genesDAO.getName() == null) genesDAO.setName(""); if (genesDAO.getPlasmid_construct() == null) genesDAO.setPlasmid_construct(""); if (genesDAO.getPromoter() == null) genesDAO.setPromoter(""); if (genesDAO.getSpecies() == null) genesDAO.setSpecies(""); if (genesDAO.getSymbol() == null) genesDAO.setSymbol(""); if (genesDAO.getUsername() == null) genesDAO.setUsername(""); } return genesDAO; }