Skip to content

Commit 5f4d953

Browse files
committed
[Java] Quote character constants in JSON output and deal with any size string.
1 parent 2e13c06 commit 5f4d953

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
public class JsonTokenListener implements TokenListener
3131
{
32-
private final byte[] tempBuffer = new byte[1024];
3332
private final StringBuilder output;
3433
private int indentation = 0;
3534

@@ -125,6 +124,7 @@ public void onVarData(
125124
property(fieldToken);
126125
doubleQuote();
127126

127+
final byte[] tempBuffer = new byte[length];
128128
buffer.getBytes(bufferIndex, tempBuffer, 0, length);
129129
output.append(new String(tempBuffer, 0, length, typeToken.encoding().characterEncoding()));
130130

@@ -158,14 +158,25 @@ private void property(final Token token)
158158
private void appendEncodingAsString(
159159
final DirectBuffer buffer, final int index, final Token typeToken, final int actingVersion)
160160
{
161+
final Encoding encoding = typeToken.encoding();
161162
final PrimitiveValue constOrNotPresentValue = constOrNotPresentValue(typeToken, actingVersion);
162163
if (null != constOrNotPresentValue)
163164
{
165+
if (encoding.primitiveType() == CHAR)
166+
{
167+
doubleQuote();
168+
}
169+
164170
output.append(constOrNotPresentValue.toString());
171+
172+
if (encoding.primitiveType() == CHAR)
173+
{
174+
doubleQuote();
175+
}
176+
165177
return;
166178
}
167179

168-
final Encoding encoding = typeToken.encoding();
169180
final int elementSize = encoding.primitiveType().size();
170181

171182
final int size = typeToken.arrayLength();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void exampleMessagePrintedAsJson() throws Exception
6363
" \"numCylinders\": 4,\n" +
6464
" \"maxRpm\": 9000,\n" +
6565
" \"manufacturerCode\": \"123\",\n" +
66-
" \"fuel\": Petrol,\n" +
66+
" \"fuel\": \"Petrol\",\n" +
6767
" \"fuelFigures\": [\n" +
6868
" {\n" +
6969
" \"speed\": 30,\n" +

0 commit comments

Comments
 (0)