src/main/java/com/cedarsoftware/util/io/JsonWriter.java
public static void writeJsonUtf8String(String s, final Writer output) throws IOException
    {
        output.write('\"');
        final int len = s.length();

        for (int i = 0; i < len; i++)
        {
            char c = s.charAt(i);

            if (c < ' ')
            {    // Anything less than ASCII space, write either in \\u00xx form, or the special \t, \n, etc. form
                switch (c)
                {
                    case '\b':
                        output.write("\\b");
                        break;
                    case '\f':
                        output.write("\\f");
                        break;
                    case '\n':
                        output.write("\\n");
                        break;
                    case '\r':
                        output.write("\\r");
                        break;
                    case '\t':
                        output.write("\\t");
                        break;
                    default:
                        output.write(String.format("\\u%04X", (int)c));
                        break;
                }
            }
            else if (c == '\\' || c == '"')
            {
                output.write('\\');
                output.write(c);
            }
            else
            {   // Anything else - write in UTF-8 form (multi-byte encoded) (OutputStreamWriter is UTF-8)
                output.write(c);
            }
        }
        output.write('\"');
    }
public static void writeJsonUtf8String(String s, final Writer output) throws IOException
    {
        output.write('\"');
        final int len = s.length();

        for (int i = 0; i < len; i++)
        {
            char c = s.charAt(i);

            if (c < ' ')
            {    // Anything less than ASCII space, write either in \\u00xx form, or the special \t, \n, etc. form
                switch (c)
                {
                    case '\b':
                        output.write("\\b");
                        break;
                    case '\f':
                        output.write("\\f");
                        break;
                    case '\n':
                        output.write("\\n");
                        break;
                    case '\r':
                        output.write("\\r");
                        break;
                    case '\t':
                        output.write("\\t");
                        break;
                    default:
                        output.write(String.format("\\u%04X", (int)c));
                        break;
                }
            }
            else if (c == '\\' || c == '"')
            {
                output.write('\\');
                output.write(c);
            }
            else
            {   // Anything else - write in UTF-8 form (multi-byte encoded) (OutputStreamWriter is UTF-8)
                output.write(c);
            }
        }
        output.write('\"');
    }
public static void writeJsonUtf8String ( String s , final Writer output ) throws IOException { output . write ( ' ' ) ; final int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = s . charAt ( i ) ; if ( c < ' ' ) { // Anything less than ASCII space, write either in \\u00xx form, or the special \t, \n, etc. form switch ( c ) { case ' ' : output . write ( "\\b" ) ; break ; case ' ' : output . write ( "\\f" ) ; break ; case ' ' : output . write ( "\\n" ) ; break ; case ' ' : output . write ( "\\r" ) ; break ; case ' ' : output . write ( "\\t" ) ; break ; default : output . write ( String . format ( "\\u%04X" , ( int ) c ) ) ; break ; } } else if ( c == ' ' || c == ' ' ) { output . write ( ' ' ) ; output . write ( c ) ; } else { // Anything else - write in UTF-8 form (multi-byte encoded) (OutputStreamWriter is UTF-8) output . write ( c ) ; } } output . write ( ' ' ) ; }
Write out special characters "\b, \f, \t, \n, \r", as such, backslash as \\
quote as \" and values less than an ASCII space (20hex) as "\\u00xx" format,
characters in the range of ASCII space to a '~' as ASCII, and anything higher in UTF-8.

@param s String to be written in UTF-8 format on the output stream.
@param output Writer to which the UTF-8 string will be written to
@throws IOException if an error occurs writing to the output stream.
Write out special characters \ b \ f \ t \ n \ r as such backslash as \\ quote as \ and values less than an ASCII space ( 20hex ) as \\ u00xx format characters in the range of ASCII space to a ~ as ASCII and anything higher in UTF - 8 .