@@ -317,18 +317,18 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
317317 var laterCode = ''
318318
319319 Object . keys ( schema . properties || { } ) . forEach ( ( key , i , a ) => {
320- // Using obj. key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
320+ // Using obj[' key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
321321 // see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
322322 code += `
323- if (obj. ${ key } !== undefined) {
323+ if (obj[' ${ key } '] !== undefined) {
324324 json += '${ $asString ( key ) } :'
325325 `
326326
327327 if ( schema . properties [ key ] [ '$ref' ] ) {
328328 schema . properties [ key ] = refFinder ( schema . properties [ key ] [ '$ref' ] , fullSchema , externalSchema )
329329 }
330330
331- const result = nested ( laterCode , name , '.' + key , schema . properties [ key ] , externalSchema , fullSchema )
331+ const result = nested ( laterCode , name , key , schema . properties [ key ] , externalSchema , fullSchema )
332332
333333 code += result . code
334334 laterCode = result . laterCode
@@ -406,6 +406,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
406406 var code = ''
407407 var funcName
408408 const type = schema . type
409+ const accessor = key . indexOf ( '[' ) === 0 ? key : `['${ key } ']`
409410 switch ( type ) {
410411 case 'null' :
411412 code += `
@@ -414,36 +415,36 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
414415 break
415416 case 'string' :
416417 code += `
417- json += $asString(obj${ key } )
418+ json += $asString(obj${ accessor } )
418419 `
419420 break
420421 case 'integer' :
421422 code += `
422- json += $asInteger(obj${ key } )
423+ json += $asInteger(obj${ accessor } )
423424 `
424425 break
425426 case 'number' :
426427 code += `
427- json += $asNumber(obj${ key } )
428+ json += $asNumber(obj${ accessor } )
428429 `
429430 break
430431 case 'boolean' :
431432 code += `
432- json += $asBoolean(obj${ key } )
433+ json += $asBoolean(obj${ accessor } )
433434 `
434435 break
435436 case 'object' :
436437 funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' ) // eslint-disable-line
437438 laterCode = buildObject ( schema , laterCode , funcName , externalSchema , fullSchema )
438439 code += `
439- json += ${ funcName } (obj${ key } )
440+ json += ${ funcName } (obj${ accessor } )
440441 `
441442 break
442443 case 'array' :
443444 funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' ) // eslint-disable-line
444445 laterCode = buildArray ( schema , laterCode , funcName , externalSchema , fullSchema )
445446 code += `
446- json += ${ funcName } (obj${ key } )
447+ json += ${ funcName } (obj${ accessor } )
447448 `
448449 break
449450 default :
0 commit comments