Is there a way to ignore get-only properties in Json.NET without using JsonIgnore attributes?(有没有办法在不使用 JsonIgnore 属性的情况下忽略 Json.NET 中的 get-only 属性?)
问题描述
有没有办法使用 Json.NET 序列化程序但不使用 JsonIgnore
属性?
Is there a way to ignore get-only properties using the Json.NET serializer but without using JsonIgnore
attributes?
例如,我有一个具有这些获取属性的类:
For example, I have a class with these get properties:
public Keys Hotkey { get; set; }
public Keys KeyCode
{
get
{
return Hotkey & Keys.KeyCode;
}
}
public Keys ModifiersKeys
{
get
{
return Hotkey & Keys.Modifiers;
}
}
public bool Control
{
get
{
return (Hotkey & Keys.Control) == Keys.Control;
}
}
public bool Shift
{
get
{
return (Hotkey & Keys.Shift) == Keys.Shift;
}
}
public bool Alt
{
get
{
return (Hotkey & Keys.Alt) == Keys.Alt;
}
}
public Modifiers ModifiersEnum
{
get
{
Modifiers modifiers = Modifiers.None;
if (Alt) modifiers |= Modifiers.Alt;
if (Control) modifiers |= Modifiers.Control;
if (Shift) modifiers |= Modifiers.Shift;
return modifiers;
}
}
public bool IsOnlyModifiers
{
get
{
return KeyCode == Keys.ControlKey || KeyCode == Keys.ShiftKey || KeyCode == Keys.Menu;
}
}
public bool IsValidKey
{
get
{
return KeyCode != Keys.None && !IsOnlyModifiers;
}
}
我是否需要将 [JsonIgnore]
添加到所有这些(我还有许多其他类),或者有更好的方法来忽略所有仅获取属性?
Do I need to add [JsonIgnore]
to all of them (I also have many other classes), or there is better way to ignore all get-only properties?
推荐答案
您可以通过实现自定义 IContractResolver
并在序列化期间使用它来做到这一点.如果您将 DefaultContractResolver
子类化,这将变得非常容易:
You can do this by implementing a custom IContractResolver
and using that during serialization. If you subclass the DefaultContractResolver
, this becomes very easy to do:
class WritablePropertiesOnlyResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
return props.Where(p => p.Writable).ToList();
}
}
这是一个演示如何使用它的测试程序:
Here is a test program demonstrating how to use it:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
class Program
{
static void Main(string[] args)
{
Widget w = new Widget { Id = 2, Name = "Joe Schmoe" };
JsonSerializerSettings settings = new JsonSerializerSettings
{
ContractResolver = new WritablePropertiesOnlyResolver()
};
string json = JsonConvert.SerializeObject(w, settings);
Console.WriteLine(json);
}
}
class Widget
{
public int Id { get; set; }
public string Name { get; set; }
public string LowerCaseName
{
get { return (Name != null ? Name.ToLower() : null); }
}
}
这是上面的输出.请注意,只读属性 LowerCaseName
未包含在输出中.
Here is the output of the above. Notice that the read-only property LowerCaseName
is not included in the output.
{"Id":2,"Name":"Joe Schmoe"}
这篇关于有没有办法在不使用 JsonIgnore 属性的情况下忽略 Json.NET 中的 get-only 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在不使用 JsonIgnore 属性的情况下忽略 Json.NET 中的 get-only 属性?
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01