All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.server.jsontuils.CrunchifyEscapeCharUtility Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.server.jsontuils;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.StringCharacterIterator;

/**
 * @author Crunchify.com Version: 1.0 Updated: 01.25.2016
 * 
 */

public class CrunchifyEscapeCharUtility {

    // Used to ensure that HTTP query strings are in proper form, by escaping special characters such as spaces.
    // Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme
    public static String crunchifyURLEscapeUtil(String crunchifyURL) {
        String crunchifyNewURL = null;
        try {
            crunchifyNewURL = URLEncoder.encode(crunchifyURL, StandardCharsets.UTF_8);
        }
        catch (Exception ex) {
            throw new RuntimeException("UTF-8 not supported", ex);
        }
        return crunchifyNewURL;
    }

    // JSON Escape Utility
    public static String crunchifyJSONEscapeUtil(String crunchifyJSON) {
        final StringBuilder crunchifyNewJSON = new StringBuilder();

        // StringCharacterIterator class iterates over the entire String
        StringCharacterIterator iterator = new StringCharacterIterator(crunchifyJSON);
        char myChar = iterator.current();

        // DONE = \\uffff (not a character)
        while (myChar != StringCharacterIterator.DONE) {
            if (myChar == '\"') {
                crunchifyNewJSON.append("\\\"");
            }
            else if (myChar == '\t') {
                crunchifyNewJSON.append("\\t");
            }
            else if (myChar == '\f') {
                crunchifyNewJSON.append("\\f");
            }
            else if (myChar == '\n') {
                crunchifyNewJSON.append("\\n");
            }
            else if (myChar == '\r') {
                crunchifyNewJSON.append("\\r");
            }
            else if (myChar == '\\') {
                crunchifyNewJSON.append("\\\\");
            }
            else if (myChar == '/') {
                crunchifyNewJSON.append("\\/");
            }
            else if (myChar == '\b') {
                crunchifyNewJSON.append("\\b");
            }
            else {

                // nothing matched - just as text as it is.
                crunchifyNewJSON.append(myChar);
            }
            myChar = iterator.next();
        }
        return crunchifyNewJSON.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy