Java String Quote quote(String string, StringBuilder w)

Here you can find the source of quote(String string, StringBuilder w)

Description

Produce a string in double quotes with backslash sequences in all the right places.

License

Apache License

Parameter

Parameter Description
string A String

Return

A String correctly formatted for insertion in a JSON text.

Declaration

public static void quote(String string, StringBuilder w) 

Method Source Code

//package com.java2s;
/**//from w w w.  j a  v  a2s.c  o m
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * 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 {
    /**
     * Produce a string in double quotes with backslash sequences in all the
     * right places. A backslash will be inserted within </, producing <\/,
     * allowing JSON text to be delivered in HTML. In JSON text, a string
     * cannot contain a control character or an unescaped quote or backslash.
     * @param string A String
     * @return  A String correctly formatted for insertion in a JSON text.
     */
    public static void quote(String string, StringBuilder w) {
        if (string == null || string.length() == 0) {
            w.append("\"\"");
            return;
        }

        char b;
        char c = 0;
        String hhhh;
        int i;
        int len = string.length();

        w.append('"');
        for (i = 0; i < len; i += 1) {
            b = c;
            c = string.charAt(i);
            switch (c) {
            case '\\':
            case '"':
                w.append('\\');
                w.append(c);
                break;
            case '/':
                if (b == '<') {
                    w.append('\\');
                }
                w.append(c);
                break;
            case '\b':
                w.append("\\b");
                break;
            case '\t':
                w.append("\\t");
                break;
            case '\n':
                w.append("\\n");
                break;
            case '\f':
                w.append("\\f");
                break;
            case '\r':
                w.append("\\r");
                break;
            default:
                if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
                        || (c >= '\u2000' && c < '\u2100')) {
                    hhhh = "000" + Integer.toHexString(c);
                    w.append("\\u" + hhhh.substring(hhhh.length() - 4));
                } else {
                    w.append(c);
                }
            }
        }
        w.append('"');
    }
}

Related

  1. quote(String string)
  2. quote(String string)
  3. quote(String string)
  4. quote(String string, boolean escapeHtml)
  5. quote(String string, String charsToQuote)
  6. quote(String tableName)
  7. quote(String text)
  8. quote(String text)
  9. quote(String text)