@@ -413,14 +413,23 @@ public void UnionWithMixedAmbiguity_GetTypeInfoSucceeds()
413413 Assert . NotNull ( typeInfo ) ;
414414 }
415415
416- [ JsonNumberHandling ( JsonNumberHandling . Strict ) ]
417- public union StrictIntOrString ( int , string ) ;
418-
416+ [ JsonUnion ( TypeClassifier = typeof ( IntCaseClassifierFactory ) ) ]
419417 [ JsonNumberHandling ( JsonNumberHandling . AllowReadingFromString | JsonNumberHandling . WriteAsString ) ]
420- public union StringEncodedInt ( int ) ;
418+ public union NumberHandlingIntUnion ( int ) ;
421419
420+ [ JsonUnion ( TypeClassifier = typeof ( IntCaseClassifierFactory ) ) ]
422421 public union PlainIntUnion ( int ) ;
423422
423+ public sealed class IntCaseClassifierFactory : JsonTypeClassifierFactory
424+ {
425+ public override bool CanClassify ( JsonTypeClassifierContext context ) =>
426+ context . DeclaringType == typeof ( NumberHandlingIntUnion ) ||
427+ context . DeclaringType == typeof ( PlainIntUnion ) ;
428+
429+ public override JsonTypeClassifier CreateJsonClassifier ( JsonTypeClassifierContext context , JsonSerializerOptions options ) =>
430+ static ( ref Utf8JsonReader reader ) => typeof ( int ) ;
431+ }
432+
424433 public sealed class UnionNumberHandlingContainer
425434 {
426435 [ JsonNumberHandling ( JsonNumberHandling . AllowReadingFromString | JsonNumberHandling . WriteAsString ) ]
@@ -430,27 +439,16 @@ public sealed class UnionNumberHandlingContainer
430439 public sealed class StrictUnionNumberHandlingContainer
431440 {
432441 [ JsonNumberHandling ( JsonNumberHandling . Strict ) ]
433- public MixedUnion Value { get ; set ; } = new ( 0 ) ;
434- }
435-
436- [ Fact ]
437- public async Task UnionNumberHandling_TypeAttributeOverridesOptions ( )
438- {
439- JsonSerializerOptions options = Serializer . CreateOptions (
440- static options => options . NumberHandling = JsonNumberHandling . AllowReadingFromString ) ;
441-
442- StrictIntOrString ? value = await Serializer . DeserializeWrapper < StrictIntOrString > ( "\" hello\" " , options ) ;
443-
444- Assert . Equal ( "hello" , GetUnionValue ( value ! ) ) ;
442+ public PlainIntUnion Value { get ; set ; }
445443 }
446444
447445 [ Fact ]
448446 public async Task UnionNumberHandling_TypeAttributeSupportsReadingAndWritingNumbersAsStrings ( )
449447 {
450- StringEncodedInt ? value = await Serializer . DeserializeWrapper < StringEncodedInt > ( "\" 42\" " ) ;
448+ NumberHandlingIntUnion ? value = await Serializer . DeserializeWrapper < NumberHandlingIntUnion > ( "\" 42\" " ) ;
451449 Assert . Equal ( 42 , GetUnionValue ( value ! ) ) ;
452450
453- string json = await Serializer . SerializeWrapper ( new StringEncodedInt ( 42 ) ) ;
451+ string json = await Serializer . SerializeWrapper ( new NumberHandlingIntUnion ( 42 ) ) ;
454452 Assert . Equal ( "\" 42\" " , json ) ;
455453 }
456454
@@ -488,12 +486,31 @@ public async Task UnionNumberHandling_PropertyAttributeSupportsReadingAndWriting
488486 public async Task UnionNumberHandling_StrictPropertyAttributeOverridesOptions ( )
489487 {
490488 JsonSerializerOptions options = Serializer . CreateOptions (
491- static options => options . NumberHandling = JsonNumberHandling . AllowReadingFromString ) ;
489+ static options => options . NumberHandling = JsonNumberHandling . WriteAsString ) ;
490+
491+ string json = await Serializer . SerializeWrapper (
492+ new StrictUnionNumberHandlingContainer { Value = new PlainIntUnion ( 42 ) } ,
493+ options ) ;
494+
495+ JsonTestHelper . AssertJsonEqual ( """{"Value":42}""" , json ) ;
496+ }
497+
498+ [ Fact ]
499+ public async Task UnionNumberHandling_CaseTypeInfoOverridesOptions ( )
500+ {
501+ JsonSerializerOptions options = Serializer . CreateOptions (
502+ static options => options . NumberHandling = JsonNumberHandling . WriteAsString ,
503+ modifier : static typeInfo =>
504+ {
505+ if ( typeInfo . Type == typeof ( int ) )
506+ {
507+ typeInfo . NumberHandling = JsonNumberHandling . Strict ;
508+ }
509+ } ) ;
492510
493- StrictUnionNumberHandlingContainer ? value =
494- await Serializer . DeserializeWrapper < StrictUnionNumberHandlingContainer > ( """{"Value":"hello"}""" , options ) ;
511+ string json = await Serializer . SerializeWrapper ( new PlainIntUnion ( 42 ) , options ) ;
495512
496- Assert . Equal ( "hello " , GetUnionValue ( value ! . Value ) ) ;
513+ Assert . Equal ( "42 " , json ) ;
497514 }
498515
499516 public class Animal { }
0 commit comments