有什么方法可以将JsonSerializerSettings添加到自定义JsonConverter [英] Is there any way to add JsonSerializerSettings to a custom JsonConverter

查看:285
本文介绍了有什么方法可以将JsonSerializerSettings添加到自定义JsonConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用json.net时,我需要调整(de)序列化器设置,因此我创建了一个 JsonSerializerSettings 实例并像这样使用它:

In using json.net I'm required to adjust the (de)serializer settings, so I create a JsonSerializerSettings instance and use it like this:

JsonConvert.DeserializeObject<SomeType>(someString, mySerializerSettings.ss); 

但是我发现有必要使用自定义的 JsonConverter 处理一些爆炸数据,然后像这样使用它:

But I then find it necessary to use a custom JsonConverter for some banged up data, and then use it like this:

JsonConvert.DeserializeObject<SomeType>(someString, new myConverter());

但是我现在发现我需要同时使用 JsonSerializerSettings 和自定义转换器,但是我看不到 DeserializeObject 方法的任何重载.

But I'm now finding I need to use both JsonSerializerSettings and a custom converter but I don't see any overloads of the DeserializeObject method that would allow that.

我可能会缺少什么?

我正在使用.net v4.5.2和json.net v10.0.

I'm using .net v4.5.2 and json.net v10.0.

推荐答案

JsonSerializerSettings 有一个 Converters 集合.因此,将转换器添加到该集合中,然后将设置传递给 DeserializeObject .

JsonSerializerSettings has a Converters collection. So add your converter to that collection and then pass the settings to DeserializeObject.

mySerializerSettings.ss.Converters.Add(new myConverter());
var obj = JsonConvert.DeserializeObject<SomeType>(someString, mySerializerSettings.ss);

这篇关于有什么方法可以将JsonSerializerSettings添加到自定义JsonConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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