using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
public abstract class PostalAddressOccupancy
public DateTime EffectiveDate { get; set; }
public DateTime EndDate { get; set; }
public string EntityType { get; set; }
public string Href { get; set; }
public string Id { get; set; }
public bool IsCorrespondenceAddress { get; set; }
public PostalAddress PostalAddress { get; set; }
public string PostalAddressType { get; set; }
public abstract object GetOccupant();
public class PostalAddressOccupancy<T> : PostalAddressOccupancy
public T Occupant { get; set; }
public override object GetOccupant()
public class PostalAddress
public string Address { get; set; }
public string Name { get; set; }
public string StudentId { get; set; }
public string Name { get; set; }
public string StaffId { get; set; }
public static class PostalAddressOccupancyExtensions
public static PostalAddressOccupancy DeserializePostalAddressOccupancy(this RestSharp.IRestResponse response)
var addressObj = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(response.Content);
var type = PostalAddressOccupancyExtensions.InferPostalAddressOccupancyType(addressObj);
return (PostalAddressOccupancy)SimpleJson.SimpleJson.DeserializeObject(response.Content, type);
static Type InferPostalAddressOccupancyType(JsonObject root)
var occupantObj = root["Occupant"];
if (occupantObj is JsonObject)
var occupant = (JsonObject)occupantObj;
if (occupant.ContainsKey("StudentId"))
return typeof(PostalAddressOccupancy<Student>);
else if (occupant.ContainsKey("StaffId"))
return typeof(PostalAddressOccupancy<Staff>);
return typeof(PostalAddressOccupancy<>).MakeGenericType(new[] { occupantObj.GetType() });
public static void Test()
Console.WriteLine("RestSharp version: " + typeof(RestResponse).Assembly.FullName);
Test(new Staff { Name = "my name", StaffId = "222" });
Test(new Student { Name = "some kid", StudentId = "-121" });
static void Test<T>(T occupant)
var address = new PostalAddressOccupancy<T>
EffectiveDate = new DateTime(2016, 2, 2),
EndDate = new DateTime(2020, 2, 2),
IsCorrespondenceAddress = true,
PostalAddress = new PostalAddress { Address = "po box here" },
PostalAddressType = "po box",
var json = Newtonsoft.Json.JsonConvert.SerializeObject(address, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nInput JSON: ");
RestSharp.RestResponse response = new RestSharp.RestResponse();
response.ContentType = "application/json";
var address2 = response.DeserializePostalAddressOccupancy();
Assert.IsTrue(address.GetOccupant().GetType() == address2.GetOccupant().GetType());
var json2 = Newtonsoft.Json.JsonConvert.SerializeObject(address2, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nDeserialized and re-serialized {0}:", address);
Console.WriteLine(json2);
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("RestSharp version: " + typeof(RestResponse).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");