Skip to content

Commit 4fc2823

Browse files
committed
[Java] Add composites support to JsonPrinter. Issue #560.
1 parent 3f63562 commit 4fc2823

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class JsonTokenListener implements TokenListener
3131
{
3232
private final StringBuilder output;
3333
private int indentation = 0;
34+
private int compositeLevel = 0;
3435

3536
public JsonTokenListener(final StringBuilder output)
3637
{
@@ -54,7 +55,7 @@ public void onEncoding(
5455
final Token typeToken,
5556
final int actingVersion)
5657
{
57-
property(fieldToken);
58+
property(compositeLevel > 0 ? typeToken.name() : fieldToken.name());
5859
appendEncodingAsString(buffer, bufferIndex, typeToken, actingVersion);
5960
next();
6061
}
@@ -90,7 +91,7 @@ public void onEnum(
9091
}
9192
}
9293

93-
property(fieldToken);
94+
property(determineName(0, fieldToken, tokens, fromIndex));
9495
doubleQuote();
9596
output.append(value);
9697
doubleQuote();
@@ -109,7 +110,7 @@ public void onBitSet(
109110
final Token typeToken = tokens.get(fromIndex + 1);
110111
final long encodedValue = readEncodingAsLong(buffer, bufferIndex, typeToken, actingVersion);
111112

112-
property(fieldToken);
113+
property(determineName(0, fieldToken, tokens, fromIndex));
113114

114115
output.append("{ ");
115116
for (int i = fromIndex + 1; i < toIndex; i++)
@@ -134,16 +135,23 @@ public void onBitSet(
134135
public void onBeginComposite(
135136
final Token fieldToken, final List<Token> tokens, final int fromIndex, final int toIndex)
136137
{
138+
++compositeLevel;
139+
140+
property(determineName(1, fieldToken, tokens, fromIndex));
141+
output.append('\n');
142+
startObject();
137143
}
138144

139145
public void onEndComposite(
140146
final Token fieldToken, final List<Token> tokens, final int fromIndex, final int toIndex)
141147
{
148+
--compositeLevel;
149+
endObject();
142150
}
143151

144152
public void onGroupHeader(final Token token, final int numInGroup)
145153
{
146-
property(token);
154+
property(token.name());
147155
output.append("[\n");
148156
}
149157

@@ -171,7 +179,7 @@ public void onVarData(
171179
{
172180
try
173181
{
174-
property(fieldToken);
182+
property(fieldToken.name());
175183
doubleQuote();
176184

177185
final byte[] tempBuffer = new byte[length];
@@ -197,11 +205,11 @@ private void next()
197205
output.append(",\n");
198206
}
199207

200-
private void property(final Token token)
208+
private void property(final String name)
201209
{
202210
indent();
203211
doubleQuote();
204-
output.append(token.name());
212+
output.append(name);
205213
output.append("\": ");
206214
}
207215

@@ -301,6 +309,19 @@ private void endObject()
301309
}
302310
}
303311

312+
private String determineName(
313+
final int thresholdLevel, final Token fieldToken, final List<Token> tokens, final int fromIndex)
314+
{
315+
if (compositeLevel > thresholdLevel)
316+
{
317+
return tokens.get(fromIndex).name();
318+
}
319+
else
320+
{
321+
return fieldToken.name();
322+
}
323+
}
324+
304325
private static PrimitiveValue constOrNotPresentValue(final Token token, final int actingVersion)
305326
{
306327
final Encoding encoding = token.encoding();

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public class JsonPrinterTest extends EncodedCarTestBase
4242
@Test
4343
public void exampleMessagePrintedAsJson() throws Exception
4444
{
45-
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocateDirect(SCHEMA_BUFFER_CAPACITY);
45+
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY);
4646
encodeSchema(encodedSchemaBuffer);
4747

48-
final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY);
48+
final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY);
4949
encodeTestMessage(encodedMsgBuffer);
5050

5151
encodedSchemaBuffer.flip();
@@ -62,11 +62,14 @@ public void exampleMessagePrintedAsJson() throws Exception
6262
" \"someNumbers\": [0, 1, 2, 3, 4],\n" +
6363
" \"vehicleCode\": \"abcdef\",\n" +
6464
" \"extras\": { \"sunRoof\": false, \"sportsPack\": true, \"cruiseControl\": true },\n" +
65-
" \"capacity\": 2000,\n" +
66-
" \"numCylinders\": 4,\n" +
67-
" \"maxRpm\": 9000,\n" +
68-
" \"manufacturerCode\": \"123\",\n" +
69-
" \"fuel\": \"Petrol\",\n" +
65+
" \"engine\": \n" +
66+
" {\n" +
67+
" \"capacity\": 2000,\n" +
68+
" \"numCylinders\": 4,\n" +
69+
" \"maxRpm\": 9000,\n" +
70+
" \"manufacturerCode\": \"123\",\n" +
71+
" \"fuel\": \"Petrol\"\n" +
72+
" },\n" +
7073
" \"fuelFigures\": [\n" +
7174
" {\n" +
7275
" \"speed\": 30,\n" +

0 commit comments

Comments
 (0)