问题
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