Jay Bienvenu
Jay Bienvenu

Reputation: 3293

Force ServiceStack to include a null field in JSON output?

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?

Upvotes: 1

Views: 594

Answers (1)

mythz
mythz

Reputation: 143319

You can configure to emit null properties with:

JsConfig.IncludeNullValues = true;

Upvotes: 3

Related Questions