Created
February 13, 2018 21:14
-
-
Save manuc66/f27314d9b2c860e42c236a0d4d582091 to your computer and use it in GitHub Desktop.
Sample for issue29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using NUnit.Framework; | |
namespace JsonSubTypes.Tests | |
{ | |
public class TwentyNine | |
{ | |
[Test] | |
public void TestIt() | |
{ | |
var auto = | |
"{\r\n\t\"id\": 123,\r\n\t\"type\": \"Auto\",\r\n\t\"status\": 17,\r\n\t\"insurer\": {\r\n\t\t\"id\": 12,\r\n\t\t\"name\": \"MyInsABC\",\r\n\t\t\"logo\": \"https://foo.com/logo.png\"\r\n\t},\r\n\t\"price\": \"1234.56\",\r\n\t\"coverage\": {\r\n\t\t\"bodily_injury_liability_per_person\": \"50000\",\r\n\t\t\"bodily_injury_liability_per_accident\": \"100000\",\r\n\t\t\"uninsured_motorist_per_person\": \"50000\",\r\n\t\t\"uninsured_motorist_per_accident\": \"100000\",\r\n\t\t\"property_damage_liability\": \"50000\",\r\n\t\t\"collision_deductible\": \"1000\",\r\n\t\t\"comprehensive_deductible\": \"1000\",\r\n\t\t\"comprehensive_type\": \"ACV\",\r\n\t\t\"medical_payment\": \"5000\",\r\n\t\t\"towing_road_service\": true,\r\n\t\t\"rental_reimbursement\": false,\r\n\t\t\"ride_sharing\": true\r\n\t}\r\n}"; | |
var renter = "{\r\n\"id\": 1235,\r\n \"type\": \"Renters\",\r\n \"status\": 19,\r\n \"insurer\": {\r\n \"id\": 27,\r\n \"name\": \"MyInsABC\",\r\n \"logo\": \"https://foo.com/logo.png\"\r\n },\r\n \"price\": \"130.00\",\r\n \"coverage\": {\r\n \"coverage_c_personal_property\": \"25000\",\r\n \"coverage_loss_of_use_type\": \"LS\",\r\n \"coverage_d_loss_of_use\": \"5000\",\r\n \"coverage_e_liability\": \"100000\",\r\n \"coverage_f_medical_per_person\": \"1000\",\r\n \"coverage_f_medical_per_accident\": \"1000\",\r\n \"deductible\": \"500\"\r\n }\r\n }"; | |
var a = JsonConvert.DeserializeObject<Quote>(auto); | |
var b = JsonConvert.DeserializeObject<Quote>(renter); | |
Assert.AreEqual("ACV", (a.Coverage as AutoCoverage).ComprehensiveType); | |
Assert.AreEqual("LS", (b.Coverage as RenterCoverage).CoverageLossOfUseType); | |
} | |
public class Quote | |
{ | |
[JsonProperty("id"), JsonRequired] | |
public int Id { get; set; } | |
[JsonProperty("type"), JsonRequired] | |
public string Type { get; set; } | |
[JsonProperty("status"), JsonRequired] | |
public long Status { get; set; } | |
[JsonProperty("insurer"), JsonRequired] | |
public Insurer Insurer { get; set; } | |
[JsonProperty("price")] | |
public string Price { get; set; } | |
[JsonProperty("coverage"), JsonRequired] | |
public CoverageBase Coverage { get; set; } | |
} | |
public class Insurer | |
{ | |
[JsonProperty("id"), JsonRequired] | |
public int Id { get; set; } | |
[JsonProperty("name"), JsonRequired] | |
public string Name { get; set; } | |
[JsonProperty("logo"), JsonRequired] | |
public string Logo { get; set; } | |
} | |
// Bonus points if I can use this instead of `string Type` in `Quote`. | |
public enum CoverageType | |
{ | |
Auto, | |
Renter, | |
} | |
// If this needs to be an empty interface I can do that. | |
[JsonConverter(typeof(JsonSubtypes))] | |
[JsonSubtypes.KnownSubTypeWithProperty(typeof(AutoCoverage), "rental_reimbursement")] | |
[JsonSubtypes.KnownSubTypeWithProperty(typeof(RenterCoverage), "coverage_f_medical_per_person")] | |
public abstract class CoverageBase { } | |
public class AutoCoverage : CoverageBase | |
{ | |
[JsonProperty("bodily_injury_liability_per_person"), JsonRequired] | |
public int BodilyInjuryLiabilityPerPerson { get; set; } | |
[JsonProperty("bodily_injury_liability_per_accident"), JsonRequired] | |
public int BodilyInjuryLiabilityPerAccident { get; set; } | |
[JsonProperty("uninsured_motorist_per_person"), JsonRequired] | |
public int UninsuredMotoristPerPerson { get; set; } | |
[JsonProperty("uninsured_motorist_per_accident"), JsonRequired] | |
public int UninsuredMotoristPerAccident { get; set; } | |
[JsonProperty("property_damage_liability"), JsonRequired] | |
public int PropertyDamageLiability { get; set; } | |
[JsonProperty("collision_deductible")] | |
public int? CollisionDeductible { get; set; } | |
[JsonProperty("comprehensive_deductible")] | |
public int? ComprehensiveDeductible { get; set; } | |
[JsonProperty("comprehensive_type"), JsonRequired] | |
public string ComprehensiveType { get; set; } | |
[JsonProperty("medical_payment"), JsonRequired] | |
public int MedicalPayment { get; set; } | |
[JsonProperty("towing_road_service"), JsonRequired] | |
public bool TowingRoadService { get; set; } | |
[JsonProperty("rental_reimbursement"), JsonRequired] | |
public bool RentalReimbursement { get; set; } | |
[JsonProperty("ride_sharing"), JsonRequired] | |
public bool RideSharing { get; set; } | |
} | |
public class RenterCoverage : CoverageBase | |
{ | |
/* | |
[JsonProperty("coverage_a_dwelling"), JsonRequired] | |
public int CoverageADwelling { get; set; } | |
[JsonProperty("coverage_b_other_structures"), JsonRequired] | |
public int CoverageBOtherStructures { get; set; } | |
*/ | |
[JsonProperty("coverage_c_personal_property"), JsonRequired] | |
public int CoverageCPersonalProperty { get; set; } | |
[JsonProperty("coverage_loss_of_use_type"), JsonRequired] | |
public string CoverageLossOfUseType { get; set; } | |
[JsonProperty("coverage_d_loss_of_use"), JsonRequired] | |
public int CoverageDLossOfUse { get; set; } | |
[JsonProperty("coverage_e_liability"), JsonRequired] | |
public int CoverageELiability { get; set; } | |
[JsonProperty("coverage_f_medical_per_person"), JsonRequired] | |
public int CoverageFMedicalPerPerson { get; set; } | |
[JsonProperty("coverage_f_medical_per_accident"), JsonRequired] | |
public int CoverageFMedicalPerAccident { get; set; } | |
[JsonProperty("deductible"), JsonRequired] | |
public int Deductible { get; set; } | |
/* | |
[JsonProperty("extended_replacement_cost_dwelling"), JsonRequired] | |
public int ExtendedReplacementCostDwelling { get; set; } | |
*/ | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using NUnit.Framework; | |
namespace JsonSubTypes.Tests | |
{ | |
public class TwentyNine2 | |
{ | |
[Test] | |
public void TestIt() | |
{ | |
var auto = | |
"{\r\n\t\"id\": 123,\r\n\t\"type\": \"Auto\",\r\n\t\"status\": 17,\r\n\t\"insurer\": {\r\n\t\t\"id\": 12,\r\n\t\t\"name\": \"MyInsABC\",\r\n\t\t\"logo\": \"https://foo.com/logo.png\"\r\n\t},\r\n\t\"price\": \"1234.56\",\r\n\t\"coverage\": {\r\n\t\t\"bodily_injury_liability_per_person\": \"50000\",\r\n\t\t\"bodily_injury_liability_per_accident\": \"100000\",\r\n\t\t\"uninsured_motorist_per_person\": \"50000\",\r\n\t\t\"uninsured_motorist_per_accident\": \"100000\",\r\n\t\t\"property_damage_liability\": \"50000\",\r\n\t\t\"collision_deductible\": \"1000\",\r\n\t\t\"comprehensive_deductible\": \"1000\",\r\n\t\t\"comprehensive_type\": \"ACV\",\r\n\t\t\"medical_payment\": \"5000\",\r\n\t\t\"towing_road_service\": true,\r\n\t\t\"rental_reimbursement\": false,\r\n\t\t\"ride_sharing\": true\r\n\t}\r\n}"; | |
var renter = "{\r\n\"id\": 1235,\r\n \"type\": \"Renters\",\r\n \"status\": 19,\r\n \"insurer\": {\r\n \"id\": 27,\r\n \"name\": \"MyInsABC\",\r\n \"logo\": \"https://foo.com/logo.png\"\r\n },\r\n \"price\": \"130.00\",\r\n \"coverage\": {\r\n \"coverage_c_personal_property\": \"25000\",\r\n \"coverage_loss_of_use_type\": \"LS\",\r\n \"coverage_d_loss_of_use\": \"5000\",\r\n \"coverage_e_liability\": \"100000\",\r\n \"coverage_f_medical_per_person\": \"1000\",\r\n \"coverage_f_medical_per_accident\": \"1000\",\r\n \"deductible\": \"500\"\r\n }\r\n }"; | |
var a = JsonConvert.DeserializeObject<Quote>(auto); | |
var b = JsonConvert.DeserializeObject<Quote>(renter); | |
Assert.AreEqual("ACV", (a as QuoteAutoCoverage).Coverage.ComprehensiveType); | |
Assert.AreEqual("LS", (b as QuoteRenterCoverage).Coverage.CoverageLossOfUseType); | |
} | |
class QuoteAutoCoverage : Quote | |
{ | |
[JsonProperty("coverage"), JsonRequired] | |
public AutoCoverage Coverage { get; set; } | |
} | |
class QuoteRenterCoverage : Quote | |
{ | |
[JsonProperty("coverage"), JsonRequired] | |
public RenterCoverage Coverage { get; set; } | |
} | |
[JsonConverter(typeof(JsonSubtypes), "type")] | |
[JsonSubtypes.KnownSubType(typeof(QuoteAutoCoverage), "Auto")] | |
[JsonSubtypes.KnownSubType(typeof(QuoteRenterCoverage), "Renters")] | |
public class Quote | |
{ | |
[JsonProperty("id"), JsonRequired] | |
public int Id { get; set; } | |
[JsonProperty("type"), JsonRequired] | |
public string Type { get; set; } | |
[JsonProperty("status"), JsonRequired] | |
public long Status { get; set; } | |
[JsonProperty("insurer"), JsonRequired] | |
public Insurer Insurer { get; set; } | |
[JsonProperty("price")] | |
public string Price { get; set; } | |
} | |
public class Insurer | |
{ | |
[JsonProperty("id"), JsonRequired] | |
public int Id { get; set; } | |
[JsonProperty("name"), JsonRequired] | |
public string Name { get; set; } | |
[JsonProperty("logo"), JsonRequired] | |
public string Logo { get; set; } | |
} | |
// Bonus points if I can use this instead of `string Type` in `Quote`. | |
public enum CoverageType | |
{ | |
Auto, | |
Renter, | |
} | |
// If this needs to be an empty interface I can do that. | |
public interface CoverageBase { } | |
public class AutoCoverage : CoverageBase | |
{ | |
[JsonProperty("bodily_injury_liability_per_person"), JsonRequired] | |
public int BodilyInjuryLiabilityPerPerson { get; set; } | |
[JsonProperty("bodily_injury_liability_per_accident"), JsonRequired] | |
public int BodilyInjuryLiabilityPerAccident { get; set; } | |
[JsonProperty("uninsured_motorist_per_person"), JsonRequired] | |
public int UninsuredMotoristPerPerson { get; set; } | |
[JsonProperty("uninsured_motorist_per_accident"), JsonRequired] | |
public int UninsuredMotoristPerAccident { get; set; } | |
[JsonProperty("property_damage_liability"), JsonRequired] | |
public int PropertyDamageLiability { get; set; } | |
[JsonProperty("collision_deductible")] | |
public int? CollisionDeductible { get; set; } | |
[JsonProperty("comprehensive_deductible")] | |
public int? ComprehensiveDeductible { get; set; } | |
[JsonProperty("comprehensive_type"), JsonRequired] | |
public string ComprehensiveType { get; set; } | |
[JsonProperty("medical_payment"), JsonRequired] | |
public int MedicalPayment { get; set; } | |
[JsonProperty("towing_road_service"), JsonRequired] | |
public bool TowingRoadService { get; set; } | |
[JsonProperty("rental_reimbursement"), JsonRequired] | |
public bool RentalReimbursement { get; set; } | |
[JsonProperty("ride_sharing"), JsonRequired] | |
public bool RideSharing { get; set; } | |
} | |
public class RenterCoverage : CoverageBase | |
{ | |
/* | |
[JsonProperty("coverage_a_dwelling"), JsonRequired] | |
public int CoverageADwelling { get; set; } | |
[JsonProperty("coverage_b_other_structures"), JsonRequired] | |
public int CoverageBOtherStructures { get; set; } | |
*/ | |
[JsonProperty("coverage_c_personal_property"), JsonRequired] | |
public int CoverageCPersonalProperty { get; set; } | |
[JsonProperty("coverage_loss_of_use_type"), JsonRequired] | |
public string CoverageLossOfUseType { get; set; } | |
[JsonProperty("coverage_d_loss_of_use"), JsonRequired] | |
public int CoverageDLossOfUse { get; set; } | |
[JsonProperty("coverage_e_liability"), JsonRequired] | |
public int CoverageELiability { get; set; } | |
[JsonProperty("coverage_f_medical_per_person"), JsonRequired] | |
public int CoverageFMedicalPerPerson { get; set; } | |
[JsonProperty("coverage_f_medical_per_accident"), JsonRequired] | |
public int CoverageFMedicalPerAccident { get; set; } | |
[JsonProperty("deductible"), JsonRequired] | |
public int Deductible { get; set; } | |
/* | |
[JsonProperty("extended_replacement_cost_dwelling"), JsonRequired] | |
public int ExtendedReplacementCostDwelling { get; set; } | |
*/ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment