使用json响应中的默认值排除枚举属性 [英] Excluding enum properties with default values in json response

查看:169
本文介绍了使用json响应中的默认值排除枚举属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我的api响应的模型,它具有枚举类型的属性。枚举属性仅在某些情况下填充,其余的情况下它的默认值为0



输出
{

prop1: ABCD

prop2:0 ////这是枚举属性默认为零

}



我尝试使用自定义jsonconverter但是这并没有真正解决问题,因为我只能为它分配一个字符串值。
如果具有默认值,我如何从响应中排除枚举属性。

解决方案

p>这是解决了做两件事
1。使枚举属性为空,并将其在构造函​​数中初始化为null。结果2。使用json serializer设置NullValueHandling = NullValueHandling.Ignore


I have a model for my api response which has a property of type enum. The enum property gets populated in only some scenarios rest of the scenario it gets the default value of 0

output {
prop1: "ABCD"
prop2: 0 //// this is enum property defaulting to zero
}

I tried using the custom jsonconverter but this does not really solve the problem as I can only assign a string value to it.
How can i exclude the enum property from the response if it has the default value.

解决方案

This is resolved doing two things
1. Make the enum property as nullable and initialized it in the constructor to null.
2. Use json serializer setting NullValueHandling = NullValueHandling.Ignore

这篇关于使用json响应中的默认值排除枚举属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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