@@ -15,6 +15,21 @@ const SINGLE_TICK = /'/g
15
15
let largeArraySize = 2e4
16
16
let largeArrayMechanism = 'default'
17
17
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
+
18
33
const validRoundingMethods = [
19
34
'floor' ,
20
35
'ceil' ,
@@ -141,6 +156,7 @@ function build (schema, options) {
141
156
const code = buildValue ( context , location , 'input' )
142
157
143
158
let contextFunctionCode = `
159
+ ${ serializerFns }
144
160
const JSON_STR_BEGIN_OBJECT = '{'
145
161
const JSON_STR_END_OBJECT = '}'
146
162
const JSON_STR_BEGIN_ARRAY = '['
@@ -292,7 +308,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
292
308
code += `
293
309
if (/${ propertyKey . replace ( / \\ * \/ / g, '\\/' ) } /.test(key)) {
294
310
${ addComma }
295
- json += serializer. asString(key) + JSON_STR_COLONS
311
+ json += asString(key) + JSON_STR_COLONS
296
312
${ buildValue ( context , propertyLocation , 'value' ) }
297
313
continue
298
314
}
@@ -307,13 +323,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
307
323
if ( additionalPropertiesSchema === true ) {
308
324
code += `
309
325
${ addComma }
310
- json += serializer. asString(key) + JSON_STR_COLONS + JSON.stringify(value)
326
+ json += asString(key) + JSON_STR_COLONS + JSON.stringify(value)
311
327
`
312
328
} else {
313
329
const propertyLocation = location . getPropertyLocation ( 'additionalProperties' )
314
330
code += `
315
331
${ addComma }
316
- json += serializer. asString(key) + JSON_STR_COLONS
332
+ json += asString(key) + JSON_STR_COLONS
317
333
${ buildValue ( context , propertyLocation , 'value' ) }
318
334
`
319
335
}
@@ -731,13 +747,13 @@ function buildSingleTypeSerializer (context, location, input) {
731
747
return 'json += JSON_STR_NULL'
732
748
case 'string' : {
733
749
if ( schema . format === 'date-time' ) {
734
- return `json += serializer. asDateTime(${ input } )`
750
+ return `json += asDateTime(${ input } )`
735
751
} else if ( schema . format === 'date' ) {
736
- return `json += serializer. asDate(${ input } )`
752
+ return `json += asDate(${ input } )`
737
753
} else if ( schema . format === 'time' ) {
738
- return `json += serializer. asTime(${ input } )`
754
+ return `json += asTime(${ input } )`
739
755
} else if ( schema . format === 'unsafe' ) {
740
- return `json += serializer. asUnsafeString(${ input } )`
756
+ return `json += asUnsafeString(${ input } )`
741
757
} else {
742
758
return `
743
759
if (typeof ${ input } !== 'string') {
@@ -746,22 +762,22 @@ function buildSingleTypeSerializer (context, location, input) {
746
762
} else if (${ input } instanceof Date) {
747
763
json += JSON_STR_QUOTE + ${ input } .toISOString() + JSON_STR_QUOTE
748
764
} else if (${ input } instanceof RegExp) {
749
- json += serializer. asString(${ input } .source)
765
+ json += asString(${ input } .source)
750
766
} else {
751
- json += serializer. asString(${ input } .toString())
767
+ json += asString(${ input } .toString())
752
768
}
753
769
} else {
754
- json += serializer. asString(${ input } )
770
+ json += asString(${ input } )
755
771
}
756
772
`
757
773
}
758
774
}
759
775
case 'integer' :
760
- return `json += serializer. asInteger(${ input } )`
776
+ return `json += asInteger(${ input } )`
761
777
case 'number' :
762
- return `json += serializer. asNumber(${ input } )`
778
+ return `json += asNumber(${ input } )`
763
779
case 'boolean' :
764
- return `json += serializer. asBoolean(${ input } )`
780
+ return `json += asBoolean(${ input } )`
765
781
case 'object' : {
766
782
const funcName = buildObject ( context , location )
767
783
return `json += ${ funcName } (${ input } )`
0 commit comments