通过JsonConverter属性对JSON.NET上的类进行NullValueHandling设置(对于Azure DocumentDb) [英] NullValueHandling setting for a class on JSON.NET by JsonConverter attribute (for Azure DocumentDb)

查看:59
本文介绍了通过JsonConverter属性对JSON.NET上的类进行NullValueHandling设置(对于Azure DocumentDb)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个DAO文件,这些文件存储在Azure DocumentDb中,现在我希望不将空值存储在DocDb中,这可以通过属性的[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]属性来实现.但是我不想在每个属性上都放这样的属性.

I have several DAO files, which are stored in Azure DocumentDb, and now I want that the null values won't be stored in the DocDb, it is possible by the [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] attribute for properties. But I don't wan't to put such attribute on every property.

问题是,无法为Azure DocumentDb API使用的Json序列化程序设置JsonSerializerSettings.

The problem is, there isn't any way to set the JsonSerializerSettings for the Json serializer used by the Azure DocumentDb API.

在我看来,要使用的方法是在类上使用JsonConverter属性,并创建一个自定义的JsonConverter类,该类将使用标准序列化,但要更改序列化设置.

The way which seems for me to go, is to use the JsonConverter attribute on a class, and create a custom JsonConverter class which will use standard serialization but with changing the serialization settings.

那是转换器:

public class CommonJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JObject.ReadFrom(reader);
        return token.ToObject(objectType, serializer);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.NullValueHandling = NullValueHandling.Ignore;
        var jo = JObject.FromObject(value, serializer);
        jo.WriteTo(writer);
    }
}

但是我在WriteJson上遇到了这样的异常:

but I get on WriteJson such exception:

类型的第一次机会异常 "Newtonsoft.Json.JsonSerializationException"发生在 Newtonsoft.Json.dll

A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

其他信息:检测到带有类型的自引用循环 '基础设施.Dao.Contacts.PersonDao'.路径.

Additional information: Self referencing loop detected with type 'Infrastructure.Dao.Contacts.PersonDao'. Path ''.

我试图将WriteJson函数更改为:

I tried to change the WriteJson function to:

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var jo = JObject.FromObject(value, new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore });
        jo.WriteTo(writer);
    }

但是我得到:

类型为'System.StackOverflowException'的未处理异常 发生在Newtonsoft.Json.dll

An unhandled exception of type 'System.StackOverflowException' occurred in Newtonsoft.Json.dll

推荐答案

我通过设置默认的全局设置解决了该问题:

I solved it by setting the default global settings:

    JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    };

这篇关于通过JsonConverter属性对JSON.NET上的类进行NullValueHandling设置(对于Azure DocumentDb)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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