- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在我的 asp.net mvc 项目中使用 azure 缓存作为缓存提供程序与 c#我使用此方法通过 JsonSerializerSettings 序列化我的数据
public static JsonSerializerSettings GetDefaultSettings()
{
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Binder = new TypeManagerSerializationBinder(),
ContractResolver = new PrivateSetterContractResolver()
};
settings.Converters.Add(new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.RoundtripKind });
return settings;
}
我的对象是这样的
{
"Name": "Bad Boys III",
"Description": "It's no Bad Boys",
"Classification": null,
"Studio": null,
"ReleaseCountries": null
}
一切正常,但我想为 null 列返回“{}”而不是 null。
{
"Name": "Bad Boys III",
"Description": "It's no Bad Boys",
"Classification": {},
"Studio": {},
"ReleaseCountries": {}
}
有什么配置可以帮我做到这一点吗?
最佳答案
您需要调整您的自定义 ContractResolver。它可能看起来像这样(我没有测试):
JsonSerializerSettings settings = new JsonSerializerSettings
{
...
ContractResolver= new MyCustomContractResolver()
};
public class MyCustomContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties().Select( p =>
{
var property = base.CreateProperty(p, memberSerialization);
property.ValueProvider = new MyCustomNullValueProvider(p);
return property;
}).ToList();
}
}
public class MyCustomNullValueProvider : IValueProvider
{
PropertyInfo _MemberInfo;
public MyCustomNullValueProvider(PropertyInfo memberInfo)
{
_MemberInfo = memberInfo;
}
public object GetValue(object target)
{
object value = _MemberInfo.GetValue(target);
if (value == null)
result = "{}";
else
return value;
}
public void SetValue(object target, object value)
{
if ((string)value == "{}")
value = null;
_MemberInfo.SetValue(target, value);
}
}
关于c# - 在 JsonSerializerSettings 中将 null 更改为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28720925/
这个问题在这里已经有了答案: Supporting multiple custom DateTime formats when deserializing with Json.Net (2 个回答)
我想为 DateTime 使用不同的格式关于查询字符串参数的字段:即如果查询字符串包含 dateFormat=iso参数,我想返回 ISO 6801 中的日期: { "date": "2018-0
我尝试实现来自 Newtonsoft Documentation 的错误处理代码 我正在使用以下 JsonSerializerSettings errors = new JsonErrors(); j
我正在为我的类属性的驼峰式外壳使用以下设置。 JsonSerializerSettings settings = new JsonSerializerSettings() {
如果我在我的项目中使用相同的设置,我可以实例化我的 JsonSerializerSettings 的单个静态副本并在整个过程中使用它吗? 例如: private static readonly Jso
在使用 json.net 时,我需要调整(反)序列化器设置,因此我创建一个 JsonSerializerSettings 实例并按如下方式使用它: JsonConvert.DeserializeObj
在使用 json.net 时,我需要调整(反)序列化器设置,因此我创建一个 JsonSerializerSettings 实例并按如下方式使用它: JsonConvert.DeserializeObj
我已经为 JSON.NET 设置了全局转换器默认值,如下所示: var jsonSerializerSettings = new JsonSerializerSettings
我在我的 asp.net mvc 项目中使用 azure 缓存作为缓存提供程序与 c#我使用此方法通过 JsonSerializerSettings 序列化我的数据 public static Jso
尝试设置 JsonOutputFormatter 选项: var jsonFormatter = (JsonOutputFormatter) options.OutputFormatters.Firs
给定一个 JsonSerializerSettings 对象、一个类型和一个 System.Reflection.PropertyInfo 对象,我如何确定: 1) 当使用给定设置使用 JSON.Ne
有什么方法可以从 JsonSerializer 类中取出设置并在新的 JsonSerializer 中重新实现它们? 似乎没有任何方法可以做那样的事情。我发现最好的是通过反射调用的私有(private
我有一个带有 CosmosDB 输出绑定(bind)的 Azure 函数,如下所示: public static class ComponentDesignHttpTrigger { [Fun
有时我需要通过 Json.NET 抑制 "$type" 属性的输出,即使由 JsonPropertyAttribute.ItemTypeNameHandling 指定也是如此.如何做到这一点? 我的根
我想使用自定义 JsonSerializerSettings 使用 DocumentDb API 创建文档。谁能告诉我如何做到这一点? 我尝试过设置 JsonConvert.DefaultSettin
我在使用 向服务器发送消息时收到以下错误SignalR 2.0.0 : SignalR: The reader's MaxDepth of 20 has been exceeded. 我使用 Unit
使用 Newton.Json 进行 Json 序列化。当我必须使用 Json 序列化一个具有字节数组属性的对象,然后以十六进制格式显示字节数组时,应用什么 JsonSerializerSettings
在我正在做的 C# 应用程序中 var settings = new ConnectionSettings(node); settings.ModifyJsonSerializerSettings(n
我在 .net 中有一个代码将请求序列化为 json 格式......代码是这样的。 var ops = new Newtonsoft.Json.JsonSerializerSettings();
我知道 ASP.NET Web API native 使用 Json.NET 来(反)序列化对象,但是有没有办法指定您希望它使用的 JsonSerializerSettings 对象? 例如,如果我想
我是一名优秀的程序员,十分优秀!