使用System.Type作为< T>在运行时使用Json.Net进行反序列化 [英] Using System.Type as <T> at runtime when deserializing with Json.Net

查看:175
本文介绍了使用System.Type作为< T>在运行时使用Json.Net进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static Task< p>串GT; Convert(string payload,Type type)
{
JsonSerializerSettings settings = new JsonSerializerSettings()。Configure(); //< ---拉取扩展方法来处理自定义类型
return JsonConvert .SerializeObject(JsonConvert.DeserializeObject< type>(payload),settings));
}

上面的代码不能编译。接下来,我使用约翰斯基特的回答到2011年类似的问题
$ b

  public static string Convert(string payload,Type type)
{
JsonSerializerSettings settings = new JsonSerializerSettings()。Configure();
//这与方法表中另一个DeserializeObject重载冲突
MethodInfo method = typeof(JsonConvert).GetMethod(DeserializeObject,new [] {typeof(string)});
method = method.MakeGenericMethod(type);
object [] arguments = new object [1];
参数[0] =有效载荷;
string result =(string)method.Invoke(null,arguments);

返回JsonConvert.SerializeObject(result,settings);

$ / code>

有一个非泛型重载和一个泛型重载方法表查找失败。我能够通过调用我的函数(非模糊)来解决这个问题,类似于下面。

  public static string Convert (字符串有效载荷,类型类型)
{
JsonSerializerSettings settings = new JsonSerializerSettings()。Configure();
//这与方法表中的DeserializeObject的另一个重载冲突
MethodInfo method = typeof(TestConverter).GetMethod(TestThis,new [] {typeof(string)});
method = method.MakeGenericMethod(type);
object [] arguments = new object [1];
参数[0] =有效载荷;
string result =(string)method.Invoke(null,arguments);

返回JsonConvert.SerializeObject(result,settings);


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

但是,我现在回到我的自定义转换扩展方法被忽略的位置因为反序列化器忽略了我的Type类型。

这甚至是可能的。当然,我可以使用类似于以下的反序列化版本:

  object result = JsonConvert.DeserializeObject(payload); 

然而,我的练习是使用 JsonConvert.DeserializeObject< T>(

任何提示?

>解决方案

你不需要跳过篮球。 JsonConvert.DeserializeObject 具有非通用的重载,它接受类型

  public static Task< string> Convert(string payload,Type type)
{
JsonSerializerSettings settings = new JsonSerializerSettings()。Configure(); //< ---拉取扩展方法来处理自定义类型
return JsonConvert .SerializeObject(JsonConvert.DeserializeObject(payload,type),settings));
}


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?

解决方案

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));
}

这篇关于使用System.Type作为&lt; T&gt;在运行时使用Json.Net进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆