@@ -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 ();
0 commit comments