CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
// https://stackoverflow.com/questions/47923402/deserializing-rest-resource-to-generic-object
using SimpleJson;
using RestSharp;
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()
{
return Occupant;
}
}
public class PostalAddress
{
// Or whatever. Type was not included in the question.
public string Address { get; set; }
}
public class Student
{
public string Name { get; set; }
public string StudentId { get; set; }
}
public class Staff
{
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;
// Add logic to recognize other cases as required.
if (occupant.ContainsKey("StudentId"))
{
return typeof(PostalAddressOccupancy<Student>);
}
else if (occupant.ContainsKey("StaffId"))
{
return typeof(PostalAddressOccupancy<Staff>);
}
}
// Or throw an exception, if you prefer.
return typeof(PostalAddressOccupancy<>).MakeGenericType(new[] { occupantObj.GetType() });
}
}
class TestClass
{
public static void Test()
{
Console.WriteLine("RestSharp version: " + typeof(RestResponse).Assembly.FullName);
Console.WriteLine();
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),
EntityType = "foo",
Href = "http://foo",
Id = "1010101",
IsCorrespondenceAddress = true,
PostalAddress = new PostalAddress { Address = "po box here" },
PostalAddressType = "po box",
Occupant = occupant,
};
var json = Newtonsoft.Json.JsonConvert.SerializeObject(address, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine("\nInput JSON: ");
Console.WriteLine(json);
RestSharp.RestResponse response = new RestSharp.RestResponse();
response.Content = json;
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 class Program
{
public static void Main()
{
Console.WriteLine("Environment version: " + Environment.Version);
//Console.WriteLine("Json.NET version: " + typeof(Newtonsoft.Json.JsonSerializer).Assembly.FullName);
Console.WriteLine("RestSharp version: " + typeof(RestResponse).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}
public class AssertionFailedException : System.Exception
{
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
}
public static class Assert
{
public static void IsTrue(bool value)
{
IsTrue(value, "failed");
}
public static void IsTrue(bool value, string message)
{
if (value == false)
throw new AssertionFailedException(message ?? "failed");
}
}