Skip to content

Commit 75f85d0

Browse files
authored
[Java] Support binary vardata in json printer (#914)
1 parent 02c5b6a commit 75f85d0

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/json/JsonTokenListener.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package uk.co.real_logic.sbe.json;
1717

1818
import org.agrona.DirectBuffer;
19+
import org.agrona.PrintBufferUtil;
1920
import uk.co.real_logic.sbe.PrimitiveValue;
2021
import uk.co.real_logic.sbe.ir.Encoding;
2122
import uk.co.real_logic.sbe.ir.Token;
@@ -230,7 +231,10 @@ public void onVarData(
230231

231232
final byte[] tempBuffer = new byte[length];
232233
buffer.getBytes(bufferIndex, tempBuffer, 0, length);
233-
final String str = new String(tempBuffer, 0, length, typeToken.encoding().characterEncoding());
234+
235+
final String charsetName = typeToken.encoding().characterEncoding();
236+
final String str = charsetName != null ? new String(tempBuffer, 0, length, charsetName) :
237+
PrintBufferUtil.hexDump(tempBuffer);
234238

235239
escape(str);
236240

sbe-tool/src/test/java/uk/co/real_logic/sbe/json/JsonPrinterTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package uk.co.real_logic.sbe.json;
1717

18+
import baseline.CredentialsEncoder;
19+
import baseline.MessageHeaderEncoder;
20+
import org.agrona.concurrent.UnsafeBuffer;
1821
import org.junit.jupiter.api.Test;
1922
import uk.co.real_logic.sbe.EncodedCarTestBase;
2023
import uk.co.real_logic.sbe.ir.Ir;
@@ -125,6 +128,33 @@ void exampleMessagePrintedAsJson() throws Exception
125128
result);
126129
}
127130

131+
@Test
132+
public void exampleVarData() throws Exception
133+
{
134+
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY);
135+
encodeSchema(encodedSchemaBuffer);
136+
137+
final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY);
138+
final UnsafeBuffer buffer = new UnsafeBuffer(encodedMsgBuffer);
139+
final CredentialsEncoder encoder = new CredentialsEncoder();
140+
encoder.wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder());
141+
encoder.login("example");
142+
encoder.putEncryptedPassword(new byte[] {11, 0, 64, 97}, 0, 4);
143+
encodedMsgBuffer.position(encoder.encodedLength());
144+
145+
encodedSchemaBuffer.flip();
146+
final Ir ir = decodeIr(encodedSchemaBuffer);
147+
148+
final JsonPrinter printer = new JsonPrinter(ir);
149+
final String result = printer.print(encodedMsgBuffer);
150+
assertEquals(
151+
"{\n" +
152+
" \"login\": \"example\",\n" +
153+
" \"encryptedPassword\": \"0b004061\"\n" +
154+
"}",
155+
result);
156+
}
157+
128158
private static void encodeSchema(final ByteBuffer buffer) throws Exception
129159
{
130160
final Path path = Paths.get("src/test/resources/json-printer-test-schema.xml");

sbe-tool/src/test/resources/json-printer-test-schema.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
</composite>
2424
<type name="uuid_t" primitiveType="int64" length="2"/>
2525
<type name="cupHolderCount_t" primitiveType="uint8"/>
26+
<composite name="varDataEncoding">
27+
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
28+
<type name="varData" primitiveType="uint8" length="0"/>
29+
</composite>
2630
</types>
2731
<types>
2832
<type name="ModelYear" primitiveType="uint16"/>
@@ -77,4 +81,11 @@
7781
<data name="model" id="18" type="varStringEncoding"/>
7882
<data name="activationCode" id="19" type="varStringEncoding"/>
7983
</sbe:message>
84+
85+
<sbe:message name="Credentials" id="2">
86+
<data name="login" id="1" type="varStringEncoding"/>
87+
<!-- variable length encoding with embedded length in composite -->
88+
<data id="2" name="encryptedPassword" type="varDataEncoding"/>
89+
</sbe:message>
90+
8091
</sbe:messageSchema>

0 commit comments

Comments
 (0)