Skip to content
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

[Java] Support binary vardata in json printer #914

Merged
merged 1 commit into from
Aug 19, 2022
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 @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions sbe-tool/src/test/resources/json-printer-test-schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
</composite>
<type name="uuid_t" primitiveType="int64" length="2"/>
<type name="cupHolderCount_t" primitiveType="uint8"/>
<composite name="varDataEncoding">
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
<type name="varData" primitiveType="uint8" length="0"/>
</composite>
</types>
<types>
<type name="ModelYear" primitiveType="uint16"/>
Expand Down Expand Up @@ -77,4 +81,11 @@
<data name="model" id="18" type="varStringEncoding"/>
<data name="activationCode" id="19" type="varStringEncoding"/>
</sbe:message>

<sbe:message name="Credentials" id="2">
<data name="login" id="1" type="varStringEncoding"/>
<!-- variable length encoding with embedded length in composite -->
<data id="2" name="encryptedPassword" type="varDataEncoding"/>
</sbe:message>

</sbe:messageSchema>