Here you can find the source of quoteJsonLib(String string)
from net.sf.json.util.JSONUtils
Parameter | Description |
---|---|
string | a parameter |
public static String quoteJsonLib(String string)
//package com.java2s; public class Main { /**/*from www.j av a 2s. com*/ * <p>from net.sf.json.util.JSONUtils</p> * * @param string * @return String */ public static String quoteJsonLib(String string) { char b; char c = 0; int i; int len = string.length(); StringBuffer sb = new StringBuffer(len * 2); String t; char[] chars = string.toCharArray(); char[] buffer = new char[1030]; int bufferIndex = 0; sb.append('"'); for (i = 0; i < len; i += 1) { if (bufferIndex > 1024) { sb.append(buffer, 0, bufferIndex); bufferIndex = 0; } b = c; c = chars[i]; switch (c) { case '\\': case '"': buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = c; break; case '/': if (b == '<') { buffer[bufferIndex++] = '\\'; } buffer[bufferIndex++] = c; break; default: if (c < ' ') { switch (c) { case '\b': buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = 'b'; break; case '\t': buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = 't'; break; case '\n': buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = 'n'; break; case '\f': buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = 'f'; break; case '\r': buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = 'r'; break; default: t = "000" + Integer.toHexString(c); int tLength = t.length(); buffer[bufferIndex++] = '\\'; buffer[bufferIndex++] = 'u'; buffer[bufferIndex++] = t.charAt(tLength - 4); buffer[bufferIndex++] = t.charAt(tLength - 3); buffer[bufferIndex++] = t.charAt(tLength - 2); buffer[bufferIndex++] = t.charAt(tLength - 1); } } else { buffer[bufferIndex++] = c; } } } sb.append(buffer, 0, bufferIndex); sb.append('"'); return sb.toString(); } public static String toString(Object obj) { return toString(obj, ""); } public static String toString(Object obj, String defaultVal) { if (obj == null) { return defaultVal; } return obj.toString(); } public static String toString(String[] strs) { String strWithComma = ""; if (strs == null) { return ""; } else { for (int i = 0; i < strs.length; i++) { if (i == 0) { strWithComma = strs[i]; } else { strWithComma += "," + strs[i]; } } } return strWithComma; } }