- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有时我需要通过 Json.NET 抑制 "$type"
属性的输出,即使由 JsonPropertyAttribute.ItemTypeNameHandling
指定也是如此.如何做到这一点?
我的根类如下所示:
public class DomainResource
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)]
public List<Extension> Extensions { get; set; }
}
此外,我还有一个用于Extension
的类层次结构,如下所示:
public class Extension
{
readonly string url;
public string Url { get { return url; } }
public Extension(string url)
{
this.url = url;
}
}
public class IntegerExtension : Extension
{
public IntegerExtension(string url) : base(url) { }
[JsonProperty("ValueInteger")]
public int Value { get; set; }
}
我想在序列化过程中的某些情况下忽略 ItemTypeNameHandling
,但我无法找到一种方法来做到这一点。当我不想使用以下代码的 "$type"
属性时,我尝试使用 TypeNameHandling.None 设置 JsonSerializerSettings 作为 jsonconvert 的输入:
public static string SerializeObject(object value)
{
JsonSerializerSettings jsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.None,
};
jsonSettings.Converters.Add(new StringEnumConverter
{
CamelCaseText = true
});
return JsonConvert.SerializeObject(value, Formatting.None, jsonSettings);
}
然后按如下方式使用它:
var res = new DomainResource();
res.Extensions = new List<Extension>();
res.Extensions.Add(new IntegerExtension("ewwer"){Value = 3});
var x = CustomJsonConvert.SerializeObject(res);
我想要的 JSON 是:
{"extensions":[{"valueInteger":3,"url":"ewwer"}]}
但它包含 "$type"
属性,如下所示:
{"extensions":[{"$type":"DicomtoJsonConverter.IntegerExtension, DicomtoJsonConverter","valueInteger":3,"url":"ewwer"}]}
如何在不更改 DomainResource
类的情况下抑制 "$type"
属性的输出?
最佳答案
您可以使用 custom ContractResolver
即使由 JsonPropertyAttribute.TypeNameHandling
指定,也可以抑制类型信息的输出, JsonPropertyAttribute.ItemTypeNameHandling
或 JsonContainerAttribute.ItemTypeNameHandling
.首先,定义以下契约解析器:
public class NoTypeNameHandlingContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
// Suppress JsonPropertyAttribute.TypeNameHandling
property.TypeNameHandling = null;
// Suppress JsonPropertyAttribute.ItemTypeNameHandling
property.ItemTypeNameHandling = null;
return property;
}
protected override JsonContract CreateContract(Type objectType)
{
var contract = base.CreateContract(objectType);
if (contract is JsonContainerContract)
{
// Suppress JsonContainerAttribute.ItemTypeNameHandling
((JsonContainerContract)contract).ItemTypeNameHandling = null;
}
return contract;
}
}
然后,修改CustomJsonConvert.SerializeObject()
如下:
public static class CustomJsonConvert
{
// You may want to cache the contract resolver for best performance, see
// https://stackoverflow.com/questions/33557737/does-json-net-cache-types-serialization-information
static readonly JsonSerializerSettings jsonSettings;
static CustomJsonConvert()
{
jsonSettings = new JsonSerializerSettings
{
ContractResolver = new NoTypeNameHandlingContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy
{
// These are the settings used by CamelCasePropertyNamesContractResolver by default.
// Change them if this is not what you want.
OverrideSpecifiedNames = true,
ProcessDictionaryKeys = true,
},
},
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.None,
Converters = { new StringEnumConverter { CamelCaseText = true } },
};
}
public static string SerializeObject(object value)
{
return JsonConvert.SerializeObject(value, Formatting.None, jsonSettings);
}
}
如果您使用的 Json.NET 版本早于 9.0.1自 NamingStrategy
以来,您将需要子类化 CamelCasePropertyNamesContractResolver
而不是子类化 DefaultContractResolver
在该版本中引入。
关于c# - 如何在 Json.NET 中使用 JsonSerializerSettings 在属性中指定时禁用 TypeNameHandling?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46925930/
我对开发领域有点陌生,我正在开发一个 Android 项目。我正在尝试使用手机相机闪光灯。我试图让它以重复的 Action 闪烁。下面是我的 .java 文件中的一个剪辑。 public vo
我正在制作一个 ios 应用程序,它是一种诗集。我想要的功能之一是定时“阅读”,因此一首诗中的单词出现的时间与我阅读时的时间相同。所以我有一个数组,里面有诗中的词,还有延迟。 现在我想遍历单词,在屏幕
这个问题在这里已经有了答案: Loop doesn't see value changed by other thread without a print statement (1 个回答) 关闭
我如何才能使用计时器?例如,我想显示某个文本 10 秒,然后我想在剩余时间内显示不同的文本。 谢谢 凯文 最佳答案 延迟操作的最简单方法是使用 NSObject 的 PerformSelector:w
我在这里的措辞上有点困难,但要点是我使用返回一个对象的 Promise.race (如下所示)。大多数时候,至少有一些 promise 会崩溃,但这是故意的。这仅意味着未找到产品。为了避免 Promi
我正在寻找定时 JDialog,它会在指定时间过后消失,并且找到的代码运行正常。 JFrame f = new JFrame(); final JDia
我试图让选择器在不同的时间轮换,我想我会尝试一个简单的 if/else 语句,让第一个选择器在 3 秒后轮换,接下来的选择器在 30 秒后轮换。然而,它只是每三秒旋转一次。如果我想出了如何完成这项工作
这个问题已经有答案了: How to implement a timer in c? (7 个回答) Creating a Timer in C (2 个回答) 已关闭 9 年前。 我需要根据用户输入
我有一个在我文档的 head 标签中调用的 javascript 文件,每隔 30 秒,我希望重新加载这个 javascript 文件。 我对此做了一些研究,似乎它在提取本地存储的文件副本或跨浏览器问
我想看看运行 bash 函数需要多长时间。在做了一些研究之后,我想出了这种使用子 shell 的方法: function test-function() { time ( rs
我正在使用 jQuery,并且我有一个 aAax 请求,如下所示; $.ajax({ type: 'POST', url: 'test.php',
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我想为系统的每笔交易制定一个程序任务,以预留15分钟的等待时间。如果超过15分钟,程序将改变状态/状态。如果状态在 15 分钟内发生变化,则结束任务。我可以应用更多更好的代码吗?比如Wait/Slee
我正在制作一个打字速度测试程序,它有一个循环需要运行 60 秒然后退出并显示结果。我读过其他关于为 C++ 程序计时的地方,但我的研究没有定论。该程序正在运行 (llbd),我希望有人有解决方案/更好
我试图在一个简单的游戏中每 0.5 - 2 秒随机创建一个障碍。我已经有了创建障碍的功能,但我无法为实例化计时。我试过研究这个,但我还没有想出任何相关的东西。你们能帮帮我吗? 最佳答案 您可以使用 p
我有以下测试,在运行特别长的 fib 断言时不会失败。 未正确失败的测试 #!/usr/env/bin python2.7 import unittest from fib import fib fr
我想知道这是否可行。有没有一种方法可以使用 PHP 每 24 小时更新一次行的值。为什么?我正在创建一个使用在线货币(称为 Yads)的 child 网站。 Row 的值是用户拥有的 Yad 数量。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
有 Cron API 这样的东西吗? 我的意思是,是否有一种编程方式可以在不影响 Cron 的情况下添加/删除 Cron 作业? 最佳答案 UNIX cron 的 API 是文件系统。有一个用于安装/
1、SpringBoot:集成Swagger终极版 学习目标: 了解Swagger的概念及作用 掌握在项目中集成Swagger自动生成API文档 1.1、Sw
我是一名优秀的程序员,十分优秀!