@@ -15,6 +15,21 @@ const SINGLE_TICK = /'/g
1515let largeArraySize = 2e4
1616let largeArrayMechanism = 'default'
1717
18+ const serializerFns = `
19+ const {
20+ asString,
21+ asNumber,
22+ asBoolean,
23+ asDateTime,
24+ asDate,
25+ asTime,
26+ asUnsafeString
27+ } = serializer
28+
29+ const asInteger = serializer.asInteger.bind(serializer)
30+
31+ `
32+
1833const validRoundingMethods = [
1934 'floor' ,
2035 'ceil' ,
@@ -141,6 +156,7 @@ function build (schema, options) {
141156 const code = buildValue ( context , location , 'input' )
142157
143158 let contextFunctionCode = `
159+ ${ serializerFns }
144160 const JSON_STR_BEGIN_OBJECT = '{'
145161 const JSON_STR_END_OBJECT = '}'
146162 const JSON_STR_BEGIN_ARRAY = '['
@@ -292,7 +308,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
292308 code += `
293309 if (/${ propertyKey . replace ( / \\ * \/ / g, '\\/' ) } /.test(key)) {
294310 ${ addComma }
295- json += serializer. asString(key) + JSON_STR_COLONS
311+ json += asString(key) + JSON_STR_COLONS
296312 ${ buildValue ( context , propertyLocation , 'value' ) }
297313 continue
298314 }
@@ -307,13 +323,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
307323 if ( additionalPropertiesSchema === true ) {
308324 code += `
309325 ${ addComma }
310- json += serializer. asString(key) + JSON_STR_COLONS + JSON.stringify(value)
326+ json += asString(key) + JSON_STR_COLONS + JSON.stringify(value)
311327 `
312328 } else {
313329 const propertyLocation = location . getPropertyLocation ( 'additionalProperties' )
314330 code += `
315331 ${ addComma }
316- json += serializer. asString(key) + JSON_STR_COLONS
332+ json += asString(key) + JSON_STR_COLONS
317333 ${ buildValue ( context , propertyLocation , 'value' ) }
318334 `
319335 }
@@ -731,13 +747,13 @@ function buildSingleTypeSerializer (context, location, input) {
731747 return 'json += JSON_STR_NULL'
732748 case 'string' : {
733749 if ( schema . format === 'date-time' ) {
734- return `json += serializer. asDateTime(${ input } )`
750+ return `json += asDateTime(${ input } )`
735751 } else if ( schema . format === 'date' ) {
736- return `json += serializer. asDate(${ input } )`
752+ return `json += asDate(${ input } )`
737753 } else if ( schema . format === 'time' ) {
738- return `json += serializer. asTime(${ input } )`
754+ return `json += asTime(${ input } )`
739755 } else if ( schema . format === 'unsafe' ) {
740- return `json += serializer. asUnsafeString(${ input } )`
756+ return `json += asUnsafeString(${ input } )`
741757 } else {
742758 return `
743759 if (typeof ${ input } !== 'string') {
@@ -746,22 +762,22 @@ function buildSingleTypeSerializer (context, location, input) {
746762 } else if (${ input } instanceof Date) {
747763 json += JSON_STR_QUOTE + ${ input } .toISOString() + JSON_STR_QUOTE
748764 } else if (${ input } instanceof RegExp) {
749- json += serializer. asString(${ input } .source)
765+ json += asString(${ input } .source)
750766 } else {
751- json += serializer. asString(${ input } .toString())
767+ json += asString(${ input } .toString())
752768 }
753769 } else {
754- json += serializer. asString(${ input } )
770+ json += asString(${ input } )
755771 }
756772 `
757773 }
758774 }
759775 case 'integer' :
760- return `json += serializer. asInteger(${ input } )`
776+ return `json += asInteger(${ input } )`
761777 case 'number' :
762- return `json += serializer. asNumber(${ input } )`
778+ return `json += asNumber(${ input } )`
763779 case 'boolean' :
764- return `json += serializer. asBoolean(${ input } )`
780+ return `json += asBoolean(${ input } )`
765781 case 'object' : {
766782 const funcName = buildObject ( context , location )
767783 return `json += ${ funcName } (${ input } )`
0 commit comments