Fixed MPString#toJson and added test

This commit is contained in:
Christian Basler 2017-02-01 08:13:47 +01:00
parent bc1ed4fe3c
commit 4505c5b22c
2 changed files with 11 additions and 1 deletions

View File

@ -147,7 +147,7 @@ public class MPString implements MPType<String>, CharSequence {
if (c < ' ') {
result.append("\\u");
String hex = Integer.toHexString(c);
for (int j = 0; j - hex.length() < 4; j++) {
for (int j = 0; j + hex.length() < 4; j++) {
result.append('0');
}
result.append(hex);

View File

@ -156,6 +156,16 @@ public class ReaderTest {
ensureStringIsEncodedAndDecodedCorrectly(65536);
}
@Test
public void ensureJsonStringsAreEscapedCorrectly() throws Exception {
StringBuilder builder = new StringBuilder();
for (char c = '\u0001'; c < ' '; c++) {
builder.append(c);
}
MPString string = new MPString(builder.toString());
assertThat(string.toJson(), is("\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\""));
}
private void ensureStringIsEncodedAndDecodedCorrectly(int length) throws Exception {
MPString value = new MPString(stringWithLength(length));
ByteArrayOutputStream out = new ByteArrayOutputStream();