Force ServiceStack to include a null field in JSON output?

谁说我不能喝 提交于 2019-12-25 02:11:47

问题


This is the object definition:

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

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
        }
    ]
}

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.

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


回答1:


You can configure to emit null properties with:

JsConfig.IncludeNullValues = true;


来源:https://stackoverflow.com/questions/28118935/force-servicestack-to-include-a-null-field-in-json-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!