Skip to content

Commit

Permalink
[Java] Add composites support to JsonPrinter. Issue #560.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Jun 17, 2018
1 parent 3f63562 commit 4fc2823
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
}
Expand Down Expand Up @@ -90,7 +91,7 @@ public void onEnum(
}
}

property(fieldToken);
property(determineName(0, fieldToken, tokens, fromIndex));
doubleQuote();
output.append(value);
doubleQuote();
Expand All @@ -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++)
Expand All @@ -134,16 +135,23 @@ public void onBitSet(
public void onBeginComposite(
final Token fieldToken, final List<Token> 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<Token> 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");
}

Expand Down Expand Up @@ -171,7 +179,7 @@ public void onVarData(
{
try
{
property(fieldToken);
property(fieldToken.name());
doubleQuote();

final byte[] tempBuffer = new byte[length];
Expand All @@ -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("\": ");
}

Expand Down Expand Up @@ -301,6 +309,19 @@ private void endObject()
}
}

private String determineName(
final int thresholdLevel, final Token fieldToken, final List<Token> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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" +
Expand Down

0 comments on commit 4fc2823

Please sign in to comment.