本文整理匯總了Java中org.json.simple.JSONValue.escape方法的典型用法代碼示例。如果您正苦於以下問題:Java JSONValue.escape方法的具體用法?Java JSONValue.escape怎麽用?Java JSONValue.escape使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.json.simple.JSONValue
的用法示例。
在下文中一共展示了JSONValue.escape方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: execute
import org.json.simple.JSONValue; //導入方法依賴的package包/類
@Override
public Object execute(String script) {
StringBuilder sb = new StringBuilder();
// Callback for scripts that want to send some message back to page-inspection.
// We utilize custom alert handling of WebEngine for this purpose.
sb.append("postMessageToNetBeans=function(e) {alert('"); // NOI18N
sb.append(WebBrowserImpl.PAGE_INSPECTION_PREFIX);
sb.append("'+JSON.stringify(e));};\n"); // NOI18N
String quoted = '\"'+JSONValue.escape(script)+'\"';
// We don't want to depend on what is the type of WebBrowser.executeJavaScript()
// for various types of script results => we stringify the result
// (i.e. pass strings only through executeJavaScript()). We decode
// the strigified result then.
sb.append("JSON.stringify({result : eval(").append(quoted).append(")});"); // NOI18N
String wrappedScript = sb.toString();
Object result = browserTab.executeJavaScript(wrappedScript);
String txtResult = result.toString();
try {
JSONObject jsonResult = (JSONObject)JSONValue.parseWithException(txtResult);
return jsonResult.get("result"); // NOI18N
} catch (ParseException ex) {
Logger.getLogger(ScriptExecutorImpl.class.getName()).log(Level.INFO, null, ex);
return ScriptExecutor.ERROR_RESULT;
}
}
示例2: sendBar
import org.json.simple.JSONValue; //導入方法依賴的package包/類
@Override
public Chat sendBar(String message, Player player) {
if (!message.startsWith("{") && !message.startsWith("[")) {
message = "{\"text\":\"" + JSONValue.escape(message) + "\"}";
}
PacketPlayOutChat packet = new PacketPlayOutChat(NMSUtil_V1_10_R1.serializeChat(message), (byte)2);
NMSUtil_V1_10_R1.sendPacket(player, packet);
return this;
}
示例3: getJsonComponent
import org.json.simple.JSONValue; //導入方法依賴的package包/類
private String getJsonComponent(TextAction action, String value, String display) {
if (action == TextAction.HOVER) {
//Just a hover text message.
return getJsonText(display) + ",\"hoverEvent\": {\"action\": \"show_text\",\"value\": {\"text\": \"\",\"extra\": [{" + getJsonText(value) + "}]}}";
} else {
//Check for inherited hover message like <<cmd||[[hover||text]]>> it will then display text, on hover display hover and on click run the command cmd.
Pattern p = Pattern.compile(TextAction.HOVER.getRegex(), Pattern.DOTALL);
Matcher m = p.matcher(display);
//When there is hover text change the display text to use the value from hover and set the hover text component.
String hover = "";
if (m.find()) {
hover = ",\"hoverEvent\": {\"action\": \"show_text\",\"value\": {\"text\": \"\",\"extra\": [{" + getJsonText(m.group(2)) + "}]}}";
display = m.group(3);
}
//Insertion doesn't use clickEvent
if (action == TextAction.INSERT) {
//We set obfuscated to false to fix the bug where it doesn't work. Bug: [MC-82425]
return getJsonText(display) + ",\"obfuscated\":false,\"insertion\": \"" + JSONValue.escape(value) + "\"" + hover;
}
//Append slash for commands if it's not there.
if (action == TextAction.CMD || action == TextAction.CMD_SUGGEST && !value.trim().startsWith("/")) {
value = "/" + value.trim();
}
//Return the JSON for general click events.
return getJsonText(display) + ",\"clickEvent\": {\"action\": \"" + JSONValue.escape(action.getName()) + "\",\"value\": \"" + value + "\"}" + hover;
}
}
示例4: getJsonText
import org.json.simple.JSONValue; //導入方法依賴的package包/類
/**
* Used to get JSON formatted text.
* <b>This does not parse syntax of {@link TextAction}!</b>
* <p/>
* All this does is convert chat colors like §a and §l etc to JSON syntax.
* For example a string like '§atest §b§lexample' would output in the following JSON:
* "text":"","extra":[{"text":"test", "color":"green"},{"text":"example","color":"aqua","bold":true}]
* <p/>
* <b>Important: </b> As you can see in the example it does not add '{' and '}' before and after the JSON.
* Manually add this to get valid JSON!
*
* @param text The text that needs to be converted to JSON.
* @return JSON text with tellraw format. (No { in front and no } on the end!)
*/
public static String getJsonText(String text) {
//When the text doesn't have color codes we just return the text as JSON but without any formatting.
if (!text.contains("§") || Str.stripColor(text).trim().isEmpty()) {
return "\"text\":\"" + JSONValue.escape(text) + "\"";
}
//Find all color codes within the string.
//For example '§a' or '§a§l' '§c§l§n' will mitch.
Pattern p = Pattern.compile("(§[\\da-fA-Fk-oK-OrR])+");
Matcher m = p.matcher(text);
StringBuilder JSON = new StringBuilder("\"text\":\"\",\"extra\":[");
int lastIndex = 0;
String lastMatch = "";
while (m.find()) {
//Create a text section for each set of color codes.
//Using the #getJsonFormat method to get the formatting for the colors in the current match.
//We ignore the first match because we want to color the matches with the previous color.
if (m.start() > lastIndex) {
JSON.append("{\"text\":\"" + JSONValue.escape(text.substring(lastIndex, m.start())) + "\"" + getJsonFormat(lastMatch) + "}");
JSON.append(",");
}
lastIndex = m.end();
lastMatch = m.group();
}
//Color the last match result if we haven't done it. (Will only be the case when there is one match)
if (lastIndex < text.length()) {
JSON.append("{\"text\": \"" + JSONValue.escape(text.substring(lastIndex, text.length())) + "\"" + getJsonFormat(lastMatch) + "}");
}
//Remove trailing comma if it's there
if (JSON.lastIndexOf(",") == JSON.length() - 1) {
JSON.setLength(JSON.length() - 1);
}
//Finalize
JSON.append("]");
return JSON.toString();
}
示例5: escape
import org.json.simple.JSONValue; //導入方法依賴的package包/類
/**
* Escapes the string to fit into Json values. Used internally.
* @param text text to escape
* @return escaped String
*/
public static String escape(String text) {
return JSONValue.escape(text);
}