Using System.Type as <T> at runtime when deserializing with Json.Net

ぐ巨炮叔叔 提交于 2019-12-20 05:15:04

问题


I have a process that needs to be able to call on a function in the following manner.

public static Task<string> Convert(string payload, Type type)
{
    JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<--- Pull in extension methods to handle custom types
    return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<type>(payload), settings));
}

The code above does not compile. Next, I used John Skeet's answer to a 2011 question similar to mine.

public static string Convert(string payload, Type type)
{
    JsonSerializerSettings settings = new JsonSerializerSettings().Configure();
    //This clashes with another overload of DeserializeObject in the method table
    MethodInfo method = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string) });
    method = method.MakeGenericMethod(type);
    object[] arguments = new object[1];
    arguments[0] = payload;
    string result = (string)method.Invoke(null, arguments);

    return JsonConvert.SerializeObject(result, settings);
}

There is an overload non-generic and a generic overload with the same name and the method table lookup fails. I was able to get around that problem by calling into my function (non-ambiguous) similar to below.

public static string Convert(string payload, Type type)
{
    JsonSerializerSettings settings = new JsonSerializerSettings().Configure();
    //This clashes with another overload of DeserializeObject in the method table
    MethodInfo method = typeof(TestConverter).GetMethod("TestThis", new[] { typeof(string) });
    method = method.MakeGenericMethod(type);
    object[] arguments = new object[1];
    arguments[0] = payload;
    string result = (string)method.Invoke(null, arguments);

    return JsonConvert.SerializeObject(result, settings);
}

public static T TestThis<T>(string s)
{
    return JsonConvert.DeserializeObject<T>(s);
}

However, I am now back to where my custom conversion extension methods are being ignored because the de-serializer is ignoring my Type type.

Is this even possible. Of courseI could use the de-serialization version similar to:

object result = JsonConvert.DeserializeObject(payload);

However, my exercise is to use the JsonConvert.DeserializeObject<T>(payload) overload so that my converters can be invoked.

Any tips?


回答1:


You do not need to jump through hoops. JsonConvert.DeserializeObject has a non-generic overload that accepts a Type.

public static Task<string> Convert(string payload, Type type)
{
    JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<--- Pull in extension methods to handle custom types
    return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(payload, type), settings));
}


来源:https://stackoverflow.com/questions/43552539/using-system-type-as-t-at-runtime-when-deserializing-with-json-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!