@@ -120,7 +120,7 @@ class JsonStreamStringify extends Readable {
120120 return value ;
121121 }
122122
123- addToStack ( value , key , index ) {
123+ addToStack ( value , key , index , parent ) {
124124 let realValue = value ;
125125 if ( this . replacerFunction ) {
126126 realValue = this . replacerFunction ( key || index , realValue , this ) ;
@@ -138,6 +138,13 @@ class JsonStreamStringify extends Readable {
138138 }
139139 }
140140 let type = getType ( realValue ) ;
141+ if ( ( ( parent && parent . type === 'Array' ) ? true : realValue !== undefined ) && type !== 'Promise' ) {
142+ if ( parent && ! parent . first ) {
143+ this . _push ( ',' ) ;
144+ }
145+ /* eslint-disable-next-line no-param-reassign */
146+ if ( parent ) parent . first = false ;
147+ }
141148 if ( realValue !== undefined && type !== 'Promise' && key ) {
142149 if ( this . gap ) {
143150 this . _push ( `\n${ this . gap . repeat ( this . depth ) } "${ escapeString ( key ) } ": ` ) ;
@@ -172,6 +179,8 @@ class JsonStreamStringify extends Readable {
172179 index,
173180 type,
174181 value : realValue ,
182+ parent,
183+ first : true ,
175184 } ;
176185
177186 if ( type === 'Object' ) {
@@ -199,7 +208,6 @@ class JsonStreamStringify extends Readable {
199208 this . error = true ;
200209 this . emit ( 'error' , err ) ;
201210 } ) ;
202- obj . first = true ;
203211 }
204212 this . stack . unshift ( obj ) ;
205213 return obj ;
@@ -222,10 +230,8 @@ class JsonStreamStringify extends Readable {
222230 const end = stackItemEnd [ type ] ;
223231 if ( isObject && ! item . isEmpty && this . gap ) this . _push ( `\n${ this . gap . repeat ( this . depth ) } ` ) ;
224232 if ( end ) this . _push ( end ) ;
225- if ( item . addSeparatorAfterEnd ) {
226- this . _push ( ',' ) ;
227- }
228- this . stack . splice ( this . stack . indexOf ( item ) , 1 ) ;
233+ const stackIndex = this . stack . indexOf ( item ) ;
234+ this . stack . splice ( stackIndex , 1 ) ;
229235 }
230236
231237 _push ( data ) {
@@ -254,14 +260,15 @@ class JsonStreamStringify extends Readable {
254260 }
255261
256262 processObject ( current ) {
263+ // when no keys left, remove obj from stack
257264 if ( ! current . unread . length ) {
258265 this . removeFromStack ( current ) ;
259266 return ;
260267 }
261268 const key = current . unread . shift ( ) ;
262269 const value = current . value [ key ] ;
263270
264- this . addToStack ( value , current . type === 'Object' && key , current . type === 'Array' && key ) . addSeparatorAfterEnd = ! ! current . unread . length ;
271+ this . addToStack ( value , current . type === 'Object' && key , current . type === 'Array' && key , current ) ;
265272 }
266273
267274 processArray ( current ) {
@@ -319,14 +326,8 @@ class JsonStreamStringify extends Readable {
319326
320327 processPromise ( current ) {
321328 return recursiveResolve ( current . value ) . then ( ( value ) => {
322- const {
323- addSeparatorAfterEnd,
324- } = current ;
325- /* eslint-disable-next-line no-param-reassign */
326- current . addSeparatorAfterEnd = false ;
327329 this . removeFromStack ( current ) ;
328- this . addToStack ( value , current . key , current . index )
329- . addSeparatorAfterEnd = addSeparatorAfterEnd ;
330+ this . addToStack ( value , current . key , current . index , current . parent ) ;
330331 } ) ;
331332 }
332333
0 commit comments