Java String Quote quote(String value)

Here you can find the source of quote(String value)

Description

Safely escape an arbitrary string as a JSON string literal.

License

Apache License

Declaration

public static String quote(String value) 

Method Source Code

//package com.java2s;
/*// w  w w  .  j a v a  2  s . co m
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /**
     * Safely escape an arbitrary string as a JSON string literal.
     */
    public static String quote(String value) {
        StringBuilder toReturn = new StringBuilder("\"");
        for (int i = 0; i < value.length(); i++) {
            char c = value.charAt(i);

            String toAppend = String.valueOf(c);
            switch (c) {
            case '\b':
                toAppend = "\\b";
                break;
            case '\t':
                toAppend = "\\t";
                break;
            case '\n':
                toAppend = "\\n";
                break;
            case '\f':
                toAppend = "\\f";
                break;
            case '\r':
                toAppend = "\\r";
                break;
            case '"':
                toAppend = "\\\"";
                break;
            case '\\':
                toAppend = "\\\\";
                break;
            default:
                if (isControlChar(c)) {
                    toAppend = escapeCharAsUnicode(c);
                }
            }
            toReturn.append(toAppend);
        }
        toReturn.append("\"");
        return toReturn.toString();
    }

    private static boolean isControlChar(char c) {
        return (c >= 0x00 && c <= 0x1f) || (c >= 0x7f && c <= 0x9f) || c == '\u00ad' || c == '\u070f'
                || c == '\u17b4' || c == '\u17b5' || c == '\ufeff' || (c >= '\u0600' && c <= '\u0604')
                || (c >= '\u200c' && c <= '\u200f') || (c >= '\u2028' && c <= '\u202f')
                || (c >= '\u2060' && c <= '\u206f') || (c >= '\ufff0' && c <= '\uffff');
    }

    /**
     * Turn a single unicode character into a 32-bit unicode hex literal.
     */
    private static String escapeCharAsUnicode(char toEscape) {
        String hexValue = Integer.toString(toEscape, 16);
        int padding = 4 - hexValue.length();
        return "\\u" + ("0000".substring(0, padding)) + hexValue;
    }
}

Related

  1. quote(String val)
  2. quote(String val)
  3. quote(String val, char quote)
  4. quote(String value)
  5. quote(String value)
  6. quote(String value)
  7. quote(StringBuilder buf, String str)
  8. quote(StringBuilder sb, String str)
  9. quote4CMDLine(String str)