This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
tests/ServiceStack.Text.Tests/JsonTests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -502,5 +502,46 @@ public void polymorphic_serialization_of_class_implementing_generic_ienumerable_
502502 Assert . IsAssignableFrom < FooTerm > ( terms2 . First ( ) ) ;
503503 }
504504
505+ [ Test ]
506+ public void Serialize_Polymorphic_collection ( )
507+ {
508+ var dto = new PolymorphicContainer
509+ {
510+ items = new List < PolymorphicBase >
511+ {
512+ new PolymorphicA { id = 1 , fieldA = "testingA" } ,
513+ new PolymorphicB { id = 2 , fieldB = "testingB" } ,
514+ }
515+ } ;
516+
517+ var json = dto . ToJson ( ) ;
518+
519+ var fromJson = json . FromJson < PolymorphicContainer > ( ) ;
520+ //fromJson.PrintDump();
521+
522+ Assert . That ( fromJson . items . Count , Is . EqualTo ( 2 ) ) ;
523+ Assert . That ( ( ( PolymorphicB ) fromJson . items [ 1 ] ) . fieldB , Is . EqualTo ( "testingB" ) ) ;
524+ }
505525 }
526+
527+ public abstract class PolymorphicBase
528+ {
529+ public int id { get ; set ; }
530+ }
531+
532+ public class PolymorphicA : PolymorphicBase
533+ {
534+ public string fieldA { get ; set ; }
535+ }
536+
537+ public class PolymorphicB : PolymorphicBase
538+ {
539+ public string fieldB { get ; set ; }
540+ }
541+
542+ public class PolymorphicContainer
543+ {
544+ public List < PolymorphicBase > items { get ; set ; }
545+ }
546+
506547}
You can’t perform that action at this time.
0 commit comments