Fixed MPString#toJson and added test
This commit is contained in:
parent
bc1ed4fe3c
commit
4505c5b22c
@ -147,7 +147,7 @@ public class MPString implements MPType<String>, CharSequence {
|
|||||||
if (c < ' ') {
|
if (c < ' ') {
|
||||||
result.append("\\u");
|
result.append("\\u");
|
||||||
String hex = Integer.toHexString(c);
|
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('0');
|
||||||
}
|
}
|
||||||
result.append(hex);
|
result.append(hex);
|
||||||
|
@ -156,6 +156,16 @@ public class ReaderTest {
|
|||||||
ensureStringIsEncodedAndDecodedCorrectly(65536);
|
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 {
|
private void ensureStringIsEncodedAndDecodedCorrectly(int length) throws Exception {
|
||||||
MPString value = new MPString(stringWithLength(length));
|
MPString value = new MPString(stringWithLength(length));
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
Loading…
Reference in New Issue
Block a user