Skip to content

Commit

Permalink
JsonPrinter remove trailing garbage after non-printable char (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywhrh committed Jul 19, 2023
1 parent b0bfc35 commit 4454c85
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
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

0 comments on commit 4454c85

Please sign in to comment.