From 4fc2823a07bb69b7310a981504dccae1f9a61777 Mon Sep 17 00:00:00 2001 From: Martin Thompson Date: Sun, 17 Jun 2018 15:32:21 +0100 Subject: [PATCH] [Java] Add composites support to JsonPrinter. Issue #560. --- .../sbe/json/JsonTokenListener.java | 35 +++++++++++++++---- .../real_logic/sbe/json/JsonPrinterTest.java | 17 +++++---- 2 files changed, 38 insertions(+), 14 deletions(-) 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 892b270388..af95d8d23b 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 @@ -31,6 +31,7 @@ public class JsonTokenListener implements TokenListener { private final StringBuilder output; private int indentation = 0; + private int compositeLevel = 0; public JsonTokenListener(final StringBuilder output) { @@ -54,7 +55,7 @@ public void onEncoding( final Token typeToken, final int actingVersion) { - property(fieldToken); + property(compositeLevel > 0 ? typeToken.name() : fieldToken.name()); appendEncodingAsString(buffer, bufferIndex, typeToken, actingVersion); next(); } @@ -90,7 +91,7 @@ public void onEnum( } } - property(fieldToken); + property(determineName(0, fieldToken, tokens, fromIndex)); doubleQuote(); output.append(value); doubleQuote(); @@ -109,7 +110,7 @@ public void onBitSet( final Token typeToken = tokens.get(fromIndex + 1); final long encodedValue = readEncodingAsLong(buffer, bufferIndex, typeToken, actingVersion); - property(fieldToken); + property(determineName(0, fieldToken, tokens, fromIndex)); output.append("{ "); for (int i = fromIndex + 1; i < toIndex; i++) @@ -134,16 +135,23 @@ public void onBitSet( public void onBeginComposite( final Token fieldToken, final List tokens, final int fromIndex, final int toIndex) { + ++compositeLevel; + + property(determineName(1, fieldToken, tokens, fromIndex)); + output.append('\n'); + startObject(); } public void onEndComposite( final Token fieldToken, final List tokens, final int fromIndex, final int toIndex) { + --compositeLevel; + endObject(); } public void onGroupHeader(final Token token, final int numInGroup) { - property(token); + property(token.name()); output.append("[\n"); } @@ -171,7 +179,7 @@ public void onVarData( { try { - property(fieldToken); + property(fieldToken.name()); doubleQuote(); final byte[] tempBuffer = new byte[length]; @@ -197,11 +205,11 @@ private void next() output.append(",\n"); } - private void property(final Token token) + private void property(final String name) { indent(); doubleQuote(); - output.append(token.name()); + output.append(name); output.append("\": "); } @@ -301,6 +309,19 @@ private void endObject() } } + private String determineName( + final int thresholdLevel, final Token fieldToken, final List tokens, final int fromIndex) + { + if (compositeLevel > thresholdLevel) + { + return tokens.get(fromIndex).name(); + } + else + { + return fieldToken.name(); + } + } + private static PrimitiveValue constOrNotPresentValue(final Token token, final int actingVersion) { final Encoding encoding = token.encoding(); 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 47e2e78506..a902852d9d 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 @@ -42,10 +42,10 @@ public class JsonPrinterTest extends EncodedCarTestBase @Test public void exampleMessagePrintedAsJson() throws Exception { - final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocateDirect(SCHEMA_BUFFER_CAPACITY); + final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY); encodeSchema(encodedSchemaBuffer); - final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY); + final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY); encodeTestMessage(encodedMsgBuffer); encodedSchemaBuffer.flip(); @@ -62,11 +62,14 @@ public void exampleMessagePrintedAsJson() throws Exception " \"someNumbers\": [0, 1, 2, 3, 4],\n" + " \"vehicleCode\": \"abcdef\",\n" + " \"extras\": { \"sunRoof\": false, \"sportsPack\": true, \"cruiseControl\": true },\n" + - " \"capacity\": 2000,\n" + - " \"numCylinders\": 4,\n" + - " \"maxRpm\": 9000,\n" + - " \"manufacturerCode\": \"123\",\n" + - " \"fuel\": \"Petrol\",\n" + + " \"engine\": \n" + + " {\n" + + " \"capacity\": 2000,\n" + + " \"numCylinders\": 4,\n" + + " \"maxRpm\": 9000,\n" + + " \"manufacturerCode\": \"123\",\n" + + " \"fuel\": \"Petrol\"\n" + + " },\n" + " \"fuelFigures\": [\n" + " {\n" + " \"speed\": 30,\n" +