Skip to content

Commit 28cf8d6

Browse files
committed
[Java] Add enum support to JsonPrinter. Issue #560.
1 parent 78854c8 commit 28cf8d6

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

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

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ public void onEnum(
6868
final int toIndex,
6969
final int actingVersion)
7070
{
71+
final Token typeToken = tokens.get(fromIndex + 1);
72+
final long encodedValue = readEncodingAsLong(buffer, bufferIndex, typeToken, actingVersion);
73+
74+
String value = null;
75+
if (fieldToken.isConstantEncoding())
76+
{
77+
final String refValue = fieldToken.encoding().constValue().toString();
78+
final int indexOfDot = refValue.indexOf('.');
79+
value = -1 == indexOfDot ? refValue : refValue.substring(indexOfDot + 1);
80+
}
81+
else
82+
{
83+
for (int i = fromIndex + 1; i < toIndex; i++)
84+
{
85+
if (encodedValue == tokens.get(i).encoding().constValue().longValue())
86+
{
87+
value = tokens.get(i).name();
88+
break;
89+
}
90+
}
91+
}
92+
93+
property(fieldToken);
94+
doubleQuote();
95+
output.append(value);
96+
doubleQuote();
97+
next();
7198
}
7299

73100
public void onBitSet(
@@ -217,21 +244,6 @@ private void backup()
217244
output.setLength(output.length() - 2);
218245
}
219246

220-
private static PrimitiveValue constOrNotPresentValue(final Token token, final int actingVersion)
221-
{
222-
final Encoding encoding = token.encoding();
223-
if (token.isConstantEncoding())
224-
{
225-
return encoding.constValue();
226-
}
227-
else if (token.isOptionalEncoding() && actingVersion < token.version())
228-
{
229-
return encoding.applicableNullValue();
230-
}
231-
232-
return null;
233-
}
234-
235247
private void indent()
236248
{
237249
for (int i = 0; i < indentation; i++)
@@ -265,4 +277,31 @@ private void endObject()
265277
next();
266278
}
267279
}
280+
281+
private static PrimitiveValue constOrNotPresentValue(final Token token, final int actingVersion)
282+
{
283+
final Encoding encoding = token.encoding();
284+
if (token.isConstantEncoding())
285+
{
286+
return encoding.constValue();
287+
}
288+
else if (token.isOptionalEncoding() && actingVersion < token.version())
289+
{
290+
return encoding.applicableNullValue();
291+
}
292+
293+
return null;
294+
}
295+
296+
private static long readEncodingAsLong(
297+
final DirectBuffer buffer, final int bufferIndex, final Token typeToken, final int actingVersion)
298+
{
299+
final PrimitiveValue constOrNotPresentValue = constOrNotPresentValue(typeToken, actingVersion);
300+
if (null != constOrNotPresentValue)
301+
{
302+
return constOrNotPresentValue.longValue();
303+
}
304+
305+
return Types.getLong(buffer, bufferIndex, typeToken.encoding());
306+
}
268307
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public void exampleMessagePrintedAsJson() throws Exception
5757
"{\n" +
5858
" \"serialNumber\": 1234,\n" +
5959
" \"modelYear\": 2013,\n" +
60+
" \"available\": \"T\",\n" +
61+
" \"code\": \"A\",\n" +
6062
" \"someNumbers\": [0, 1, 2, 3, 4],\n" +
6163
" \"vehicleCode\": \"abcdef\",\n" +
6264
" \"capacity\": 2000,\n" +

0 commit comments

Comments
 (0)