private void writeQuotedAndEscaped ( CharSequence string ) { if ( string != null && string . length ( ) != 0 ) { int len = string . length ( ) ; writer . write ( ' ' ) ; for ( int i = 0 ; i < len ; ++ i ) { char cp = string . charAt ( i ) ; if ( ( cp < 0x7f && cp >= 0x20 && cp != ' ' && cp != ' ' ) || ( cp > 0x7f && isConsolePrintable ( cp ) && ! isSurrogate ( cp ) ) ) { // quick bypass for direct printable chars. writer . write ( cp ) ; } else { switch ( cp ) { case ' ' : writer . write ( "\\b" ) ; break ; case ' ' : writer . write ( "\\t" ) ; break ; case ' ' : writer . write ( "\\n" ) ; break ; case ' ' : writer . write ( "\\f" ) ; break ; case ' ' : writer . write ( "\\r" ) ; break ; case ' ' : case ' ' : writer . write ( ' ' ) ; writer . write ( cp ) ; break ; default : if ( isSurrogate ( cp ) && ( i + 1 ) < len ) { char c2 = string . charAt ( i + 1 ) ; writer . format ( "\\u%04x" , ( int ) cp ) ; writer . format ( "\\u%04x" , ( int ) c2 ) ; ++ i ; } else { writer . format ( "\\u%04x" , ( int ) cp ) ; } break ; } } } writer . write ( ' ' ) ; } else { writer . write ( "\"\"" ) ; } }
Ported from org . json JSONObject . quote and modified for local use .