@Test public void escapeStringShouldNotSwallowCharacters() { assertEquals("a\\nb\\\\nc", new String(JsonWriter.escapeString("a\nb\\nc"))); }
public final static char[] escapeString(final String value) { StringBuilder sb = new StringBuilder(); int last = 0; final int length = value.length(); for (int i = 0; i < length; i++) { char c = value.charAt(i); char[] replacement; if (c < 128) { replacement = REPLACEMENT_CHARS[c]; if (replacement == null) { continue; } } else if (c == '\u2028') { replacement = "\\u2028".toCharArray(); } else if (c == '\u2029') { replacement = "\\u2029".toCharArray(); } else { continue; } if (last < i) { sb.append(value, last, i); } sb.append(replacement, 0, replacement.length); last = i + 1; } if (last < length) { sb.append(value, last, length); } return sb.toString().toCharArray(); }
JsonWriter implements ObjectWriter { public final static char[] escapeString(final String value) { StringBuilder sb = new StringBuilder(); int last = 0; final int length = value.length(); for (int i = 0; i < length; i++) { char c = value.charAt(i); char[] replacement; if (c < 128) { replacement = REPLACEMENT_CHARS[c]; if (replacement == null) { continue; } } else if (c == '\u2028') { replacement = "\\u2028".toCharArray(); } else if (c == '\u2029') { replacement = "\\u2029".toCharArray(); } else { continue; } if (last < i) { sb.append(value, last, i); } sb.append(replacement, 0, replacement.length); last = i + 1; } if (last < length) { sb.append(value, last, length); } return sb.toString().toCharArray(); } }
JsonWriter implements ObjectWriter { public final static char[] escapeString(final String value) { StringBuilder sb = new StringBuilder(); int last = 0; final int length = value.length(); for (int i = 0; i < length; i++) { char c = value.charAt(i); char[] replacement; if (c < 128) { replacement = REPLACEMENT_CHARS[c]; if (replacement == null) { continue; } } else if (c == '\u2028') { replacement = "\\u2028".toCharArray(); } else if (c == '\u2029') { replacement = "\\u2029".toCharArray(); } else { continue; } if (last < i) { sb.append(value, last, i); } sb.append(replacement, 0, replacement.length); last = i + 1; } if (last < length) { sb.append(value, last, length); } return sb.toString().toCharArray(); } JsonWriter(Writer writer);  JsonWriter(Writer writer, final boolean skipNull, final boolean htmlSafe,
                    boolean indentation); }
JsonWriter implements ObjectWriter { public final static char[] escapeString(final String value) { StringBuilder sb = new StringBuilder(); int last = 0; final int length = value.length(); for (int i = 0; i < length; i++) { char c = value.charAt(i); char[] replacement; if (c < 128) { replacement = REPLACEMENT_CHARS[c]; if (replacement == null) { continue; } } else if (c == '\u2028') { replacement = "\\u2028".toCharArray(); } else if (c == '\u2029') { replacement = "\\u2029".toCharArray(); } else { continue; } if (last < i) { sb.append(value, last, i); } sb.append(replacement, 0, replacement.length); last = i + 1; } if (last < length) { sb.append(value, last, length); } return sb.toString().toCharArray(); } JsonWriter(Writer writer);  JsonWriter(Writer writer, final boolean skipNull, final boolean htmlSafe,
                    boolean indentation); JsonType enclosingType(); void close(); void flush(); JsonWriter beginArray(); JsonWriter beginObject(); JsonWriter endArray(); JsonWriter endObject(); JsonWriter writeName(final String name); ObjectWriter writeEscapedName(char[] name); JsonWriter writeValue(int value); JsonWriter writeValue(final double value); JsonWriter writeValue(long value); ObjectWriter writeValue(short value); ObjectWriter writeValue(float value); JsonWriter writeValue(final boolean value); JsonWriter writeValue(final Number value); ObjectWriter writeBoolean(final Boolean value); ObjectWriter writeNumber(final Number value); ObjectWriter writeString(String value); ObjectWriter writeBytes(byte[] value); ObjectWriter writeValue(byte[] value); JsonWriter writeUnsafeValue(final String value); JsonWriter writeValue(final String value); final static char[] escapeString(final String value); ObjectWriter writeNull(); ObjectWriter beginNextObjectMetadata(); ObjectWriter writeMetadata(String name, String value); ObjectWriter writeBoolean(String name, Boolean value); ObjectWriter writeNumber(String name, Number value); ObjectWriter writeString(String name, String value); ObjectWriter writeBytes(String name, byte[] value); Writer unwrap(); }
JsonWriter implements ObjectWriter { public final static char[] escapeString(final String value) { StringBuilder sb = new StringBuilder(); int last = 0; final int length = value.length(); for (int i = 0; i < length; i++) { char c = value.charAt(i); char[] replacement; if (c < 128) { replacement = REPLACEMENT_CHARS[c]; if (replacement == null) { continue; } } else if (c == '\u2028') { replacement = "\\u2028".toCharArray(); } else if (c == '\u2029') { replacement = "\\u2029".toCharArray(); } else { continue; } if (last < i) { sb.append(value, last, i); } sb.append(replacement, 0, replacement.length); last = i + 1; } if (last < length) { sb.append(value, last, length); } return sb.toString().toCharArray(); } JsonWriter(Writer writer);  JsonWriter(Writer writer, final boolean skipNull, final boolean htmlSafe,
                    boolean indentation); JsonType enclosingType(); void close(); void flush(); JsonWriter beginArray(); JsonWriter beginObject(); JsonWriter endArray(); JsonWriter endObject(); JsonWriter writeName(final String name); ObjectWriter writeEscapedName(char[] name); JsonWriter writeValue(int value); JsonWriter writeValue(final double value); JsonWriter writeValue(long value); ObjectWriter writeValue(short value); ObjectWriter writeValue(float value); JsonWriter writeValue(final boolean value); JsonWriter writeValue(final Number value); ObjectWriter writeBoolean(final Boolean value); ObjectWriter writeNumber(final Number value); ObjectWriter writeString(String value); ObjectWriter writeBytes(byte[] value); ObjectWriter writeValue(byte[] value); JsonWriter writeUnsafeValue(final String value); JsonWriter writeValue(final String value); final static char[] escapeString(final String value); ObjectWriter writeNull(); ObjectWriter beginNextObjectMetadata(); ObjectWriter writeMetadata(String name, String value); ObjectWriter writeBoolean(String name, Boolean value); ObjectWriter writeNumber(String name, Number value); ObjectWriter writeString(String name, String value); ObjectWriter writeBytes(String name, byte[] value); Writer unwrap();  }