Java String Sanitize sanitizeForJson(String data)

Here you can find the source of sanitizeForJson(String data)

Description

Sanitize a string for inclusion in a JSON string Because most browsers will interpret HTML tags e.g.

License

Open Source License

Declaration

public static String sanitizeForJson(String data) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   www .ja  va  2  s  . c  om*/
     * Sanitize a string for inclusion in a JSON string Because most browsers
     * will interpret HTML tags e.g. </script> that appear in Javascript as
     * document level elements. Works by replacing angle brackets with their
     * character literal equivalents. See
     * http://www.wwco.com/~wls/blog/2007/04/25
     * /using-script-in-a-javascript-literal/ for discussion of the problem and
     * the solution.
     */
    public static String sanitizeForJson(String data) {
        data = data.replace("<", "\\x3C");
        data = data.replace(">", "\\x3E");
        // UI-destroying LINE-SEPERATOR character of doom. It's really rare, so lets just replace it.
        data = data.replace("\u2028", "");
        return data;
    }
}

Related

  1. sanitizeDescription(String description)
  2. sanitizeDir(String name)
  3. sanitizeFolderName(String s)
  4. sanitizeForCmisName(String in)
  5. sanitizeForCsv(String str)
  6. sanitizeForLogMessage(String unsanitizedString)
  7. sanitizeForSearch(String str)
  8. sanitizeForSemgrexName(String text)
  9. sanitizeForTableName(String input)