diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/json/JsonTokenListener.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/json/JsonTokenListener.java index 3337db04d2..a618463229 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/json/JsonTokenListener.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/json/JsonTokenListener.java @@ -16,6 +16,7 @@ package uk.co.real_logic.sbe.json; import org.agrona.DirectBuffer; +import org.agrona.PrintBufferUtil; import uk.co.real_logic.sbe.PrimitiveValue; import uk.co.real_logic.sbe.ir.Encoding; import uk.co.real_logic.sbe.ir.Token; @@ -230,7 +231,10 @@ public void onVarData( final byte[] tempBuffer = new byte[length]; buffer.getBytes(bufferIndex, tempBuffer, 0, length); - final String str = new String(tempBuffer, 0, length, typeToken.encoding().characterEncoding()); + + final String charsetName = typeToken.encoding().characterEncoding(); + final String str = charsetName != null ? new String(tempBuffer, 0, length, charsetName) : + PrintBufferUtil.hexDump(tempBuffer); escape(str); diff --git a/sbe-tool/src/test/java/uk/co/real_logic/sbe/json/JsonPrinterTest.java b/sbe-tool/src/test/java/uk/co/real_logic/sbe/json/JsonPrinterTest.java index 4d2c9e597f..1134876d22 100644 --- a/sbe-tool/src/test/java/uk/co/real_logic/sbe/json/JsonPrinterTest.java +++ b/sbe-tool/src/test/java/uk/co/real_logic/sbe/json/JsonPrinterTest.java @@ -15,6 +15,9 @@ */ package uk.co.real_logic.sbe.json; +import baseline.CredentialsEncoder; +import baseline.MessageHeaderEncoder; +import org.agrona.concurrent.UnsafeBuffer; import org.junit.jupiter.api.Test; import uk.co.real_logic.sbe.EncodedCarTestBase; import uk.co.real_logic.sbe.ir.Ir; @@ -125,6 +128,33 @@ void exampleMessagePrintedAsJson() throws Exception result); } + @Test + public void exampleVarData() 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 CredentialsEncoder encoder = new CredentialsEncoder(); + encoder.wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder()); + encoder.login("example"); + encoder.putEncryptedPassword(new byte[] {11, 0, 64, 97}, 0, 4); + 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" + + " \"login\": \"example\",\n" + + " \"encryptedPassword\": \"0b004061\"\n" + + "}", + result); + } + private static void encodeSchema(final ByteBuffer buffer) throws Exception { final Path path = Paths.get("src/test/resources/json-printer-test-schema.xml"); diff --git a/sbe-tool/src/test/resources/json-printer-test-schema.xml b/sbe-tool/src/test/resources/json-printer-test-schema.xml index 94044b19f7..da01a58fd1 100644 --- a/sbe-tool/src/test/resources/json-printer-test-schema.xml +++ b/sbe-tool/src/test/resources/json-printer-test-schema.xml @@ -23,6 +23,10 @@ + + + + @@ -77,4 +81,11 @@ + + + + + + +