访问ASP.NET Web API中的默认JSON.NET序列化器 [英] Access the default JSON.NET serializer in ASP.NET Web API

查看:44
本文介绍了访问ASP.NET Web API中的默认JSON.NET序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Web API项目,由于它不支持具有2个主体参数,因此我使用JObject参数,然后从中提取实际参数.这样.

I have a ASP.NET web API project and since it does not support having 2 body parameters, I use a JObject parameter and then extract the actual parameters from it. Like this.

Public bool mymethod(JObject data){
   myclassA a = data["a"].toObject<myclassA>();
   myclassA b = data["b"].toObject<myclassB>();
}

但是2个类类型实现ISerializable,我需要JSON.NET来忽略它.我已经设置了默认的JSON.NET序列化程序来执行此操作,并且在自动完成序列化后它可以正常工作.

But the 2 class types implement ISerializable and I need the JSON.NET to ignore it. I have set up the default JSON.NET serializer to do that and it works fine when serialization is done automatically.

但是我需要获得对内置JSON.NET序列化器的引用,以便可以在上面的代码中像这样使用它.

But I need to get a reference to the built in JSON.NET serializer so that I could use it like this in the above code.

myclassA b = data["b"].toObject<myclassB>(defaultSerializer);

当前,我创建一个JSON.NET序列化程序的新实例并使用它.但是,如何在asp.net WEB API中获得对默认内置序列化程序的引用?

Currently I create a new instance of the JSON.NET serializer and use it. But how can I get a reference to the default built in serializer in the asp.net WEB API ?

我也无法更改类类型中的任何内容,因为这是我正在转换为Web API的旧版应用程序.谢谢.

Also I cannot change anything in class types as this is sort of a legacy app that I'm converting to web api. Thanks.

推荐答案

尝试一下:

JsonSerializer serializer = JsonSerializer.Create(Configuration.Formatters.JsonFormatter.SerializerSettings);

这应该为您提供与Web API相同的序列化器.

That should give you the same serializer Web API uses.

这篇关于访问ASP.NET Web API中的默认JSON.NET序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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