@@ -15,6 +15,21 @@ const SINGLE_TICK = /'/g
1515let largeArraySize = 2e4
1616let largeArrayMechanism = 'default'
1717
18+ const serializerFns = `
19+ let {
20+ asString,
21+ asInteger,
22+ asNumber,
23+ asBoolean,
24+ asDateTime,
25+ asDate,
26+ asTime,
27+ } = serializer
28+
29+ asNumber = asNumber.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 }
@@ -729,13 +745,13 @@ function buildSingleTypeSerializer (context, location, input) {
729745 return 'json += JSON_STR_NULL'
730746 case 'string' : {
731747 if ( schema . format === 'date-time' ) {
732- return `json += serializer. asDateTime(${ input } )`
748+ return `json += asDateTime(${ input } )`
733749 } else if ( schema . format === 'date' ) {
734- return `json += serializer. asDate(${ input } )`
750+ return `json += asDate(${ input } )`
735751 } else if ( schema . format === 'time' ) {
736- return `json += serializer. asTime(${ input } )`
752+ return `json += asTime(${ input } )`
737753 } else if ( schema . format === 'unsafe' ) {
738- return `json += serializer. asUnsafeString(${ input } )`
754+ return `json += asUnsafeString(${ input } )`
739755 } else {
740756 return `
741757 if (typeof ${ input } !== 'string') {
@@ -744,22 +760,22 @@ function buildSingleTypeSerializer (context, location, input) {
744760 } else if (${ input } instanceof Date) {
745761 json += JSON_STR_QUOTE + ${ input } .toISOString() + JSON_STR_QUOTE
746762 } else if (${ input } instanceof RegExp) {
747- json += serializer. asString(${ input } .source)
763+ json += asString(${ input } .source)
748764 } else {
749- json += serializer. asString(${ input } .toString())
765+ json += asString(${ input } .toString())
750766 }
751767 } else {
752- json += serializer. asString(${ input } )
768+ json += asString(${ input } )
753769 }
754770 `
755771 }
756772 }
757773 case 'integer' :
758- return `json += serializer. asInteger(${ input } )`
774+ return `json += asInteger(${ input } )`
759775 case 'number' :
760- return `json += serializer. asNumber(${ input } )`
776+ return `json += asNumber(${ input } )`
761777 case 'boolean' :
762- return `json += serializer. asBoolean(${ input } )`
778+ return `json += asBoolean(${ input } )`
763779 case 'object' : {
764780 const funcName = buildObject ( context , location )
765781 return `json += ${ funcName } (${ input } )`
0 commit comments