强制 ServiceStack 在 JSON 输出中包含空字段? [英] Force ServiceStack to include a null field in JSON output?

查看:39
本文介绍了强制 ServiceStack 在 JSON 输出中包含空字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对象定义:

Public Class ApplicationError
    Public Property Id As Integer
    Public Property Application As Application ' object defined elsewhere
    Public Property Task As String
    Public Property Description As String
    Public Property ErrorTimestamp As DateTime
    Public Property RestartTimestamp As DateTime? ' <-- nullable
    Public Property ApplicationStopped As Boolean
End Class

响应消息定义包含属性 Result() As ApplicationError.生成的 JSON 如下所示:

The response message definition contains a property Result() As ApplicationError. The JSON produced looks like this:

{
    "Result": [
        {
            "Id": 4,
            "Application": {
                "ID": 4,
                "Name": "An Application",
                ...
            },
            "Task": "Fake Error",
            "Description": "Data to validate monitoring app.",
            "ErrorTimestamp": "2015-01-23T12:22:01.5830000",
            "ApplicationStopped": false
        }
    ]
}

请注意,该 Result 对象中没有 RestartTimestamp 属性.此数据将进入 Knockout 应用程序.Knockout 将抛出错误,因为 RestartTimestamp 属性缺失.我需要一个 RestartTimestamp: null 属性.是的,我可以在我的客户端应用程序中添加代码来做到这一点.我真的不想那样做;客户端应用程序相当有序,我想保持这种状态.

Notice that there is no RestartTimestamp property in that Result object. This data is going to a Knockout application. Knockout is going to throw an error because the RestartTimestamp property is missing. I need a RestartTimestamp: null property. Yes, I could add code in my client application to do this. I really don't want to do that; the client application is rather orderly and I'd like to keep it that way.

如何让 ServiceStack 创建我需要的 RestartTimestamp: null 属性?

How can I make ServiceStack create the RestartTimestamp: null property that I need?

推荐答案

您可以配置为发出 null 属性:

You can configure to emit null properties with:

JsConfig.IncludeNullValues = true;

这篇关于强制 ServiceStack 在 JSON 输出中包含空字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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