How to programmatically choose a constructor during deserialization?

If it is not possible to add a [JsonConstructor] attribute to the target class (because you don’t own the code), then the usual workaround is to create a custom JsonConverter as was suggested by @James Thorpe in the comments. It is pretty straightforward. You can load the JSON into a JObject, then pick the individual properties out of it to instantiate your Claim instance. Here is the code you would need:

class ClaimConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(System.Security.Claims.Claim));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        string type = (string)jo["Type"];
        string value = (string)jo["Value"];
        string valueType = (string)jo["ValueType"];
        string issuer = (string)jo["Issuer"];
        string originalIssuer = (string)jo["OriginalIssuer"];
        return new Claim(type, value, valueType, issuer, originalIssuer);
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

To use the converter, simply pass an instance of it to the JsonConvert.DeserializeObject<T>() method call:

Claim claim = JsonConvert.DeserializeObject<Claim>(json, new ClaimConverter());

Fiddle: https://dotnetfiddle.net/7LjgGR

Leave a Comment