List of usage examples for org.json.simple JSONValue escape
public static String escape(String s)
From source file:myproject.MyServer.java
public static void Add(HttpServletRequest request, HttpServletResponse response) throws IOException { try {//from w w w . ja v a 2 s . c om String jsonString = IOUtils.toString(request.getInputStream()); JSONObject json = (JSONObject) JSONValue.parse(jsonString); String student_name = (String) json.get("student_name"); Long regno = (Long) json.get("regno"); Double cgpa = (Double) json.get("cgpa"); String query = String.format( "INSERT INTO student " + "(student_name, regno, cgpa) " + "VALUES('%s',%d,%f)", JSONValue.escape(student_name), regno, cgpa); database.runUpdate(query); String result = database.getStudent(regno); response.getWriter().write(result); } catch (Exception ex) { JSONObject output = new JSONObject(); output.put("error", "Connection failed: " + ex.getMessage()); response.getWriter().write(JSONValue.toJSONString(output)); } }
From source file:myproject.MyServer.java
public static void Update(HttpServletRequest request, HttpServletResponse response) throws IOException { try {/* w w w . j a v a 2 s .c om*/ String jsonString = IOUtils.toString(request.getInputStream()); JSONObject json = (JSONObject) JSONValue.parse(jsonString); Long student_id = (Long) json.get("student_id"); String student_name = (String) json.get("student_name"); Long regno = (Long) json.get("regno"); Double cgpa = (Double) json.get("cgpa"); String query = String.format( "UPDATE student " + "SET student_name='%s'," + "regno=%d," + "cgpa=%f " + "WHERE student_id=%d", JSONValue.escape(student_name), regno, cgpa, student_id); database.runUpdate(query); String result = database.getStudent(regno); response.getWriter().write(result); } catch (Exception ex) { JSONObject output = new JSONObject(); output.put("error", "Connection failed: " + ex.getMessage()); response.getWriter().write(JSONValue.toJSONString(output)); } }
From source file:me.timothy.ddd.DDDUtils.java
private static void write(Writer out, Object o, int indentation) throws IOException { if (o == null) { out.write("null"); } else if (o instanceof Map<?, ?>) writeJSONPretty(out, (Map<?, ?>) o, indentation); else if (o instanceof Collection<?>) writeJSONPretty(out, (Collection<?>) o, indentation); else if (o instanceof Number) { out.write(o.toString());/*from w ww.j av a2s . co m*/ } else if (o instanceof String) { out.write("\""); out.write(JSONValue.escape((String) o)); out.write("\""); } else if (o instanceof Boolean) { out.write(o == Boolean.TRUE ? "true" : "false"); } else { throw new IllegalArgumentException("How do I write " + o.getClass().getName() + "?"); } }
From source file:com.namelessmc.plugin.NamelessSpigot.util.Json.java
/** * Escapes the string to fit into Json values. Used internally. * @param text text to escape/*from w w w .j a v a2 s . c om*/ * @return escaped String */ public static String escape(String text) { return JSONValue.escape(text); }
From source file:lv.semti.morphology.webservice.VerbResource.java
private String formatJSON(Collection<String> tags) { Iterator<String> i = tags.iterator(); String out = "["; while (i.hasNext()) { out += "\"" + JSONValue.escape(i.next()) + "\""; if (i.hasNext()) out += ", "; }/* w w w . j a va 2 s. c o m*/ out += "]"; return out; }
From source file:lv.semti.morphology.analyzer.Word.java
public String toJSONsingle() { if (isRecognized()) { /* is ir tad, ja vajag tikai vienu - ticam?ko formu. t? jau vartu atgriezt visu sarakstu. */ Wordform maxwf = getBestWordform(); //return maxwf.toJSON(); TODO - varbt ar o te vajag atgriezt return String.format("{\"V?rds\":\"%s\",\"Marjums\":\"%s\",\"Pamatforma\":\"%s\"}", JSONValue.escape(maxwf.getToken()), JSONValue.escape(maxwf.getTag()), JSONValue.escape(maxwf.getValue(AttributeNames.i_Lemma))); } else//from w w w .ja va 2s . c om return String.format("{\"V?rds\":\"%s\",\"Marjums\":\"-\",\"Pamatforma\":\"%s\"}", JSONValue.escape(getToken()), JSONValue.escape(getToken())); }
From source file:com.googlecode.fascinator.common.StorageDataUtil.java
/** * Safely escape the supplied string for use in JSON. * * @param value: The string to escape//from ww w. j a v a 2 s . c o m * @return String: The escaped string */ public String encodeJson(String value) { return JSONValue.escape(value); }
From source file:org.jitsi.videobridge.Conference.java
/** * Initializes a new <tt>String</tt> to be sent over an * <tt>SctpConnection</tt> in order to notify an <tt>Endpoint</tt> that the * dominant speaker in this multipoint conference has changed to a specific * <tt>Endpoint</tt>.// ww w. j a v a 2 s. co m * * @param dominantSpeaker the dominant speaker in this multipoint conference * @return a new <tt>String</tt> to be sent over an <tt>SctpConnection</tt> * in order to notify an <tt>Endpoint</tt> that the dominant speaker in this * multipoint conference has changed to <tt>dominantSpeaker</tt> */ private String createDominantSpeakerEndpointChangeEvent(Endpoint dominantSpeaker) { return "{\"colibriClass\":\"DominantSpeakerEndpointChangeEvent\"," + "\"dominantSpeakerEndpoint\":\"" + JSONValue.escape(dominantSpeaker.getID()) + "\"}"; }
From source file:org.jitsi.videobridge.rest.JSONSerializer.java
private static Object serializeSourceGroup(SourceGroupPacketExtension sourceGroup) { if (sourceGroup.getSemantics() != null && sourceGroup.getSemantics().length() != 0 && sourceGroup.getSources() != null && sourceGroup.getSources().size() != 0) { JSONObject sourceGroupJSONObject = new JSONObject(); // Add semantics sourceGroupJSONObject.put(SourceGroupPacketExtension.SEMANTICS_ATTR_NAME, JSONValue.escape(sourceGroup.getSemantics())); // Add sources JSONArray ssrcsJSONArray = new JSONArray(); for (SourcePacketExtension source : sourceGroup.getSources()) ssrcsJSONArray.add(Long.valueOf(source.getSSRC())); sourceGroupJSONObject.put(SOURCES, ssrcsJSONArray); return sourceGroupJSONObject; } else {/* w ww . ja v a 2s . com*/ return null; } }
From source file:org.trec.liveqa.GetYAnswersPropertiesFromQid.java
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: GetYAnswersPropertiesFromQid <plaintext-list-of-qids> <out-file>"); return;//from w w w .j a v a 2 s . co m } BufferedWriter writer = getWriter(args[1]); String[] qids = getQids(args[0]); for (String qid : qids) { System.out.println("Getting data for QID " + qid); Map<String, String> data = extractData(qid); JSONObject jo = new JSONObject(); for (Map.Entry<String, String> e : data.entrySet()) { jo.put(e.getKey(), JSONValue.escape(e.getValue())); } writer.append(jo.toString()); writer.newLine(); writer.flush(); } writer.close(); }