@@ -314,12 +314,18 @@ function additionalProperty (schema, externalSchema, fullSchema) {
314314 } else if ( type === 'integer' ) {
315315 code += `
316316 ${ addComma }
317- json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
317+ var t = Number(obj[keys[i]])
318+ if (isLong && isLong(obj[keys[i]]) || !isNaN(t)) {
319+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
320+ }
318321 `
319322 } else if ( type === 'number' ) {
320323 code += `
321- ${ addComma }
322- json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]])
324+ var t = Number(obj[keys[i]])
325+ if (!isNaN(t)) {
326+ ${ addComma }
327+ json += $asString(keys[i]) + ':' + t
328+ }
323329 `
324330 } else if ( type === 'boolean' ) {
325331 code += `
@@ -364,22 +370,50 @@ function refFinder (ref, schema, externalSchema) {
364370
365371function buildCode ( schema , code , laterCode , name , externalSchema , fullSchema ) {
366372 Object . keys ( schema . properties || { } ) . forEach ( ( key , i , a ) => {
367- // Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
368- // see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
369- code += `
370- if (obj['${ key } '] !== undefined) {
371- ${ addComma }
372- json += '${ $asString ( key ) } :'
373- `
374-
375373 if ( schema . properties [ key ] [ '$ref' ] ) {
376374 schema . properties [ key ] = refFinder ( schema . properties [ key ] [ '$ref' ] , fullSchema , externalSchema )
377375 }
378376
379- var result = nested ( laterCode , name , key , schema . properties [ key ] , externalSchema , fullSchema )
377+ // Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
378+ // see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
379+
380+ var type = schema . properties [ key ] . type
381+ if ( type === 'number' ) {
382+ code += `
383+ var t = Number(obj['${ key } '])
384+ if (!isNaN(t)) {
385+ ${ addComma }
386+ json += '${ $asString ( key ) } :' + t
387+ `
388+ } else if ( type === 'integer' ) {
389+ code += `
390+ var rendered = false
391+ if (isLong && isLong(obj['${ key } '])) {
392+ ${ addComma }
393+ json += '${ $asString ( key ) } :' + obj['${ key } '].toString()
394+ rendered = true
395+ } else {
396+ var t = Number(obj['${ key } '])
397+ if (!isNaN(t)) {
398+ ${ addComma }
399+ json += '${ $asString ( key ) } :' + t
400+ rendered = true
401+ }
402+ }
403+
404+ if (rendered) {
405+ `
406+ } else {
407+ code += `
408+ if (obj['${ key } '] !== undefined) {
409+ ${ addComma }
410+ json += '${ $asString ( key ) } :'
411+ `
380412
381- code += result . code
382- laterCode = result . laterCode
413+ var result = nested ( laterCode , name , key , schema . properties [ key ] , externalSchema , fullSchema )
414+ code += result . code
415+ laterCode = result . laterCode
416+ }
383417
384418 if ( schema . required && schema . required . indexOf ( key ) !== - 1 ) {
385419 code += `
0 commit comments