22
33const fastSafeStringify = require ( 'fast-safe-stringify' )
44
5+ let isLong
6+ try {
7+ isLong = require ( 'long' ) . isLong
8+ } catch ( e ) {
9+ isLong = null
10+ }
11+
512function build ( schema , options ) {
613 options = options || { }
714 /* eslint no-new-func: "off" */
@@ -20,6 +27,19 @@ function build (schema, options) {
2027 ${ $asNull . toString ( ) }
2128 ${ $asBoolean . toString ( ) }
2229 `
30+
31+ // only handle longs if the module is used
32+ if ( isLong ) {
33+ code += `
34+ const isLong = ${ isLong . toString ( ) }
35+ ${ $asInteger . toString ( ) }
36+ `
37+ } else {
38+ code += `
39+ const $asInteger = $asNumber
40+ `
41+ }
42+
2343 var main
2444
2545 switch ( schema . type ) {
@@ -31,6 +51,8 @@ function build (schema, options) {
3151 main = $asString . name
3252 break
3353 case 'integer' :
54+ main = $asInteger . name
55+ break
3456 case 'number' :
3557 main = $asNumber . name
3658 break
@@ -62,6 +84,14 @@ function $asNull () {
6284 return 'null'
6385}
6486
87+ function $asInteger ( i ) {
88+ if ( isLong && isLong ( i ) ) {
89+ return i . toString ( )
90+ } else {
91+ return $asNumber ( i )
92+ }
93+ }
94+
6595function $asNumber ( i ) {
6696 var num = Number ( i )
6797 if ( isNaN ( num ) ) {
@@ -149,7 +179,11 @@ function addPatternProperties (schema, externalSchema, fullSchema) {
149179 code += `
150180 json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
151181 `
152- } else if ( type === 'number' || type === 'integer' ) {
182+ } else if ( type === 'integer' ) {
183+ code += `
184+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) + ','
185+ `
186+ } else if ( type === 'number' ) {
153187 code += `
154188 json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
155189 `
@@ -209,7 +243,11 @@ function additionalProperty (schema, externalSchema, fullSchema) {
209243 code += `
210244 json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
211245 `
212- } else if ( type === 'number' || type === 'integer' ) {
246+ } else if ( type === 'integer' ) {
247+ code += `
248+ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) + ','
249+ `
250+ } else if ( type === 'number' ) {
213251 code += `
214252 json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
215253 `
@@ -365,8 +403,12 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) {
365403 json += $asString(obj${ key } )
366404 `
367405 break
368- case 'number' :
369406 case 'integer' :
407+ code += `
408+ json += $asInteger(obj${ key } )
409+ `
410+ break
411+ case 'number' :
370412 code += `
371413 json += $asNumber(obj${ key } )
372414 `
0 commit comments