@@ -39,7 +39,6 @@ interface SerializeCxt {
39
39
readonly self : Ajv // current Ajv instance
40
40
readonly schemaEnv : SchemaEnv
41
41
readonly definitions : SchemaObjectMap
42
- readonly jsonStr : Name
43
42
schema : SchemaObject
44
43
data : Code
45
44
}
@@ -55,14 +54,12 @@ export function compileSerializer(
55
54
const { ownProperties} = this . opts
56
55
const gen = new CodeGen ( this . scope , { es5, lines, ownProperties} )
57
56
const serializeName = gen . scopeName ( "serialize" )
58
- const jsonStr = gen . name ( "json" )
59
57
const cxt = {
60
58
self : this ,
61
59
gen,
62
60
schema : sch . schema as SchemaObject ,
63
61
schemaEnv : sch ,
64
62
definitions,
65
- jsonStr,
66
63
data : N . data ,
67
64
}
68
65
@@ -71,9 +68,9 @@ export function compileSerializer(
71
68
this . _compilations . add ( sch )
72
69
sch . serializeName = serializeName
73
70
gen . func ( serializeName , N . data , false , ( ) => {
74
- gen . let ( jsonStr , str `` )
71
+ gen . let ( N . json , str `` )
75
72
serializeCode ( cxt )
76
- gen . return ( jsonStr )
73
+ gen . return ( N . json )
77
74
} )
78
75
gen . optimize ( this . opts . code . optimize )
79
76
const serializeFuncCode = gen . toString ( )
@@ -105,47 +102,49 @@ function serializeCode(cxt: SerializeCxt): void {
105
102
}
106
103
107
104
function serializeNullable ( cxt : SerializeCxt , serializeForm : ( _cxt : SerializeCxt ) => void ) : void {
108
- const { gen, schema, jsonStr , data} = cxt
105
+ const { gen, schema, data} = cxt
109
106
if ( ! schema . nullable ) return serializeForm ( cxt )
110
107
gen . if (
111
108
_ `${ data } === undefined || ${ data } === null` ,
112
- ( ) => gen . add ( jsonStr , _ `"null"` ) ,
109
+ ( ) => gen . add ( N . json , _ `"null"` ) ,
113
110
( ) => serializeForm ( cxt )
114
111
)
115
112
}
116
113
117
114
function serializeElements ( cxt : SerializeCxt ) : void {
118
- const { gen, schema, jsonStr , data} = cxt
119
- gen . add ( jsonStr , str `[` )
115
+ const { gen, schema, data} = cxt
116
+ gen . add ( N . json , str `[` )
120
117
const first = gen . let ( "first" , true )
121
118
gen . forOf ( "el" , data , ( el ) => {
122
119
addComma ( cxt , first )
123
120
serializeCode ( { ...cxt , schema : schema . elements , data : el } )
124
121
} )
125
- gen . add ( jsonStr , str `]` )
122
+ gen . add ( N . json , str `]` )
126
123
}
127
124
128
125
function serializeValues ( cxt : SerializeCxt ) : void {
129
- const { gen, schema, jsonStr , data} = cxt
130
- gen . add ( jsonStr , str `{` )
126
+ const { gen, schema, data} = cxt
127
+ gen . add ( N . json , str `{` )
131
128
const first = gen . let ( "first" , true )
132
- gen . forIn ( "key" , data , ( key ) => serializeKeyValue ( cxt , key , schema . values , first ) )
133
- gen . add ( jsonStr , str `}` )
129
+ gen . forIn ( "key" , data , ( key ) =>
130
+ serializeKeyValue ( cxt , key , schema . values , first )
131
+ )
132
+ gen . add ( N . json , str `}` )
134
133
}
135
134
136
135
function serializeKeyValue ( cxt : SerializeCxt , key : Name , schema : SchemaObject , first : Name ) : void {
137
- const { gen, jsonStr , data} = cxt
136
+ const { gen, data} = cxt
138
137
addComma ( cxt , first )
139
138
serializeString ( { ...cxt , data : key } )
140
- gen . add ( jsonStr , str `:` )
139
+ gen . add ( N . json , str `:` )
141
140
const value = gen . const ( "value" , _ `${ data } ${ getProperty ( key ) } ` )
142
141
serializeCode ( { ...cxt , schema, data : value } )
143
142
}
144
143
145
144
function serializeDiscriminator ( cxt : SerializeCxt ) : void {
146
- const { gen, schema, jsonStr , data} = cxt
145
+ const { gen, schema, data} = cxt
147
146
const { discriminator} = schema
148
- gen . add ( jsonStr , str `{${ JSON . stringify ( discriminator ) } :` )
147
+ gen . add ( N . json , str `{${ JSON . stringify ( discriminator ) } :` )
149
148
const tag = gen . const ( "tag" , _ `${ data } ${ getProperty ( discriminator ) } ` )
150
149
serializeString ( { ...cxt , data : tag } )
151
150
gen . if ( false )
@@ -155,18 +154,18 @@ function serializeDiscriminator(cxt: SerializeCxt): void {
155
154
serializeSchemaProperties ( { ...cxt , schema : sch } , discriminator )
156
155
}
157
156
gen . endIf ( )
158
- gen . add ( jsonStr , str `}` )
157
+ gen . add ( N . json , str `}` )
159
158
}
160
159
161
160
function serializeProperties ( cxt : SerializeCxt ) : void {
162
- const { gen, jsonStr } = cxt
163
- gen . add ( jsonStr , str `{` )
161
+ const { gen} = cxt
162
+ gen . add ( N . json , str `{` )
164
163
serializeSchemaProperties ( cxt )
165
- gen . add ( jsonStr , str `}` )
164
+ gen . add ( N . json , str `}` )
166
165
}
167
166
168
167
function serializeSchemaProperties ( cxt : SerializeCxt , discriminator ?: string ) : void {
169
- const { gen, schema, jsonStr , data} = cxt
168
+ const { gen, schema, data} = cxt
170
169
const { properties, optionalProperties} = schema
171
170
const props = keys ( properties )
172
171
const optProps = keys ( optionalProperties )
@@ -207,8 +206,8 @@ function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): v
207
206
208
207
function serializeProperty ( key : string , propSchema : SchemaObject , value : Name ) : void {
209
208
if ( first ) first = false
210
- else gen . add ( jsonStr , str `,` )
211
- gen . add ( jsonStr , str `${ JSON . stringify ( key ) } :` )
209
+ else gen . add ( N . json , str `,` )
210
+ gen . add ( N . json , str `${ JSON . stringify ( key ) } :` )
212
211
serializeCode ( { ...cxt , schema : propSchema , data : value } )
213
212
}
214
213
@@ -218,18 +217,18 @@ function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): v
218
217
}
219
218
220
219
function serializeType ( cxt : SerializeCxt ) : void {
221
- const { gen, schema, jsonStr , data} = cxt
220
+ const { gen, schema, data} = cxt
222
221
switch ( schema . type ) {
223
222
case "boolean" :
224
- gen . add ( jsonStr , _ `${ data } ? "true" : "false"` )
223
+ gen . add ( N . json , _ `${ data } ? "true" : "false"` )
225
224
break
226
225
case "string" :
227
226
serializeString ( cxt )
228
227
break
229
228
case "timestamp" :
230
229
gen . if (
231
230
_ `${ data } instanceof Date` ,
232
- ( ) => gen . add ( jsonStr , _ `${ data } .toISOString()` ) ,
231
+ ( ) => gen . add ( N . json , _ `${ data } .toISOString()` ) ,
233
232
( ) => serializeString ( cxt )
234
233
)
235
234
break
@@ -238,23 +237,23 @@ function serializeType(cxt: SerializeCxt): void {
238
237
}
239
238
}
240
239
241
- function serializeString ( { gen, jsonStr , data} : SerializeCxt ) : void {
242
- gen . add ( jsonStr , _ `${ quoteFunc ( gen ) } (${ data } )` )
240
+ function serializeString ( { gen, data} : SerializeCxt ) : void {
241
+ gen . add ( N . json , _ `${ quoteFunc ( gen ) } (${ data } )` )
243
242
}
244
243
245
- function serializeNumber ( { gen, jsonStr , data} : SerializeCxt ) : void {
246
- gen . add ( jsonStr , _ `"" + ${ data } ` )
244
+ function serializeNumber ( { gen, data} : SerializeCxt ) : void {
245
+ gen . add ( N . json , _ `"" + ${ data } ` )
247
246
}
248
247
249
248
function serializeRef ( cxt : SerializeCxt ) : void {
250
- const { gen, self, jsonStr , data, definitions, schema, schemaEnv} = cxt
249
+ const { gen, self, data, definitions, schema, schemaEnv} = cxt
251
250
const { ref} = schema
252
251
const refSchema = definitions [ ref ]
253
252
if ( ! refSchema ) throw new MissingRefError ( "" , ref , `No definition ${ ref } ` )
254
253
if ( ! hasRef ( refSchema ) ) return serializeCode ( { ...cxt , schema : refSchema } )
255
254
const { root} = schemaEnv
256
255
const sch = compileSerializer . call ( self , new SchemaEnv ( { schema : refSchema , root} ) , definitions )
257
- gen . add ( jsonStr , _ `${ getSerialize ( gen , sch ) } (${ data } )` )
256
+ gen . add ( N . json , _ `${ getSerialize ( gen , sch ) } (${ data } )` )
258
257
}
259
258
260
259
function getSerialize ( gen : CodeGen , sch : SchemaEnv ) : Code {
@@ -263,15 +262,15 @@ function getSerialize(gen: CodeGen, sch: SchemaEnv): Code {
263
262
: _ `${ gen . scopeValue ( "wrapper" , { ref : sch } ) } .serialize`
264
263
}
265
264
266
- function serializeEmpty ( { gen, jsonStr , data} : SerializeCxt ) : void {
267
- gen . add ( jsonStr , _ `JSON.stringify(${ data } )` )
265
+ function serializeEmpty ( { gen, data} : SerializeCxt ) : void {
266
+ gen . add ( N . json , _ `JSON.stringify(${ data } )` )
268
267
}
269
268
270
- function addComma ( { gen, jsonStr } : SerializeCxt , first : Name ) : void {
269
+ function addComma ( { gen} : SerializeCxt , first : Name ) : void {
271
270
gen . if (
272
271
first ,
273
272
( ) => gen . assign ( first , false ) ,
274
- ( ) => gen . add ( jsonStr , str `,` )
273
+ ( ) => gen . add ( N . json , str `,` )
275
274
)
276
275
}
277
276
0 commit comments