Skip to content

JsonPrinter remove trailing garbage after non-printable char #950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,7 @@ private void appendEncodingAsString(
if (size > 1 && encoding.primitiveType() == CHAR)
{
doubleQuote();

for (int i = 0; i < size; i++)
{
escape((char)buffer.getByte(index + (i * elementSize)));
}

escapePrintableChar(buffer, index, size, elementSize);
doubleQuote();
}
else
Expand Down Expand Up @@ -369,6 +364,22 @@ private void appendEncodingAsString(
}
}

private void escapePrintableChar(final DirectBuffer buffer, final int index, final int size, final int elementSize)
{
for (int i = 0; i < size; i++)
{
final byte c = buffer.getByte(index + (i * elementSize));
if (c > 0)
{
escape((char)c);
}
else
{
break;
}
}
}

private void backup()
{
final int newLength = output.length() - 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package uk.co.real_logic.sbe.json;

import baseline.CarEncoder;
import baseline.CredentialsEncoder;
import baseline.MessageHeaderEncoder;
import org.agrona.concurrent.UnsafeBuffer;
Expand Down Expand Up @@ -155,6 +156,37 @@ public void exampleVarData() throws Exception
result);
}

@Test
public void removeTrailingGarbage() throws Exception
{
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY);
encodeSchema(encodedSchemaBuffer);

final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY);
final UnsafeBuffer buffer = new UnsafeBuffer(encodedMsgBuffer);
final CarEncoder encoder = new CarEncoder();
encoder.wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder());
encoder.vehicleCode("vc\0ノ�");
encodedMsgBuffer.position(encoder.encodedLength());
encodedSchemaBuffer.flip();
final Ir ir = decodeIr(encodedSchemaBuffer);

final JsonPrinter printer = new JsonPrinter(ir);
final String result = printer.print(encodedMsgBuffer);
assertEquals("{\n" + " \"serialNumber\": 0,\n" +
" \"modelYear\": 0,\n" +
" \"available\": \"F\",\n" + " \"code\": \"null\",\n" + " \"someNumbers\": [0, 0, 0, 0, 0],\n" +
" \"vehicleCode\": \"vc\",\n" + //trailing garbage removed
" \"extras\": { \"sunRoof\": false, \"sportsPack\": false, \"cruiseControl\": false },\n" +
" \"engine\": \n" + " {\n" + " \"capacity\": 0,\n" + " \"numCylinders\": 0,\n" +
" \"maxRpm\": 9000,\n" + " \"manufacturerCode\": \"\",\n" +
" \"fuel\": \"Petrol\"\n" + " },\n" + " \"uuid\": [0, 0],\n" + " \"cupHolderCount\": 0,\n" +
" \"fuelFigures\": [],\n" + " \"performanceFigures\": [],\n" + " \"manufacturer\": \"\",\n" +
" \"model\": \"\",\n" + " \"activationCode\": \"\"\n" + "}",
result);
}


private static void encodeSchema(final ByteBuffer buffer) throws Exception
{
final Path path = Paths.get("src/test/resources/json-printer-test-schema.xml");
Expand Down