1 @serdeIgnoreUnexpectedKeys 2 struct Named 3 { 4 string name; 5 } 6 7 @serdeIgnoreUnexpectedKeys 8 struct Person 9 { 10 string name; 11 int age; 12 } 13 14 @serdeIgnoreUnexpectedKeys 15 struct Place 16 { 17 string name; 18 double lat, lon; 19 } 20 21 StructVariant!(Named, Person, Place) var = Person("Bob", 32); 22 23 assert(var.serializeJson == `{"name":"Bob","age":32}`); 24 25 Named extractedNamed; 26 Person extractedPerson; 27 Place extractedPlace; 28 assert(var.tryExtract!Named(extractedNamed));//, var.mismatchMessage!Named(var.value)); 29 assert(var.tryExtract!Person(extractedPerson));//, var.mismatchMessage!Person(var.value)); 30 assert(!var.tryExtract!Place(extractedPlace));//, var.mismatchMessage!Place(var.value)); 31 32 try 33 { 34 var.extract!Place(); 35 assert(false); 36 } 37 catch (Exception e) 38 { 39 // assert(e.msg == "missing required key lat\nmissing required key lon", e.msg); 40 assert(e.msg == "can't extract Place from effective Person", e.msg); 41 } 42 43 assert(extractedNamed.name == "Bob"); 44 assert(extractedPerson == Person("Bob", 32)); 45 assert(extractedPlace is Place.init); 46 47 var = `{"name":"new name"}`.deserializeJson!(typeof(var)); 48 assert(var.extract!Named.name == "new name"); 49 assert(!var.tryExtract!Person(extractedPerson)); 50 assert(!var.tryExtract!Place(extractedPlace)); 51 52 assert(var.extract!Named.name == "new name"); 53 assertThrown({ 54 var = `{"nam":"name"}`.deserializeJson!(typeof(var)); 55 }()); 56 assert(var.extract!Named.name == "new name"); 57 58 assertThrown({ 59 var = `"hello"`.deserializeJson!(typeof(var)); 60 }());