@@ -166,6 +166,36 @@ export function serializeJsonCanonicalFunction(
166
166
}
167
167
}
168
168
169
+ if ( head === 'Add' && args . length === 2 && ! exclusions . includes ( 'Subtract' ) ) {
170
+ if ( args [ 1 ] ?. numericValue !== null ) {
171
+ const t1 = asSmallInteger ( args [ 1 ] ) ;
172
+ if ( t1 !== null && t1 < 0 )
173
+ return serializeJsonFunction (
174
+ ce ,
175
+ 'Subtract' ,
176
+ [ args [ 0 ] , ce . number ( - t1 ) ] ,
177
+ metadata
178
+ ) ;
179
+ }
180
+ if ( args [ 1 ] ?. head === 'Negate' ) {
181
+ return serializeJsonFunction (
182
+ ce ,
183
+ 'Subtract' ,
184
+ [ args [ 0 ] , args [ 1 ] . op1 ] ,
185
+ metadata
186
+ ) ;
187
+ }
188
+ }
189
+
190
+ if ( head === 'Tuple' ) {
191
+ if ( args . length === 1 && ! exclusions . includes ( 'Single' ) )
192
+ return serializeJsonFunction ( ce , 'Single' , args , metadata ) ;
193
+ if ( args . length === 2 && ! exclusions . includes ( 'Pair' ) )
194
+ return serializeJsonFunction ( ce , 'Pair' , args , metadata ) ;
195
+ if ( args . length === 3 && ! exclusions . includes ( 'Triple' ) )
196
+ return serializeJsonFunction ( ce , 'Triple' , args , metadata ) ;
197
+ }
198
+
169
199
return serializeJsonFunction ( ce , head , args , metadata ) ;
170
200
}
171
201
@@ -175,32 +205,27 @@ export function serializeJsonFunction(
175
205
args : ( undefined | BoxedExpression ) [ ] ,
176
206
metadata ?: Metadata
177
207
) : Expression {
178
- // Special case some functions...
179
-
180
208
const exclusions = ce . jsonSerializationOptions . exclude ;
181
209
182
- if (
183
- ( head === 'Rational' || head === 'Divide' ) &&
184
- args . length === 2 &&
185
- asSmallInteger ( args [ 0 ] ) === 1 &&
186
- asSmallInteger ( args [ 1 ] ) === 2 &&
187
- ! exclusions . includes ( 'Half' )
188
- ) {
189
- return serializeJsonSymbol ( ce , 'Half' , {
190
- ...metadata ,
191
- wikidata : 'Q39373172' ,
192
- } ) ;
193
- }
194
-
195
- if ( args . length === 1 ) {
210
+ //
211
+ // The only sugaring done for both canonical and non-canonical expressions
212
+ // is for 'Negate', since `-2` gets parsed as `['Negate', 2]` and not
213
+ // `-2`.
214
+ //
215
+ if ( head === 'Negate' && args . length === 1 ) {
196
216
const num0 = args [ 0 ] ?. numericValue ;
197
- if ( head === 'Negate' && num0 !== null ) {
217
+ if ( num0 !== null ) {
198
218
if ( typeof num0 === 'number' ) return serializeJsonNumber ( ce , - num0 ) ;
199
219
if ( num0 instanceof Decimal ) return serializeJsonNumber ( ce , num0 . neg ( ) ) ;
200
220
if ( num0 instanceof Complex ) return serializeJsonNumber ( ce , num0 . neg ( ) ) ;
201
221
if ( isRational ( num0 ) ) return serializeJsonNumber ( ce , neg ( num0 ) ) ;
202
222
}
203
223
}
224
+
225
+ //
226
+ // If there are some exclusions, try to avoid them.
227
+ // This is done both to canonical or non-canonical expressions.
228
+ //
204
229
if ( typeof head === 'string' && exclusions . includes ( head ) ) {
205
230
if ( head === 'Rational' && args . length === 2 )
206
231
return serializeJsonFunction ( ce , 'Divide' , args , metadata ) ;
@@ -267,8 +292,11 @@ export function serializeJsonFunction(
267
292
if ( head === 'Exp' && args . length === 1 )
268
293
return serializeJsonFunction ( ce , 'Power' , [ ce . E , args [ 0 ] ] , metadata ) ;
269
294
295
+ if ( head === 'Pair' || head == 'Single' || head === 'Triple' )
296
+ return serializeJsonFunction ( ce , 'Tuple' , args , metadata ) ;
297
+
270
298
// Note: even though 'Subtract' is boxed out, we still need to handle it here
271
- // because the function may be called with a 'Subtract' head.
299
+ // because the function may be called with a non-canonical 'Subtract' head.
272
300
if ( head === 'Subtract' && args . length === 2 )
273
301
return serializeJsonFunction (
274
302
ce ,
@@ -280,36 +308,6 @@ export function serializeJsonFunction(
280
308
return serializeJsonFunction ( ce , 'Negate' , args , metadata ) ;
281
309
}
282
310
283
- if ( head === 'Add' && args . length === 2 && ! exclusions . includes ( 'Subtract' ) ) {
284
- if ( args [ 1 ] ?. numericValue !== null ) {
285
- const t1 = asSmallInteger ( args [ 1 ] ) ;
286
- if ( t1 !== null && t1 < 0 )
287
- return serializeJsonFunction (
288
- ce ,
289
- 'Subtract' ,
290
- [ args [ 0 ] , ce . number ( - t1 ) ] ,
291
- metadata
292
- ) ;
293
- }
294
- if ( args [ 1 ] ?. head === 'Negate' ) {
295
- return serializeJsonFunction (
296
- ce ,
297
- 'Subtract' ,
298
- [ args [ 0 ] , args [ 1 ] . op1 ] ,
299
- metadata
300
- ) ;
301
- }
302
- }
303
-
304
- if ( head === 'Tuple' ) {
305
- if ( args . length === 1 && ! exclusions . includes ( 'Single' ) )
306
- return serializeJsonFunction ( ce , 'Single' , args , metadata ) ;
307
- if ( args . length === 2 && ! exclusions . includes ( 'Pair' ) )
308
- return serializeJsonFunction ( ce , 'Pair' , args , metadata ) ;
309
- if ( args . length === 3 && ! exclusions . includes ( 'Triple' ) )
310
- return serializeJsonFunction ( ce , 'Triple' , args , metadata ) ;
311
- }
312
-
313
311
const jsonHead =
314
312
typeof head === 'string' ? _escapeJsonString ( head ) : head . json ;
315
313
0 commit comments