code: public static String encodeBackslash(CharSequence s, Legal legal) {
  	StringBuilder b=new StringBuilder((int)(s.length()*1.5));
  	for(int i=0;i<s.length();i++) {
  		if(legal.isLegal(s.charAt(i))) {
  			b.append(s.charAt(i));
  		} else {
  			if(charToBackslash.containsKey(s.charAt(i))) {
  				b.append(charToBackslash.get(s.charAt(i)));
  				continue;
  			}
  			b.append("\\u");
  		    String hex = Integer.toHexString(s.charAt(i));
  		    for(int j=0;j<4-hex.length();j++)
  		      b.append('0');
  		    b.append(hex);
  		}
  	}
  	return(b.toString());
    }
docs: Encodes with backslash all illegal characters