Java Json escape(String text)

Here you can find the source of escape(String text)

Description

Escape a string.

License

Apache License

Parameter

Parameter Description
text the text

Return

the escaped string

Declaration

public static String escape(String text) 

Method Source Code

//package com.java2s;
/* See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Esri Inc. licenses this file to You 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./*from  ww  w .  j  a va 2 s  .c om*/
 */

public class Main {
    /**
     * Escape a string.
     * @param text the text
     * @return the escaped string
     */
    public static String escape(String text) {
        // TODO: is there a javax.json method to do this?
        if (text == null)
            return null;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            switch (ch) {
            case '"':
                sb.append("\\\"");
                break;
            case '\\':
                sb.append("\\\\");
                break;
            case '\b':
                sb.append("\\b");
                break;
            case '\f':
                sb.append("\\f");
                break;
            case '\n':
                sb.append("\\n");
                break;
            case '\r':
                sb.append("\\r");
                break;
            case '\t':
                sb.append("\\t");
                break;
            /*
            // TODO: is this required?
            case '/':
            sb.append("\\/");
            break;
             */
            default:
                if ((ch >= '\u0000') && (ch <= '\u001F')) {
                    String s2 = Integer.toHexString(ch);
                    sb.append("\\u");
                    for (int k = 0; k < 4 - s2.length(); k++) {
                        sb.append('0');
                    }
                    sb.append(s2.toUpperCase());
                } else {
                    sb.append(ch);
                }
            }
        }
        return sb.toString();
    }

    /**
     * Escape a string.
     * @param text the text
     * @param quote if true, quote the text
     * @return the escaped string
     */
    public static String escape(String text, boolean quote) {
        if (text == null)
            return null;
        if (quote) {
            return "\"" + escape(text) + "\"";
        } else {
            return escape(text);
        }
    }
}

Related

  1. addAll(JsonObjectBuilder a, JsonObjectBuilder b)
  2. asJsonObject(JsonValue value)
  3. asString(JsonValue value)
  4. compare(JsonValue expected, JsonValue actual, boolean strict)
  5. convertJsonValue(Object jsonValue, Class desiredType)
  6. fill_dictionary(Map dictionary, JsonObject jsonObject)
  7. fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)
  8. fill_list(JsonObjectBuilder jsonObject, String key, List list)
  9. fromDictionary(Map dictionary)

  10. HOME | Copyright © www.java2s.com 2016